OLED SH1106通过I2C显示

预备知识

1. I2C基本原理

2. SH1106通过I2C读写

OLED初始化

#define OLED_CMD  0#define OLED_DATA 1/**********************************************// IIC Write Command**********************************************/void Write_IIC_Command(unsigned char IIC_Command)//只提供伪代码,具体实现依赖于HW{   IIC_Start();   Write_IIC_Byte(0x78);            //Slave address,具体看OLED pin脚设置IIC_Wait_Ack();   Write_IIC_Byte(0x00);//write commandIIC_Wait_Ack();   Write_IIC_Byte(IIC_Command); IIC_Wait_Ack();   IIC_Stop();}/**********************************************// IIC Write Data**********************************************/void Write_IIC_Data(unsigned char IIC_Data){   IIC_Start();   Write_IIC_Byte(0x78);//IIC_Wait_Ack();   Write_IIC_Byte(0x40);//write dataIIC_Wait_Ack();   Write_IIC_Byte(IIC_Data);IIC_Wait_Ack();   IIC_Stop();}void OLED_WR_Byte(unsigned dat,unsigned cmd){if(cmd){Write_IIC_Data(dat);}else{Write_IIC_Command(dat);}}void OLED_Set_Pos(unsigned char x, unsigned char y){OLED_WR_Byte(0xb0 y,OLED_CMD);OLED_WR_Byte((((x 2)&0xf0)>>4)|0x10,OLED_CMD);OLED_WR_Byte(((x 2)&0x0f),OLED_CMD);}void OLED_Clear(void){U8 i,n;for(i=0;i<8;i  ){OLED_WR_Byte (0xb0 i,OLED_CMD);    //设置页地址(0~7)OLED_WR_Byte (0x02,OLED_CMD);      //设置显示位置—列低地址OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址for(n=0;n<128;n  )OLED_WR_Byte(0,OLED_DATA);} //更新显示}void OLED_All(void){U8 i,n;for(i=0;i<8;i  ){OLED_WR_Byte (0xb0 i,OLED_CMD);OLED_WR_Byte (0x02,OLED_CMD);OLED_WR_Byte (0x10,OLED_CMD);for(n=0;n<128;n  )OLED_WR_Byte(0xff,OLED_DATA);}}void OLED_Init(void){OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panelOLED_WR_Byte(0x02,OLED_CMD);//---set low column addressOLED_WR_Byte(0x10,OLED_CMD);//---set high column addressOLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control registerOLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current BrightnessOLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常     //????OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常    ///????OLED_WR_Byte(0xA6,OLED_CMD);//--set normal displayOLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 dutyOLED_WR_Byte(0xD3,OLED_CMD);//-set display offsetShift Mapping RAM Counter (0x00~0x3F)OLED_WR_Byte(0x00,OLED_CMD);//-not offsetOLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequencyOLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/SecOLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge periodOLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 ClockOLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configurationOLED_WR_Byte(0x12,OLED_CMD);OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomhOLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect LevelOLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panelOLED_Clear();OLED_Set_Pos(0,0);}

字符/字符串显示

/************************************6*8的点阵************************************/const unsigned char F6x8[][6] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,//  0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 00x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 10x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 20x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 30x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 40x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 50x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 60x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 70x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 80x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 90x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 550x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines};void OLED_ShowChar(U8 x,U8 y,U8 chr){unsigned char c=0,i=0;c=chr-' ';//得到偏移后的值if(x>128-1){x=0;y=y 2;}{OLED_Set_Pos(x,y 1);for(i=0;i<6;i  )OLED_WR_Byte(F6x8[c][i],OLED_DATA);}}void OLED_ShowString(U8 x,U8 y,U8 *chr){unsigned char j=0;while (chr[j]!='\0'){OLED_ShowChar(x,y,chr[j]);x =8;if(x>120){x=0;y =2;}j  ;}}

显示示例

void main(){    //init settingOLED_Init();OLED_All();OLED_Clear();OLED_ShowChar( 1, 1, '1');}

参考代码:https://download.csdn.net/download/qq_36243592/15893303?spm=1001.2014.3001.5501

来源:https://www.icode9.com/content-4-899751.html

(0)

相关推荐