STM32的HAL库实现OLED显示
oled.c
1 #include "i2c.h" 2 #include "string.h" 3 4 #include "oled.h" 5 #include "oledfont.h" 6 7 unsigned int HZ=0; 8 //返回GB16字库里汉字个数 9 unsigned int GB16_NUM(void) 10 { 11 unsigned int HZ_NUM; 12 unsigned char *PT; 13 PT = hz_index; 14 while(*PT++ != '\0') 15 { 16 HZ_NUM++; 17 } 18 19 return HZ_NUM/2; 20 } 21 22 23 //初始化命令 24 uint8_t CMD_Data[]={ 25 0xAE, 0x00, 0x10, 0x40, 0xB0, 0x81, 0xFF, 0xA1, 0xA6, 0xA8, 0x3F, 26 27 0xC8, 0xD3, 0x00, 0xD5, 0x80, 0xD8, 0x05, 0xD9, 0xF1, 0xDA, 0x12, 28 29 0xD8, 0x30, 0x8D, 0x14, 0xAF}; 30 void WriteCmd() 31 { 32 uint8_t i = 0; 33 for(i=0; i<27; i++){ 34 HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,CMD_Data+i,1,0x100); 35 } 36 } 37 //向设备写控制命令 38 void OLED_WR_CMD(uint8_t cmd) 39 { 40 HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100); 41 } 42 //向设备写数据 43 void OLED_WR_DATA(uint8_t data) 44 { 45 HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&data,1,0x100); 46 } 47 48 //OLED全屏填充 49 void OLED_Fill(uint8_t fill_Data) 50 { 51 uint8_t m,n; 52 for(m=0;m<8;m++) 53 { 54 OLED_WR_CMD(0xb0+m); //page0-page1 55 OLED_WR_CMD(0x00); //low column start address 56 OLED_WR_CMD(0x10); //high column start address 57 for(n=0;n<130;n++) 58 { 59 OLED_WR_DATA(fill_Data); 60 } 61 } 62 } 63 //设置起始点坐标 64 void OLED_Set_Pos(u8 x, u8 y) 65 { 66 OLED_WR_CMD(0xb0+y); 67 OLED_WR_CMD((((x+2)&0xf0)>>4)|0x10); 68 OLED_WR_CMD(((x+2)&0x0f)|0x01); 69 } 70 71 //将OLED从休眠中唤醒 72 void OLED_ON(void) 73 { 74 OLED_WR_CMD(0X8D); //设置电荷泵 75 OLED_WR_CMD(0X14); //开启电荷泵 76 OLED_WR_CMD(0XAF); //OLED唤醒 77 } 78 79 //让OLED休眠 -- 休眠模式下,OLED功耗不到10uA 80 void OLED_OFF(void) 81 { 82 OLED_WR_CMD(0X8D); //设置电荷泵 83 OLED_WR_CMD(0X10); //关闭电荷泵 84 OLED_WR_CMD(0XAE); //OLED休眠 85 } 86 87 //清屏 88 void OLED_Clear(void) 89 { 90 OLED_Fill(0x00); 91 } 92 93 //在指定位置显示一个字符,包括部分字符 94 //x:0~127 95 //y:0~63 96 void OLED_ShowChar(u8 x, u8 y, u8 chr) 97 { 98 u8 c = 0, i = 0; 99 c = chr - ' '; //得到偏移后的值 100 if(x > 130 - 1) 101 { 102 x = 0; 103 y = y + 2; 104 } 105 OLED_Set_Pos(x, y); 106 for(i = 0; i < 8; i++) 107 OLED_WR_DATA(zf[c * 16 + i]); 108 OLED_Set_Pos(x, y + 1); 109 for(i = 0; i < 8; i++) 110 OLED_WR_DATA(zf[c * 16 + i + 8]); 111 112 } 113 //m^n函数 114 u32 oled_pow(u8 m, u8 n) 115 { 116 u32 result = 1; 117 while(n--)result *= m; 118 return result; 119 } 120 //显示个数字 121 //x,y :起点坐标 122 //len :数字的位数 123 //num:数值(0~4294967295); 124 //mode: 为1:当要显示的为0时显示0 为0:当要显示的为0时显示空格 125 void OLED_ShowNum(u8 x, u8 y, u32 num, u8 len,u8 mode) 126 { 127 u8 t, temp; 128 u8 enshow=0; 129 for(t = 0; t < len; t++) 130 { 131 temp = (num / oled_pow(10, len - t - 1)) % 10; 132 if(enshow==0&&t<(len-1)) 133 { 134 if(temp==0) 135 { 136 if(mode) 137 OLED_ShowChar(x + 8*t, y, '0'); 138 else 139 OLED_ShowChar(x + 8*t, y, ' '); 140 continue; 141 }else enshow=1; 142 } 143 OLED_ShowChar(x + 8*t, y, temp + '0'); 144 } 145 } 146 //显示中英文字符 147 void OLED_ShowCH(u8 x, u8 y,u8 *chs) 148 { 149 u32 i=0; 150 u32 j; 151 char* m; 152 while (*chs != '\0') 153 { 154 if (*chs > 0xa0) //汉字内码都是大于0xa0 155 { 156 for (i=0 ;i < HZ;i++) 157 { 158 if(x>112) 159 { 160 x=0; 161 y=y+2; 162 } 163 if ((*chs == hz_index[i*2]) && (*(chs+1) == hz_index[i*2+1])) 164 { 165 OLED_Set_Pos(x, y); 166 for(j=0;j<16;j++) 167 OLED_WR_DATA(hz[i*32+j]); 168 OLED_Set_Pos(x,y+1); 169 for(j=0;j<16;j++) 170 OLED_WR_DATA(hz[i*32+j+16]); 171 x +=16; 172 break; 173 } 174 } 175 chs+=2; 176 } 177 else 178 { 179 if(x>122) 180 { 181 x=0; 182 y=y+2; 183 } 184 m=strchr(zf_index,*chs); 185 if (m!=NULL) 186 { 187 OLED_Set_Pos(x, y); 188 for(j = 0; j < 8; j++) 189 OLED_WR_DATA(zf[((u8)*m-' ') * 16 + j]); 190 OLED_Set_Pos(x, y + 1); 191 for(j = 0; j < 8; j++) 192 OLED_WR_DATA(zf[((u8)*m-' ') * 16 + j + 8]); 193 x += 8; 194 } 195 chs++; 196 } 197 } 198 } 199 200 //初始化SSD1306 201 void OLED_Init(void) 202 { 203 HZ= GB16_NUM(); 204 HAL_Delay(200); 205 206 WriteCmd(); 207 } 208 209 // x0,y0 -- 起始点坐标(x0:0~127, y0:0~7); x1,y1 -- 起点对角线(结束点)的坐标(x1:1~128,y1:1~8) 210 void OLED_DrawBMP(u8 x0,u8 y0,u8 x1,u8 y1,u8 BMP[]) 211 { 212 u16 j=0; 213 u8 x,y; 214 215 if(y1%8==0) 216 y = y1/8; 217 else 218 y = y1/8 + 1; 219 for(y=y0;y<y1;y++) 220 { 221 OLED_Set_Pos(x0,y); 222 for(x=x0;x<x1;x++) 223 { 224 OLED_WR_DATA(BMP[j++]); 225 } 226 } 227 }
oledfont.h
1 #ifndef __OLEDFONT_H 2 #define __OLEDFONT_H 3 4 /***************************16*16的点阵字体取模方式:共阴——列行式——逆向输出*********/ 5 6 /****************************************8*16的点阵************************************/ 7 unsigned char zf[] = 8 { 9 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0 10 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, //! 1 11 0x00, 0x10, 0x0C, 0x06, 0x10, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //" 2 12 0x40, 0xC0, 0x78, 0x40, 0xC0, 0x78, 0x40, 0x00, 0x04, 0x3F, 0x04, 0x04, 0x3F, 0x04, 0x04, 0x00, //# 3 13 0x00, 0x70, 0x88, 0xFC, 0x08, 0x30, 0x00, 0x00, 0x00, 0x18, 0x20, 0xFF, 0x21, 0x1E, 0x00, 0x00, //$ 4 14 0xF0, 0x08, 0xF0, 0x00, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x21, 0x1C, 0x03, 0x1E, 0x21, 0x1E, 0x00, //% 5 15 0x00, 0xF0, 0x08, 0x88, 0x70, 0x00, 0x00, 0x00, 0x1E, 0x21, 0x23, 0x24, 0x19, 0x27, 0x21, 0x10, //& 6 16 0x10, 0x16, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //' 7 17 0x00, 0x00, 0x00, 0xE0, 0x18, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x07, 0x18, 0x20, 0x40, 0x00, //( 8 18 0x00, 0x02, 0x04, 0x18, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x18, 0x07, 0x00, 0x00, 0x00, //) 9 19 0x40, 0x40, 0x80, 0xF0, 0x80, 0x40, 0x40, 0x00, 0x02, 0x02, 0x01, 0x0F, 0x01, 0x02, 0x02, 0x00, //* 10 20 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x1F, 0x01, 0x01, 0x01, 0x00, //+ 11 21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xB0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, //, 12 22 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, //- 13 23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, //. 14 24 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x18, 0x04, 0x00, 0x60, 0x18, 0x06, 0x01, 0x00, 0x00, 0x00, /// 15 25 0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x10, 0x0F, 0x00, //0 16 26 0x00, 0x10, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //1 17 27 0x00, 0x70, 0x08, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x30, 0x28, 0x24, 0x22, 0x21, 0x30, 0x00, //2 18 28 0x00, 0x30, 0x08, 0x88, 0x88, 0x48, 0x30, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, //3 19 29 0x00, 0x00, 0xC0, 0x20, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x07, 0x04, 0x24, 0x24, 0x3F, 0x24, 0x00, //4 20 30 0x00, 0xF8, 0x08, 0x88, 0x88, 0x08, 0x08, 0x00, 0x00, 0x19, 0x21, 0x20, 0x20, 0x11, 0x0E, 0x00, //5 21 31 0x00, 0xE0, 0x10, 0x88, 0x88, 0x18, 0x00, 0x00, 0x00, 0x0F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, //6 22 32 0x00, 0x38, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, //7 23 33 0x00, 0x70, 0x88, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x1C, 0x22, 0x21, 0x21, 0x22, 0x1C, 0x00, //8 24 34 0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x00, 0x31, 0x22, 0x22, 0x11, 0x0F, 0x00, //9 25 35 0x00, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, //: 26 36 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x00, 0x00, 0x00, 0x00, //; 27 37 0x00, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, //< 28 38 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, //= 29 39 0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, //> 30 40 0x00, 0x70, 0x48, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x36, 0x01, 0x00, 0x00, //? 31 41 0xC0, 0x30, 0xC8, 0x28, 0xE8, 0x10, 0xE0, 0x00, 0x07, 0x18, 0x27, 0x24, 0x23, 0x14, 0x0B, 0x00, //@ 32 42 0x00, 0x00, 0xC0, 0x38, 0xE0, 0x00, 0x00, 0x00, 0x20, 0x3C, 0x23, 0x02, 0x02, 0x27, 0x38, 0x20, //A 33 43 0x08, 0xF8, 0x88, 0x88, 0x88, 0x70, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, //B 34 44 0xC0, 0x30, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x07, 0x18, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, //C 35 45 0x08, 0xF8, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, //D 36 46 0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x23, 0x20, 0x18, 0x00, //E 37 47 0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00, //F 38 48 0xC0, 0x30, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x07, 0x18, 0x20, 0x20, 0x22, 0x1E, 0x02, 0x00, //G 39 49 0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x21, 0x3F, 0x20, //H 40 50 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //I 41 51 0x00, 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, 0x00, //J 42 52 0x08, 0xF8, 0x88, 0xC0, 0x28, 0x18, 0x08, 0x00, 0x20, 0x3F, 0x20, 0x01, 0x26, 0x38, 0x20, 0x00, //K 43 53 0x08, 0xF8, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x30, 0x00, //L 44 54 0x08, 0xF8, 0xF8, 0x00, 0xF8, 0xF8, 0x08, 0x00, 0x20, 0x3F, 0x00, 0x3F, 0x00, 0x3F, 0x20, 0x00, //M 45 55 0x08, 0xF8, 0x30, 0xC0, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x20, 0x00, 0x07, 0x18, 0x3F, 0x00, //N 46 56 0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, //O 47 57 0x08, 0xF8, 0x08, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x01, 0x00, 0x00, //P 48 58 0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x18, 0x24, 0x24, 0x38, 0x50, 0x4F, 0x00, //Q 49 59 0x08, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x0C, 0x30, 0x20, //R 50 60 0x00, 0x70, 0x88, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x38, 0x20, 0x21, 0x21, 0x22, 0x1C, 0x00, //S 51 61 0x18, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x18, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, //T 52 62 0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, //U 53 63 0x08, 0x78, 0x88, 0x00, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x07, 0x38, 0x0E, 0x01, 0x00, 0x00, //V 54 64 0xF8, 0x08, 0x00, 0xF8, 0x00, 0x08, 0xF8, 0x00, 0x03, 0x3C, 0x07, 0x00, 0x07, 0x3C, 0x03, 0x00, //W 55 65 0x08, 0x18, 0x68, 0x80, 0x80, 0x68, 0x18, 0x08, 0x20, 0x30, 0x2C, 0x03, 0x03, 0x2C, 0x30, 0x20, //X 56 66 0x08, 0x38, 0xC8, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, //Y 57 67 0x10, 0x08, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x20, 0x38, 0x26, 0x21, 0x20, 0x20, 0x18, 0x00, //Z 58 68 0x00, 0x00, 0x00, 0xFE, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x40, 0x40, 0x40, 0x00, //[ 59 69 0x00, 0x0C, 0x30, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x38, 0xC0, 0x00, //\\ 60 70 0x00, 0x02, 0x02, 0x02, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7F, 0x00, 0x00, 0x00, //] 61 71 0x00, 0x00, 0x04, 0x02, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //^ 62 72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //_ 63 73 0x00, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //` 64 74 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x19, 0x24, 0x22, 0x22, 0x22, 0x3F, 0x20, //a 65 75 0x08, 0xF8, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, //b 66 76 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x20, 0x11, 0x00, //c 67 77 0x00, 0x00, 0x00, 0x80, 0x80, 0x88, 0xF8, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x10, 0x3F, 0x20, //d 68 78 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x22, 0x22, 0x22, 0x22, 0x13, 0x00, //e 69 79 0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0x18, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //f 70 80 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x6B, 0x94, 0x94, 0x94, 0x93, 0x60, 0x00, //g 71 81 0x08, 0xF8, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, //h 72 82 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //i 73 83 0x00, 0x00, 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, //j 74 84 0x08, 0xF8, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x24, 0x02, 0x2D, 0x30, 0x20, 0x00, //k 75 85 0x00, 0x08, 0x08, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //l 76 86 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x3F, 0x20, 0x00, 0x3F, //m 77 87 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, //n 78 88 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, //o 79 89 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xA1, 0x20, 0x20, 0x11, 0x0E, 0x00, //p 80 90 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0xA0, 0xFF, 0x80, //q 81 91 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x20, 0x3F, 0x21, 0x20, 0x00, 0x01, 0x00, //r 82 92 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x33, 0x24, 0x24, 0x24, 0x24, 0x19, 0x00, //s 83 93 0x00, 0x80, 0x80, 0xE0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x00, 0x00, //t 84 94 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x10, 0x3F, 0x20, //u 85 95 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x01, 0x0E, 0x30, 0x08, 0x06, 0x01, 0x00, //v 86 96 0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x0F, 0x30, 0x0C, 0x03, 0x0C, 0x30, 0x0F, 0x00, //w 87 97 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x31, 0x2E, 0x0E, 0x31, 0x20, 0x00, //x 88 98 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x81, 0x8E, 0x70, 0x18, 0x06, 0x01, 0x00, //y 89 99 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x21, 0x30, 0x2C, 0x22, 0x21, 0x30, 0x00, //z 90 100 0x00, 0x00, 0x00, 0x00, 0x80, 0x7C, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x40, 0x40, //{ 91 101 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, //| 92 102 0x00, 0x02, 0x02, 0x7C, 0x80, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00, //} 93 103 0x00, 0x02, 0x01, 0x02, 0x02, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //~ 94 104 105 }; 106 107 /***************************16*16的点阵字体取模方式:共阴——列行式——逆向输出*********/ 108 unsigned char hz[] = 109 { 110 0x00, 0x02, 0x02, 0xC2, 0x02, 0x02, 0x02, 0xFE, 0x82, 0x82, 0x82, 0x82, 0x82, 0x02, 0x00, 0x00, 111 0x40, 0x40, 0x40, 0x7F, 0x40, 0x40, 0x40, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, /*"正",0*/ 112 0x08, 0x08, 0x88, 0xC8, 0x38, 0x0C, 0x0B, 0x08, 0x08, 0xE8, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 113 0x02, 0x01, 0x00, 0xFF, 0x40, 0x41, 0x41, 0x41, 0x41, 0x7F, 0x41, 0x41, 0x41, 0x41, 0x40, 0x00, /*"在",1*/ 114 0x10, 0x10, 0xD0, 0xFF, 0x90, 0x50, 0x20, 0x50, 0x4C, 0x43, 0x4C, 0x50, 0x20, 0x40, 0x40, 0x00, 115 0x04, 0x03, 0x00, 0xFF, 0x00, 0x41, 0x44, 0x58, 0x41, 0x4E, 0x60, 0x58, 0x47, 0x40, 0x40, 0x00, /*"检",2*/ 116 0x10, 0x60, 0x02, 0x8C, 0x00, 0xFE, 0x02, 0xF2, 0x02, 0xFE, 0x00, 0xF8, 0x00, 0xFF, 0x00, 0x00, 117 0x04, 0x04, 0x7E, 0x01, 0x80, 0x47, 0x30, 0x0F, 0x10, 0x27, 0x00, 0x47, 0x80, 0x7F, 0x00, 0x00, /*"测",3*/ 118 0x00, 0x00, 0xF0, 0x10, 0x10, 0x10, 0x10, 0xFF, 0x10, 0x10, 0x10, 0x10, 0xF0, 0x00, 0x00, 0x00, 119 0x00, 0x00, 0x0F, 0x04, 0x04, 0x04, 0x04, 0xFF, 0x04, 0x04, 0x04, 0x04, 0x0F, 0x00, 0x00, 0x00, /*"中",4*/ 120 0x10, 0x60, 0x02, 0x8C, 0x00, 0xFE, 0x02, 0xF2, 0x02, 0xFE, 0x00, 0xF8, 0x00, 0xFF, 0x00, 0x00, 121 0x04, 0x04, 0x7E, 0x01, 0x80, 0x47, 0x30, 0x0F, 0x10, 0x27, 0x00, 0x47, 0x80, 0x7F, 0x00, 0x00, /*"测",5*/ 122 0x40, 0x40, 0x42, 0xCC, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0xFF, 0x10, 0x11, 0x16, 0x10, 0x00, 123 0x00, 0x00, 0x00, 0x3F, 0x10, 0x28, 0x60, 0x3F, 0x10, 0x10, 0x01, 0x0E, 0x30, 0x40, 0xF0, 0x00, /*"试",6*/ 124 0x24, 0x24, 0xA4, 0xFE, 0x23, 0x22, 0x00, 0x3E, 0x22, 0x22, 0x22, 0x22, 0x22, 0x3E, 0x00, 0x00, 125 0x08, 0x06, 0x01, 0xFF, 0x01, 0x06, 0x40, 0x49, 0x49, 0x49, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00, /*"程",7*/ 126 0x00, 0x00, 0xFC, 0x04, 0x04, 0x04, 0x14, 0x15, 0x56, 0x94, 0x54, 0x34, 0x14, 0x04, 0x04, 0x00, 127 0x40, 0x30, 0x0F, 0x00, 0x01, 0x01, 0x01, 0x41, 0x81, 0x7F, 0x01, 0x01, 0x01, 0x05, 0x03, 0x00, /*"序",8*/ 128 129 }; 130 131 //注意:下面字库声明的汉字顺序必须和取字模的顺序相同,且不能有相同的字 132 unsigned char hz_index[] = {"正在检测中测试程序"}; 133 char zf_index[] = {" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"}; 134 135 #endif
oled.h
1 #ifndef OLED_H__ 2 #define OLED_H__ 3 4 #include "stm32l4xx_hal.h" 5 #include "stm32l4xx.h" 6 7 void WriteCmd(void); 8 void OLED_WR_CMD(uint8_t cmd); 9 void OLED_WR_DATA(uint8_t data); 10 11 void OLED_Init(void); 12 void OLED_Clear(void); 13 void OLED_ON(void); 14 void OLED_OFF(void); 15 16 void OLED_Set_Pos(uint8_t x, uint8_t y) ; 17 unsigned int oled_pow(uint8_t m,uint8_t n); 18 void OLED_Fill(uint8_t fill_Data); 19 void OLED_ShowNum(uint8_t x,uint8_t y,unsigned int num,uint8_t len,uint8_t size2); 20 void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr); 21 void OLED_ShowCH(uint8_t x, uint8_t y,uint8_t *chs); 22 void OLED_DrawBMP(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t BMP[]); 23 #endif
mian.c
1 #include "oled.h" 2 3 4 OLED_Init(); 5 OLED_Clear(); 6 7 OLED_Clear();//清屏 8 OLED_ShowCH(12,0,"SGP30测试程序");//显示 9 OLED_ShowCH(18,3,"TVOC:"); 10 OLED_ShowCH(92,3,"ppd"); 11 OLED_ShowCH(18,6,"CO2:"); 12 OLED_ShowCH(84,6,"ppm");