SSD1306 OLED 1.3" IIC 驱动 带字库 中英文混合显示
早就听说OLED显示效果比较好,也不需要背光,使用起来接线也很方便,于是TB上买了一个1.3Inch的OLED,蓝色的字,显示效果确实非常好,下边就分享一下驱动过程
我在STM32F103C8T6这块单片机上调试的,硬件上我的屏幕是IIC接口的,没有用硬件I2C 而是模拟的,PB4->SCL PB6->SDA ,屏幕地址0x78,字库用的M25Q16 2MB字节
字库包含ASCII码,中文ASCII码,所有标点符号和GB2312汉字,字库容量263762Byte,就制作了16X16的字库,其它的没加,Flash空间足够,如果需要可以自行添加
字库的可以随时更新,通过串口1 Ymodem协议进行更新,下边会说明字库如何制件的
驱动上在RAM里开1024Byte有缓存,所有操作都是对这块内存区域进行操作,然后一次更新到屏幕上去
本驱动可以画点,画线,画圆,画矩形,画三角形,填充圆形,填充矩形等,
效果图:
void I2C_WriteByte(uint8_t addr,uint8_t data) { IIC1_Start(); IIC1_Send_Byte(0x78); IIC1_Wait_Ack(); IIC1_Send_Byte(addr); IIC1_Wait_Ack(); IIC1_Send_Byte(data); IIC1_Wait_Ack(); IIC1_Stop(); //delay_ms(10); } void I2C_WriteMutliByte(uint8_t addr,uint8_t* data,uint16_t count) { u8 i=0; IIC1_Start(); IIC1_Send_Byte(0x78); IIC1_Wait_Ack(); IIC1_Send_Byte(addr); IIC1_Wait_Ack(); for (i = 0; i < count; i++) { IIC1_Send_Byte(data[i]); IIC1_Wait_Ack(); } IIC1_Stop(); }
//更新屏幕 void SSD1306_UpdateScreen(void) { uint8_t m; for (m = 0; m < 8; m++) { WriteCmd(0xB0 + m); WriteCmd(0x02);//这里的起始地址是0x02,如果用SSD1306地址是0x00 WriteCmd(0x10); /* Write multi data */ I2C_WriteMutliByte(0x40,&SSD1306_Buffer[SSD1306_WIDTH * m], SSD1306_WIDTH); } }
下边是从Flash里读取字库并写入缓冲区
void SSD1306_ShowChineseString(uint8_t x, uint8_t y,uint8_t *str) { uint8_t index_char,index_word; unsigned char High8bit,Low8bit; index_char = 0; index_word = 0; while(*str!=0) { High8bit=*str; if(High8bit&0x80) {str++;Low8bit=*str;} if(High8bit&0x80 ) { SPI_Flash_Read(&SSD1306_Buffer[(x+index_word)*16 + (x+index_char)*8 + 2*y*128],((High8bit-0xa1)*94+Low8bit-0xa1)*32,16); SPI_Flash_Read(&SSD1306_Buffer[(x+index_word)*16 + (x+index_char)*8 + (2*y+1)*128],((High8bit-0xa1)*94+Low8bit-0xa1)*32 + 16,16); //SPI_Flash_Read(SSD1306_Buffer,((High8bit-0xa1)*94+Low8bit-0xa1)*32,32); index_word++; } else { SPI_Flash_Read(&SSD1306_Buffer[(x+index_char)*8 + (x+index_word)*16 + 2*y*128],(High8bit)*16 + 0x3fe50,8); //0x0fe50 ÕâλÖúóÃæÌí¼ÓÁËASCIIÂë SPI_Flash_Read(&SSD1306_Buffer[(x+index_char)*8 + (x+index_word)*16 + (2*y+1)*128],(High8bit)*16 + 8 + 0x3fe50,8); index_char++; } str++; } }
驱动使用方法
SSD1306_ShowString(2,2,"Hello Word",1); SSD1306_ShowString(3,3,"Hello Word",1); SSD1306_ShowString(0,4,"Hello Word!",2); //SSD1306_ToggleInvert(); SSD1306_DrawCircle(32,32,32,0x01); SSD1306_DrawRectangle(0,0,128,64,0x01); SSD1306_DrawTriangle(80,32,120,56,120,0,0x01); SSD1306_UpdateScreen() ; delay_ms(5000); SSD1306_CLS();// SSD1306_ShowChineseString(0,0,"Hello弄啥类HaHa"); SSD1306_ShowChineseString(0,1,"弄啥类");
SSD1306_ShowChineseString(0,2,"Hello Word!"); SSD1306_UpdateScreen() ;