OLED显示
基本参数:
?OLED(Organic Light Emitting Diode):有机发光二极管
?OLED显示屏:性能优异的新型显示屏,具有功耗低、相应速度快、宽视角、轻薄柔韧等特点
?0.96寸OLED模块:小巧玲珑、占用接口少、简单易用,是电子设计中非常常见的显示屏模块
?供电:3~5.5V,通信协议:I2C/SPI,分辨率:128*64
本篇文章默认你是有一些I2C通讯的基础知识的,所以在源码展示部分有直接调用的 I2C_SAND_BYTE(),I2C发送数据函数
常见的硬件连接方式:
电亮方式:
将分辨率为128*64的屏幕分为多个8x8像素块来显示:
那么以8x8像素块为基本单位而言,行就有8个像素块,列就有16个像素块
单独对于一个小的8x8像素块而言,根据芯片的配置,采用“纵向取模下高位的方式”。即如图电亮第一列的像素点需要输入的数据为0x47,转化为二进制数据为0100 0111。二进制数的高位在下,低位在上。“1”表示电亮,“0”表示熄灭
对于英文,数字,汉字而言需要用不同方式的8x8元素块进行拼接显示,这样显示的效果最好,后续会详细说明
(下面的代码注释紊乱是编码的问题,复制到Keil5里选择ANSI编码就没问题了)
本篇默认有I2C的基础
OLED初始化
#define OLED0561_ADD 0x78
#define COM 0x00
#define DAT 0x40
void OLED0561_Init (void){
OLED_DISPLAY_OFF();
OLED_DISPLAY_CLEAR();
OLED_DISPLAY_ON();
}
void OLED_DISPLAY_ON (void){
u8 buf[28]={
0xae,
0x00,0x10,
0xd5,0x80,
0xa8,0x3f,
0xd3,0x00,
0XB0,
0x40,
0x8d,0x14,
0xa1,
0xc8,
0xda,0x12,
0x81,0xff,
0xd9,0xf1,
0xdb,0x30,
0x20,0x00,
0xa4,
0xa6,
0xaf
};
I2C_SAND_BUFFER(OLED0561_ADD,COM,buf,28);
}
void OLED_DISPLAY_OFF (void){
u8 buf[3]={
0xae,
0x8d,0x10,
};
I2C_SAND_BUFFER(OLED0561_ADD,COM,buf,3);
}
void OLED_DISPLAY_LIT (u8 x){
I2C_SAND_BYTE(OLED0561_ADD,COM,0x81);
I2C_SAND_BYTE(OLED0561_ADD,COM,x);
}
void OLED_DISPLAY_CLEAR(void){
u8 j,t;
for(t=0xB0;t<0xB8;t++){
I2C_SAND_BYTE(OLED0561_ADD,COM,t);
I2C_SAND_BYTE(OLED0561_ADD,COM,0x10);
I2C_SAND_BYTE(OLED0561_ADD,COM,0x00);
for(j=0;j<132;j++){
I2C_SAND_BYTE(OLED0561_ADD,DAT,0x00);
}
}
}
OLED显示ASCLL表里面的字符
void OLED_DISPLAY_8x16(u8 x,
u8 y,
u16 w){
u8 j,t,c=0;
y=y+2;
for(t=0;t<2;t++){
I2C_SAND_BYTE(OLED0561_ADD,COM,0xb0+x);
I2C_SAND_BYTE(OLED0561_ADD,COM,y/16+0x10);
I2C_SAND_BYTE(OLED0561_ADD,COM,y%16);
for(j=0;j<8;j++)
{
I2C_SAND_BYTE(OLED0561_ADD,DAT,ASCII_8x16[(w*16)+c-512]);
c++;
}
x++;
}
}
void OLED_DISPLAY_8x16(u8 x , u8 y , u16 w)
这里需要说明的是 y 的取值范围为0 ~ 128列,调用函数的时候,y输入x*8,x=0 ~ 15,一共可以表示16列字母或者数字。当然如果你觉得位置不如意的话,依据可以将y设置为0 ~ 128中间的一个数字,而不仅仅局限于那16个固定的列坐标。在设计函数的时候并没有将y设计成只输入8x8块元素的列数,而是128列像素点的列数,这样显示的时候更具有灵活性。
当然如果你觉得输入x * 8比较麻烦,也可以将y=y * 8内嵌到函数中。后面的显示字符串也确实是这么处理的
发送数据函数
void OLED_DISPLAY_8x16(u8 x , u8 y , u16 w)里面嵌套的for循环:
这里的 j 就是显示多少列的意思,对于显示ASCLL码,8列就够了,显示汉字的时候需要16列,显示图片的时候就是显示图片的宽度像素值,0~128中间的某个值。
{(w*16)+c-512}是一个将ASCLL对应字符的标号转化为数组ASCII_8x16中的序号的公式,很简单验证:因为我们这里是ASCLL表中第32号到127号,如果我们要表示ASCLL中的第32号元素那么有:(32 * 16)-512=0,恰好是数组中的第一个元素。(ASCLL数组在文末有展示)
for(j=0;j<8;j++)
{ I2C_SAND_BYTE(OLED0561_ADD,DAT,ASCII_8x16[(w*16)+c-512]);
c++;
}
OLED显示字符串
void OLED_DISPLAY_8x16_BUFFER(u8 row,u8 *str){
u8 r=0;
while(*str != '\0'){
OLED_DISPLAY_8x16(row,r*8,*str++);
r++;
}
}
对于字符串的表示,这里显然就用到我们刚刚说道的r*8来嵌入。
初始列默认为第0列(r=0),需要左移显示的字符时,进行空格操作。与显示单个字符一样,但是不管怎么说每行最多显示16列
OLED显示汉字
void OLED_DISPLAY_16x16(u8 x,
u8 y,
u16 w){
u8 j,t,c=0;
for(t=0;t<2;t++){
I2C_SAND_BYTE(OLED0561_ADD,COM,0xb0+x);
I2C_SAND_BYTE(OLED0561_ADD,COM,y/16+0x10);
I2C_SAND_BYTE(OLED0561_ADD,COM,y%16);
for(j=0;j<16;j++){
I2C_SAND_BYTE(OLED0561_ADD,DAT,GB_16[(w*32)+c]);
c++;}x++;
}
I2C_SAND_BYTE(OLED0561_ADD,COM,0xAF);
}
细心观察发现,汉字的显示和单个英文或者数字相似,只是第二个 for 循环中的 j 值的取值范围以及字库不同:GB_16[(w*32)+c]
汉字显示两行,每行有2个8x8方块,即16列,所以 j = 16
一共就有32列,所以,每个汉字由32个数据构成,即w后面乘以32
OLED显示图片
仔细观察发现与初始化的息屏操作相似,息屏是将每一个像素点都置0,这里是根据提前生产好的数组PIC1[,]来显示。
void OLED_DISPLAY_PIC1(void){
u8 m,i;
for(m=0;m<8;m++){
I2C_SAND_BYTE(OLED0561_ADD,COM,0xb0+m);
I2C_SAND_BYTE(OLED0561_ADD,COM,0x10);
I2C_SAND_BYTE(OLED0561_ADD,COM,0x02);
for(i=0;i<128;i++){
I2C_SAND_BYTE(OLED0561_ADD,DAT,PIC1[i+m*128]);}
}
}
ASCLL库,字模库以及图片库的自定义化生成:
示例:
const u8 ASCII_8x16[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x38,0xFC,0xFC,0x38,0x00,0x00,
0x00,0x00,0x00,0x0D,0x0D,0x00,0x00,0x00,
0x00,0x0E,0x1E,0x00,0x00,0x1E,0x0E,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x20,0xF8,0xF8,0x20,0xF8,0xF8,0x20,0x00,
0x02,0x0F,0x0F,0x02,0x0F,0x0F,0x02,0x00,
0x38,0x7C,0x44,0x47,0x47,0xCC,0x98,0x00,
0x03,0x06,0x04,0x1C,0x1C,0x07,0x03,0x00,
0x30,0x30,0x00,0x80,0xC0,0x60,0x30,0x00,
0x0C,0x06,0x03,0x01,0x00,0x0C,0x0C,0x00,
0x80,0xD8,0x7C,0xE4,0xBC,0xD8,0x40,0x00,
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x00,0x10,0x1E,0x0E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xF0,0xF8,0x0C,0x04,0x00,0x00,
0x00,0x00,0x03,0x07,0x0C,0x08,0x00,0x00,
0x00,0x00,0x04,0x0C,0xF8,0xF0,0x00,0x00,
0x00,0x00,0x08,0x0C,0x07,0x03,0x00,0x00,
0x80,0xA0,0xE0,0xC0,0xC0,0xE0,0xA0,0x80,
0x00,0x02,0x03,0x01,0x01,0x03,0x02,0x00,
0x00,0x80,0x80,0xE0,0xE0,0x80,0x80,0x00,
0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x10,0x1E,0x0E,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xC0,0x60,0x30,0x00,
0x0C,0x06,0x03,0x01,0x00,0x00,0x00,0x00,
0xF8,0xFC,0x04,0xC4,0x24,0xFC,0xF8,0x00,
0x07,0x0F,0x09,0x08,0x08,0x0F,0x07,0x00,
0x00,0x10,0x18,0xFC,0xFC,0x00,0x00,0x00,
0x00,0x08,0x08,0x0F,0x0F,0x08,0x08,0x00,
0x08,0x0C,0x84,0xC4,0x64,0x3C,0x18,0x00,
0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0C,0x00,
0x08,0x0C,0x44,0x44,0x44,0xFC,0xB8,0x00,
0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0xB0,0x98,0xFC,0xFC,0x80,0x00,
0x00,0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,
0x7C,0x7C,0x44,0x44,0xC4,0xC4,0x84,0x00,
0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,
0xF0,0xF8,0x4C,0x44,0x44,0xC0,0x80,0x00,
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x0C,0x0C,0x04,0x84,0xC4,0x7C,0x3C,0x00,
0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,0x00,
0xB8,0xFC,0x44,0x44,0x44,0xFC,0xB8,0x00,
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x38,0x7C,0x44,0x44,0x44,0xFC,0xF8,0x00,
0x00,0x08,0x08,0x08,0x0C,0x07,0x03,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,
0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,
0x00,0x00,0x08,0x0E,0x06,0x00,0x00,0x00,
0x00,0x80,0xC0,0x60,0x30,0x18,0x08,0x00,
0x00,0x00,0x01,0x03,0x06,0x0C,0x08,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,
0x00,0x08,0x18,0x30,0x60,0xC0,0x80,0x00,
0x00,0x08,0x0C,0x06,0x03,0x01,0x00,0x00,
0x18,0x1C,0x04,0xC4,0xE4,0x3C,0x18,0x00,
0x00,0x00,0x00,0x0D,0x0D,0x00,0x00,0x00,
0xF0,0xF8,0x08,0xC8,0xC8,0xF8,0xF0,0x00,
0x07,0x0F,0x08,0x0B,0x0B,0x0B,0x01,0x00,
0xE0,0xF0,0x98,0x8C,0x98,0xF0,0xE0,0x00,
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0x04,0xFC,0xFC,0x44,0x44,0xFC,0xB8,0x00,
0x08,0x0F,0x0F,0x08,0x08,0x0F,0x07,0x00,
0xF0,0xF8,0x0C,0x04,0x04,0x0C,0x18,0x00,
0x03,0x07,0x0C,0x08,0x08,0x0C,0x06,0x00,
0x04,0xFC,0xFC,0x04,0x0C,0xF8,0xF0,0x00,
0x08,0x0F,0x0F,0x08,0x0C,0x07,0x03,0x00,
0x04,0xFC,0xFC,0x44,0xE4,0x0C,0x1C,0x00,
0x08,0x0F,0x0F,0x08,0x08,0x0C,0x0E,0x00,
0x04,0xFC,0xFC,0x44,0xE4,0x0C,0x1C,0x00,
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xF0,0xF8,0x0C,0x84,0x84,0x8C,0x98,0x00,
0x03,0x07,0x0C,0x08,0x08,0x07,0x0F,0x00,
0xFC,0xFC,0x40,0x40,0x40,0xFC,0xFC,0x00,
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0x00,0x00,0x04,0xFC,0xFC,0x04,0x00,0x00,
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x00,0x00,0x00,0x04,0xFC,0xFC,0x04,0x00,
0x07,0x0F,0x08,0x08,0x0F,0x07,0x00,0x00,
0x04,0xFC,0xFC,0xC0,0xF0,0x3C,0x0C,0x00,
0x08,0x0F,0x0F,0x00,0x01,0x0F,0x0E,0x00,
0x04,0xFC,0xFC,0x04,0x00,0x00,0x00,0x00,
0x08,0x0F,0x0F,0x08,0x08,0x0C,0x0E,0x00,
0xFC,0xFC,0x38,0x70,0x38,0xFC,0xFC,0x00,
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0xFC,0xFC,0x38,0x70,0xE0,0xFC,0xFC,0x00,
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0xF0,0xF8,0x0C,0x04,0x0C,0xF8,0xF0,0x00,
0x03,0x07,0x0C,0x08,0x0C,0x07,0x03,0x00,
0x04,0xFC,0xFC,0x44,0x44,0x7C,0x38,0x00,
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xF8,0xFC,0x04,0x04,0x04,0xFC,0xF8,0x00,
0x07,0x0F,0x08,0x0E,0x3C,0x3F,0x27,0x00,
0x04,0xFC,0xFC,0x44,0xC4,0xFC,0x38,0x00,
0x08,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0x18,0x3C,0x64,0x44,0xC4,0x9C,0x18,0x00,
0x06,0x0E,0x08,0x08,0x08,0x0F,0x07,0x00,
0x00,0x1C,0x0C,0xFC,0xFC,0x0C,0x1C,0x00,
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0xFC,0xFC,0x00,0x00,0x00,0xFC,0xFC,0x00,
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0xFC,0xFC,0x00,0x00,0x00,0xFC,0xFC,0x00,
0x01,0x03,0x06,0x0C,0x06,0x03,0x01,0x00,
0xFC,0xFC,0x00,0x80,0x00,0xFC,0xFC,0x00,
0x03,0x0F,0x0E,0x03,0x0E,0x0F,0x03,0x00,
0x0C,0x3C,0xF0,0xC0,0xF0,0x3C,0x0C,0x00,
0x0C,0x0F,0x03,0x00,0x03,0x0F,0x0C,0x00,
0x00,0x3C,0x7C,0xC0,0xC0,0x7C,0x3C,0x00,
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x1C,0x0C,0x84,0xC4,0x64,0x3C,0x1C,0x00,
0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0E,0x00,
0x00,0x00,0xFC,0xFC,0x04,0x04,0x00,0x00,
0x00,0x00,0x0F,0x0F,0x08,0x08,0x00,0x00,
0x38,0x70,0xE0,0xC0,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x03,0x07,0x0E,0x00,
0x00,0x00,0x04,0x04,0xFC,0xFC,0x00,0x00,
0x00,0x00,0x08,0x08,0x0F,0x0F,0x00,0x00,
0x08,0x0C,0x06,0x03,0x06,0x0C,0x08,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x00,0x00,0x03,0x07,0x04,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xA0,0xA0,0xA0,0xE0,0xC0,0x00,0x00,
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x04,0xFC,0xFC,0x20,0x60,0xC0,0x80,0x00,
0x08,0x0F,0x07,0x08,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0x20,0x20,0x20,0x60,0x40,0x00,
0x07,0x0F,0x08,0x08,0x08,0x0C,0x04,0x00,
0x80,0xC0,0x60,0x24,0xFC,0xFC,0x00,0x00,
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0xC0,0xE0,0xA0,0xA0,0xA0,0xE0,0xC0,0x00,
0x07,0x0F,0x08,0x08,0x08,0x0C,0x04,0x00,
0x40,0xF8,0xFC,0x44,0x0C,0x18,0x00,0x00,
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xC0,0xE0,0x20,0x20,0xC0,0xE0,0x20,0x00,
0x27,0x6F,0x48,0x48,0x7F,0x3F,0x00,0x00,
0x04,0xFC,0xFC,0x40,0x20,0xE0,0xC0,0x00,
0x08,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0x00,0x00,0x20,0xEC,0xEC,0x00,0x00,0x00,
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0xEC,0xEC,0x00,
0x00,0x30,0x70,0x40,0x40,0x7F,0x3F,0x00,
0x04,0xFC,0xFC,0x80,0xC0,0x60,0x20,0x00,
0x08,0x0F,0x0F,0x01,0x03,0x0E,0x0C,0x00,
0x00,0x00,0x04,0xFC,0xFC,0x00,0x00,0x00,
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0xE0,0xE0,0x60,0xC0,0x60,0xE0,0xC0,0x00,
0x0F,0x0F,0x00,0x0F,0x00,0x0F,0x0F,0x00,
0x20,0xE0,0xC0,0x20,0x20,0xE0,0xC0,0x00,
0x00,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0xC0,0xE0,0x20,0x20,0x20,0xE0,0xC0,0x00,
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x20,0xE0,0xC0,0x20,0x20,0xE0,0xC0,0x00,
0x40,0x7F,0x7F,0x48,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0x20,0x20,0xC0,0xE0,0x20,0x00,
0x07,0x0F,0x08,0x48,0x7F,0x7F,0x40,0x00,
0x20,0xE0,0xC0,0x60,0x20,0x60,0xC0,0x00,
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0x40,0xE0,0xA0,0x20,0x20,0x60,0x40,0x00,
0x04,0x0C,0x09,0x09,0x0B,0x0E,0x04,0x00,
0x20,0x20,0xF8,0xFC,0x20,0x20,0x00,0x00,
0x00,0x00,0x07,0x0F,0x08,0x0C,0x04,0x00,
0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,
0x00,0x03,0x07,0x0C,0x0C,0x07,0x03,0x00,
0xE0,0xE0,0x00,0x00,0x00,0xE0,0xE0,0x00,
0x07,0x0F,0x0C,0x07,0x0C,0x0F,0x07,0x00,
0x20,0x60,0xC0,0x80,0xC0,0x60,0x20,0x00,
0x08,0x0C,0x07,0x03,0x07,0x0C,0x08,0x00,
0xE0,0xE0,0x00,0x00,0x00,0xE0,0xE0,0x00,
0x47,0x4F,0x48,0x48,0x68,0x3F,0x1F,0x00,
0x60,0x60,0x20,0xA0,0xE0,0x60,0x20,0x00,
0x0C,0x0E,0x0B,0x09,0x08,0x0C,0x0C,0x00,
0x00,0x40,0x40,0xF8,0xBC,0x04,0x04,0x00,
0x00,0x00,0x00,0x07,0x0F,0x08,0x08,0x00,
0x00,0x00,0x00,0xBC,0xBC,0x00,0x00,0x00,
0x00,0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,
0x00,0x04,0x04,0xBC,0xF8,0x40,0x40,0x00,
0x00,0x08,0x08,0x0F,0x07,0x00,0x00,0x00,
0x08,0x0C,0x04,0x0C,0x08,0x0C,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0xC0,0x60,0x30,0x60,0xC0,0x80,0x00,
0x07,0x07,0x04,0x04,0x04,0x07,0x07,0x00,
};
一般字库什么的都是用软件来生成的:
ASCLL: 汉字:
图片:
|