/* * oled.c * * Created on: 2022年4月15日 * Author: wuwen */ #include "oled.h" /* 通过IIC发送指令到OLED */ void OLED_WrCmd(unsigned char IIC_Command) { uint8_t *pData; pData = &IIC_Command; IIC_SAND_DATE(OLED_ADDRESS,0x00,pData,1); } /* 通过IIC发送数据到OLED */ void OLED_WrDat(unsigned char IIC_Data) { uint8_t *pData; pData = &IIC_Data; IIC_SAND_DATE(OLED_ADDRESS,0x40,pData,1); } /* 设置硬件内的光标 */ void OLED_Set_Pos(unsigned char x, unsigned char y) { OLED_WrCmd( 0xb0 + 7-y ); OLED_WrCmd(((x&0xf0)>>4)|0x10); OLED_WrCmd( (x&0x0f) |0x00); } /* 初始化硬件内存(显存) */ void OLED_Init_Display_Buffer(char a) { for(unsigned char y = 0;y < Y_WIDTH_;y++) { OLED_Set_Pos(0,y); for(unsigned char x = 0;x < X_WIDTH;x++) { OLED_WrDat(a); //HAL_Delay(1); } } } /* 设置屏幕亮度 */ void OLED_Setting_luminance(unsigned char a) { OLED_WrCmd(0x81); OLED_WrCmd(a*2.55); } /* 初始化OLED 硬件扫描方式 从左到右从上到下 纵向8点上高位 */ void OLED_Init(void) { //HAL_Delay(200); OLED_WrCmd(0xAE); //关闭显示 OLED_WrCmd(0xD5); //设置时钟分频因子,震荡频率 OLED_WrCmd( 80); //[3:0],分频因子;[7:4],震荡频率 OLED_WrCmd(0xA8); //设置驱动路数 OLED_WrCmd(0X3F); //默认0X3F(1/64) OLED_WrCmd(0xD3); //设置显示偏移 OLED_WrCmd(0X00); //默认为0 OLED_WrCmd(0x40); //设置显示开始行 [5:0],行数. OLED_WrCmd(0x8D); //电荷泵设置 OLED_WrCmd(0x14); //bit2,开启/关闭 OLED_WrCmd(0x20); //设置内存地址模式 OLED_WrCmd(0x02); //[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10; OLED_WrCmd(0xA1); //段重定义设置,bit0:0,0->0;1,0->127; OLED_WrCmd(0xC0); //设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数 //OLED_WrCmd(0xDA); //设置COM硬件引脚配置 //OLED_WrCmd(0x12); //[5:4]配置 //显示方向设置 //OLED_WrCmd(0xc8);//OLED_WrCmd(0xa0); //行扫描顺序:从上到下 //列扫描顺序:从左到右 OLED_WrCmd(0xD9); //设置预充电周期 OLED_WrCmd(0xf1); //[3:0],PHASE 1;[7:4],PHASE 2; OLED_WrCmd(0xDB); //设置VCOMH 电压倍率 OLED_WrCmd(0x30); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc; OLED_WrCmd(0xA4); //全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏) OLED_WrCmd(0xA6); //设置显示方式;bit0:1,反相显示;0,正常显示 OLED_WrCmd(0xAF); //开启显示 OLED_Init_Display_Buffer(0); } /*************************************************************************************************************************************************************************************/ /*硬件与软件之间的部分*/ /* 定义显存 */ char OLED_buff[Y_WIDTH_][X_WIDTH]; /* 将显存发送到硬件 */ void OLED_Cache_to_hardware() { for(uint8_t y=0;y",30*/ {0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0x6C,0x10,0x80,0x0F,0x00,0x00,0x00},/*"?",31*/ {0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x24,0x17,0xC4,0x08,0x28,0x07,0xD0,0x00,0x00},/*"@",32*/ {0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4,0x00,0x1C,0x00,0x04},/*"A",33*/ {0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88,0x00,0x70,0x00,0x00},/*"B",34*/ {0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08,0x1C,0x10,0x00,0x00},/*"C",35*/ {0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"D",36*/ {0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04,0x08,0x18,0x00,0x00},/*"E",37*/ {0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00,0x08,0x00,0x00,0x00},/*"F",38*/ {0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78,0x00,0x40,0x00,0x00},/*"G",39*/ {0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84,0x1F,0xFC,0x10,0x04},/*"H",40*/ {0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x00,0x00,0x00,0x00},/*"I",41*/ {0x00,0x03,0x00,0x01,0x10,0x01,0x10,0x01,0x1F,0xFE,0x10,0x00,0x10,0x00,0x00,0x00},/*"J",42*/ {0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C,0x10,0x04,0x00,0x00},/*"K",43*/ {0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x0C,0x00,0x00},/*"L",44*/ {0x10,0x04,0x1F,0xFC,0x1F,0x00,0x00,0xFC,0x1F,0x00,0x1F,0xFC,0x10,0x04,0x00,0x00},/*"M",45*/ {0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18,0x1F,0xFC,0x10,0x00},/*"N",46*/ {0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"O",47*/ {0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00},/*"P",48*/ {0x07,0xF0,0x08,0x18,0x10,0x24,0x10,0x24,0x10,0x1C,0x08,0x0A,0x07,0xF2,0x00,0x00},/*"Q",49*/ {0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30,0x0E,0x0C,0x00,0x04},/*"R",50*/ {0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44,0x1C,0x38,0x00,0x00},/*"S",51*/ {0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00,0x18,0x00,0x00,0x00},/*"T",52*/ {0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04,0x1F,0xF8,0x10,0x00},/*"U",53*/ {0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80,0x1C,0x00,0x10,0x00},/*"V",54*/ {0x1F,0xC0,0x10,0x3C,0x00,0xE0,0x1F,0x00,0x00,0xE0,0x10,0x3C,0x1F,0xC0,0x00,0x00},/*"W",55*/ {0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34,0x18,0x0C,0x10,0x04},/*"X",56*/ {0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00,0x10,0x00,0x00,0x00},/*"Y",57*/ {0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04,0x10,0x18,0x00,0x00},/*"Z",58*/ {0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x00,0x00},/*"[",59*/ {0x00,0x00,0x30,0x00,0x0C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C,0x00,0x03,0x00,0x00},/*"\",60*/ {0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},/*"]",61*/ {0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00},/*"^",62*/ {0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},/*"_",63*/ {0x00,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/ {0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xFC,0x00,0x04},/*"a",65*/ {0x10,0x00,0x1F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"b",66*/ {0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x00},/*"c",67*/ {0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x11,0x08,0x1F,0xFC,0x00,0x04},/*"d",68*/ {0x00,0x00,0x00,0xF8,0x01,0x44,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xC8,0x00,0x00},/*"e",69*/ {0x00,0x00,0x01,0x04,0x01,0x04,0x0F,0xFC,0x11,0x04,0x11,0x04,0x11,0x00,0x18,0x00},/*"f",70*/ {0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9,0x01,0x06,0x00,0x00},/*"g",71*/ {0x10,0x04,0x1F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"h",72*/ {0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"i",73*/ {0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE,0x00,0x00,0x00,0x00},/*"j",74*/ {0x10,0x04,0x1F,0xFC,0x00,0x24,0x00,0x40,0x01,0xB4,0x01,0x0C,0x01,0x04,0x00,0x00},/*"k",75*/ {0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"l",76*/ {0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04,0x01,0x00,0x00,0xFC},/*"m",77*/ {0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"n",78*/ {0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0xF8,0x00,0x00},/*"o",79*/ {0x01,0x01,0x01,0xFF,0x00,0x85,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"p",80*/ {0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x05,0x01,0xFF,0x00,0x01},/*"q",81*/ {0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00,0x01,0x80,0x00,0x00},/*"r",82*/ {0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x98,0x00,0x00},/*"s",83*/ {0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04,0x00,0x00,0x00,0x00},/*"t",84*/ {0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08,0x01,0xFC,0x00,0x04},/*"u",85*/ {0x01,0x00,0x01,0x80,0x01,0x70,0x00,0x0C,0x00,0x10,0x01,0x60,0x01,0x80,0x01,0x00},/*"v",86*/ {0x01,0xF0,0x01,0x0C,0x00,0x30,0x01,0xC0,0x00,0x30,0x01,0x0C,0x01,0xF0,0x01,0x00},/*"w",87*/ {0x00,0x00,0x01,0x04,0x01,0x8C,0x00,0x74,0x01,0x70,0x01,0x8C,0x01,0x04,0x00,0x00},/*"x",88*/ {0x01,0x01,0x01,0x81,0x01,0x71,0x00,0x0E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},/*"y",89*/ {0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},/*"z",90*/ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x3E,0xFC,0x40,0x02,0x40,0x02},/*"{",91*/ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},/*"|",92*/ {0x00,0x00,0x40,0x02,0x40,0x02,0x3E,0xFC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"}",93*/ {0x00,0x00,0x60,0x00,0x80,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x20,0x00},/*"~",94*/ }; /* 打点函数 打点类型 dot_type=0 清除点 =1 打点 =2 取反点 */ void OLED_set_dot(unsigned char x,unsigned char y,unsigned char dot_type) { if(x>=X_WIDTH||y>=Y_WIDTH) { return ; } uint8_t y1=y/8; uint8_t y2=y%8; uint8_t y3=(0x80>>y2); switch(dot_type) { case 0: OLED_buff[y1][x] &= ~y3; break; case 1: OLED_buff[y1][x] |= y3; break; case 2: OLED_buff[y1][x] ^= y3; break; case 3:break; } } /* 绘图函数 数据格式 从左到右,从上到下,横向8点右高位 type=0 清空区域 =1 区域图层 =2 正片叠底 =3 正片清空 =4 负片 */ void OLED_Pix(unsigned char x,unsigned char y,unsigned char w,unsigned char h,const char *p,unsigned char type) { unsigned char b=0; unsigned int c=0; for(uint8_t h1=0;h1>b) { switch(type) { case 0: OLED_set_dot(w1+x,h1+y,0); break; case 1: OLED_set_dot(w1+x,h1+y,1); break; case 2: OLED_set_dot(w1+x,h1+y,1); break; case 3: OLED_set_dot(w1+x,h1+y,0); break; case 4: OLED_set_dot(w1+x,h1+y,0); break; } }else { switch(type) { case 0: OLED_set_dot(w1+x,h1+y,0); break; case 1: OLED_set_dot(w1+x,h1+y,0); break; case 2: break; case 4: OLED_set_dot(w1+x,h1+y,1); break; } } b++; if(b==8) { b=0;c++; } } b=0;c++; } } void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr) { OLED_Set_Pos(x,y); for(char a=0;a<8;a++) { OLED_WrDat(asc2_1608[chr-' '][(a*2)]); } OLED_Set_Pos(x,y+1); for(char a=0;a<8;a++) { OLED_WrDat(asc2_1608[chr-' '][(a*2)+1]); } } void OLED_ShowStr(unsigned char x,unsigned char y,char *str) { while(*str!='\0') { if(x>=X_WIDTH) { y+=2; if(y>=Y_WIDTH_) { y=0; } x=0; } OLED_ShowChar(x,y,*str); str++; x+=8; } }