oled屏幕模块似乎是厂家提供的
也许可以根据屏幕驱动芯片去写
根据现在了解的芯片一般有两个:SH1106和SSD1306
不过这次我们用的是SSD1306芯片驱动的屏幕
下面是从裸屏到模块的pcb:
有上面的图可以看出,虽然是双色,但是只是底子不同罢了,其实还是单色的.
下面是51单片机的程序:
下面是初始化程序:初始化函数里面都是一些发送指令和数据的.
发送指令和数据的函数如下:
发送指令和数据的函数:
void OLED_WrCmd(UCHAR8 ucCmd) //发送指令 { UCHAR8 i = 8;OLED_DC = 0; OLED_SCL = 0; while (i--) { if (ucCmd & 0x80) { OLED_SDA = 1; } else { OLED_SDA = 0; } OLED_SCL = 1; _nop_(); OLED_SCL = 0; ucCmd <<= 1; } return; }
初始化:
void OLED_Init() { UCHAR8 i; OLED_SCL = 1; OLED_RST = 0; for(i = 0; i < 100; i++) { _nop_(); //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕 } OLED_RST = 1; SetDisplayOnOff(0x00); // Display Off (0x00/0x01) SetDisplayClock(0x80); // Set Clock as 100 Frames/Sec SetMultiplexRatio(0x3F); // 1/64 Duty (0x0F~0x3F) SetDisplayOffset(0x00); // Shift Mapping RAM Counter (0x00~0x3F) SetStartLine(0x00); // Set Mapping RAM Display Start Line (0x00~0x3F) SetChargePump(0x04); // Enable Embedded DC/DC Converter (0x00/0x04) SetAddressingMode(0x02); // Set Page Addressing Mode (0x00/0x01/0x02) SetSegmentRemap(0x01); // Set SEG/Column Mapping 0x00左右反置 0x01正常 SetCommonRemap(0x08); // Set COM/Row Scan Direction 0x00上下反置 0x08正常 SetCommonConfig(0x10); // Set Sequential Configuration (0x00/0x10) SetContrastControl(0xcf); // Set SEG Output Current SetPrechargePeriod(0xF1); // Set Pre-Charge as 15 Clocks & Discharge as 1 Clock SetVCOMH(0x40); // Set VCOM Deselect Level SetEntireDisplay(0x00); // Disable Entire Display On (0x00/0x01) SetInverseDisplay(0x00); // Disable Inverse Display On (0x00/0x01) SetDisplayOnOff(0x01); // Display On (0x00/0x01) OLED_Fill(0x00); // 初始清屏 //这句话还是可以理解,但是上面的一堆又是什么呢 OLED_SetPos(0,0); return; }
初始化里面的子函数如下所示:
void SetDisplayOnOff(UCHAR8 ucData) { OLED_WrCmd(0xAE|ucData); // Set Display On/Off // Default => 0xAE // 0xAE (0x00) => Display Off // 0xAF (0x01) => Display On } void SetDisplayClock(UCHAR8 ucData) { OLED_WrCmd(0xD5); // Set Display Clock Divide Ratio / Oscillator Frequency OLED_WrCmd(ucData); // Default => 0x80 //D[3:0] => Display Clock Divider // D[7:4] => Oscillator Frequency } void SetMultiplexRatio(UCHAR8 ucData) { OLED_WrCmd(0xA8); // Set Multiplex Ratio OLED_WrCmd(ucData); // Default => 0x3F (1/64 Duty) } void SetDisplayOffset(UCHAR8 ucData) { OLED_WrCmd(0xD3); // Set Display Offset OLED_WrCmd(ucData); // Default => 0x00 } void SetStartLine(UCHAR8 ucData) { OLED_WrCmd(0x40|ucData); // Set Display Start Line // Default => 0x40 (0x00) } void SetChargePump(UCHAR8 ucData) { OLED_WrCmd(0x8D); // Set Charge Pump OLED_WrCmd(0x10|ucData); // Default => 0x10 // 0x10 (0x00) => Disable Charge Pump // 0x14 (0x04) => Enable Charge Pump } void SetAddressingMode(UCHAR8 ucData) { OLED_WrCmd(0x20); // Set Memory Addressing Mode OLED_WrCmd(ucData); // Default => 0x02 // 0x00 => Horizontal Addressing Mode // 0x01 => Vertical Addressing Mode // 0x02 => Page Addressing Mode } void SetSegmentRemap(UCHAR8 ucData) { OLED_WrCmd(0xA0|ucData); // Set Segment Re-Map // Default => 0xA0 // 0xA0 (0x00) => Column Address 0 Mapped to SEG0 // 0xA1 (0x01) => Column Address 0 Mapped to SEG127 } void SetCommonRemap(UCHAR8 ucData) { OLED_WrCmd(0xC0|ucData); // Set COM Output Scan Direction // Default => 0xC0 // 0xC0 (0x00) => Scan from COM0 to 63 // 0xC8 (0x08) => Scan from COM63 to 0 } void SetCommonConfig(UCHAR8 ucData) { OLED_WrCmd(0xDA); // Set COM Pins Hardware Configuration OLED_WrCmd(0x02|ucData); // Default => 0x12 (0x10) // Alternative COM Pin Configuration // Disable COM Left/Right Re-Map } void SetContrastControl(UCHAR8 ucData) { OLED_WrCmd(0x81); // SetContrast Control OLED_WrCmd(ucData); // Default => 0x7F } void SetPrechargePeriod(UCHAR8 ucData) { OLED_WrCmd(0xD9); // Set Pre-Charge Period OLED_WrCmd(ucData); // Default => 0x22 (2 Display Clocks [Phase 2] / 2 Display Clocks [Phase 1]) // D[3:0] => Phase 1 Period in 1~15 Display Clocks // D[7:4] => Phase 2 Period in 1~15 Display Clocks } void SetVCOMH(UCHAR8 ucData) { OLED_WrCmd(0xDB); // Set VCOMH Deselect Level OLED_WrCmd(ucData); // Default => 0x20 (0.77*VCC) } void SetEntireDisplay(UCHAR8 ucData) { OLED_WrCmd(0xA4|ucData); // Set Entire Display On / Off // Default => 0xA4 // 0xA4 (0x00) => Normal Display // 0xA5 (0x01) => Entire Display On } void SetInverseDisplay(UCHAR8 ucData) { OLED_WrCmd(0xA6|ucData); // Set Inverse Display On/Off // Default => 0xA6 // 0xA6 (0x00) => Normal Display // 0xA7 (0x01) => Inverse Display On } void SetDisplayOnOff(UCHAR8 ucData) { OLED_WrCmd(0xAE|ucData); // Set Display On/Off // Default => 0xAE // 0xAE (0x00) => Display Off // 0xAF (0x01) => Display On } void OLED_Fill(UCHAR8 ucData) { UCHAR8 ucPage,ucColumn; for(ucPage = 0; ucPage < 8; ucPage++) { OLED_WrCmd(0xb0 + ucPage); //0xb0+0~7表示页0~7 8*8=64 OLED_WrCmd(0x00); //0x00+0~16表示将128列分成16组其地址在某组中的第几列 16*8=128 OLED_WrCmd(0x10); //0x10+0~16表示将128列分成16组其地址所在第几组 for(ucColumn = 0; ucColumn < 128; ucColumn++) { OLED_WrDat(ucData); } } } void OLED_SetPos(UCHAR8 ucIdxX, UCHAR8 ucIdxY) //坐标指定. { OLED_WrCmd(0xb0 + ucIdxY); OLED_WrCmd(((ucIdxX & 0xf0) >> 4) | 0x10); //在第几组 OLED_WrCmd((ucIdxX & 0x0f) | 0x00); //在某一组的,第几列 }
然后:让屏幕显示,年月日的程序:
//OLED显示日期函数 void DAY_OLED_Display() { year=TableDs1302[6]; //GPS或时钟芯片读取时间放在此变量中 mooth=TableDs1302[4]; //GPS或时钟芯片读取时间放在此变量中 day=TableDs1302[3]; //GPS或时钟芯片读取时间放在此变量中 OLED_P8x16char(0, 5, 2+'0'); //下面这两行显示:20 OLED_P8x16char(8, 5, 0+'0'); OLED_P8x16char(16, 5, year/10+'0'); OLED_P8x16char(24, 5, year%10+'0');//显示20**年 OLED_P8x16char(32, 5, '-'); //分隔标记 OLED_P8x16char(40, 5, mooth/10+'0'); OLED_P8x16char(48, 5, mooth%10+'0');//显示月 OLED_P8x16char(56, 5, '-'); // OLED_P8x16char(64, 5, day/10+'0');//显示日期 OLED_P8x16char(72, 5, day%10+'0'); }