早就聽說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() ;