Arduino i2c协议 驱动ssd1306 OLED

       写程序,本蛙是很弱的,c语言确实没好好学,单片机也没有玩的转的。Arduino的高度资源整合倒是为做东西降低了很多门槛,我也受(shen)益(shou)匪(qi)浅(hai)。

      用Arduino驱动屏幕应该很多人都知道u8glib这个库,确实,这库很强大,兼容性强,功能丰富,就是现在的我还没能好好的看懂它。那么,根据个人需求,还是我自己来写下驱动吧。

下面是针对小蛙电源板(还未问世)按需写的显示程序,为方便使用就没打包成库(其实是我不会),除了默认自带的库外仅调用了I2C的库文件。其它功能都在主函数里,名称命名格式很不规范,别介意。

#include <Wire.h> //总线库文件
//#include <avr/pgmspace.h>
//SDA-AD4,SCL-AD5
int Address = 60;  //OLED 12864器件7位地址
unsigned char buffer12864[8][128] = { 0x00,};//缓存
/*===================================将字模存放到flash空间=====================================*/
const unsigned char logo[8][128] PROGMEM = {
  0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  0x01, 0x01, 0x01, 0x01, 0xC1, 0xC1, 0x61, 0x61, 0x31, 0x31, 0x31, 0x31, 0x31, 0x61, 0xE1, 0x81,
  0x01, 0xC1, 0xC1, 0x61, 0x71, 0x31, 0x31, 0x31, 0xB1, 0xB1, 0x71, 0xE1, 0xC1, 0x01, 0x01, 0x01,
  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x81, 0x81, 0x01, 0x01, 0x01, 0x01,
  0x81, 0x81, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x81, 0x81,
  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x81, 0x81, 0x01, 0x01,
  0x01, 0x01, 0x01, 0x81, 0x81, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xC0, 0x7F, 0x7F, 0xF0, 0xC0, 0x80, 0x80, 0x00, 0x06, 0x0F, 0x8F, 0x8F, 0xF7, 0x3F,
  0x1F, 0x3F, 0x71, 0xE0, 0xC0, 0x80, 0x00, 0x07, 0x87, 0x87, 0xC7, 0xF0, 0x7F, 0x5F, 0x80, 0x80,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0xFF, 0xFF, 0x08, 0x08, 0x08, 0x08,
  0xFF, 0xFF, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x02, 0xF2, 0xF2, 0x12, 0x13,
  0xF3, 0xFA, 0x0A, 0x0A, 0x0A, 0x0A, 0x02, 0x00, 0x08, 0x38, 0x31, 0xC7, 0xC6, 0x88, 0xC9, 0x69,
  0x39, 0x9F, 0x9F, 0x39, 0x69, 0xC9, 0x89, 0x88, 0x00, 0x00, 0xFC, 0xCC, 0xFF, 0xFF, 0xCC, 0xFC,
  0x22, 0x22, 0x22, 0xBF, 0xBF, 0x22, 0x22, 0x22, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xF0, 0x38, 0xCC,
  0xF6, 0x1B, 0x0D, 0x04, 0x04, 0x04, 0x0C, 0x19, 0xF3, 0x83, 0xC3, 0xC3, 0x41, 0x61, 0x60, 0x20,
  0x30, 0x30, 0x10, 0x10, 0x19, 0x19, 0x0B, 0x0B, 0x09, 0x09, 0x0D, 0x0C, 0x0C, 0x04, 0x84, 0xC7,
  0x7E, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x41, 0x61, 0x39, 0x1F, 0x07, 0x01, 0x01, 0x01, 0x01,
  0x7F, 0x7F, 0x01, 0x01, 0x01, 0x01, 0x00, 0x40, 0x70, 0x3F, 0x0F, 0x00, 0x7F, 0x7F, 0x31, 0x11,
  0x21, 0x67, 0x4F, 0x19, 0x31, 0x79, 0x79, 0x00, 0x02, 0x02, 0x3F, 0x3F, 0x00, 0x10, 0x1C, 0x2C,
  0x60, 0x7F, 0x3F, 0x1C, 0x1A, 0x06, 0x1C, 0x18, 0x00, 0x10, 0x31, 0x31, 0x1F, 0x0F, 0x1C, 0x3C,
  0x22, 0x22, 0x22, 0x3F, 0x3F, 0x22, 0x22, 0x22, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0x40, 0x60, 0x60, 0x60, 0x78, 0x1F, 0x00, 0x00, 0x00,
  0x03, 0x06, 0x0C, 0x0C, 0x0C, 0x0C, 0x04, 0x06, 0x03, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0xF0, 0x78, 0x48, 0xCC, 0x02, 0x01, 0x01, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xE0, 0xE0, 0x20, 0x20, 0x60,
  0xC0, 0x80, 0x00, 0x00, 0x20, 0x20, 0xE0, 0xE0, 0x20, 0x20, 0x00, 0x00, 0x20, 0xE0, 0xE0, 0x20,
  0x00, 0xE0, 0xE0, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x06, 0x1F, 0x70, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x20, 0x20, 0x60, 0xC0,
  0x00, 0x18, 0xF0, 0x00, 0x80, 0xE0, 0x38, 0x0C, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  0x01, 0x01, 0x03, 0x06, 0x0C, 0xF8, 0x00, 0x00, 0xFF, 0xC0, 0x40, 0x71, 0x0E, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0x80, 0x80, 0xC0,
  0x7F, 0x3F, 0x00, 0x00, 0x80, 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x83, 0xFF,
  0xFF, 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xE3, 0xB6, 0x94, 0x98, 0x98, 0x98, 0x99,
  0x8F, 0x8E, 0xC7, 0x40, 0x73, 0x1F, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0C, 0x0C, 0x0C,
  0x04, 0x04, 0x04, 0x04, 0x04, 0x0F, 0x08, 0x08, 0x19, 0x19, 0x19, 0x09, 0x0B, 0x0F, 0x06, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xF0, 0x80, 0xC0, 0x31, 0x19, 0xE1, 0x01,
  0xE1, 0x18, 0x18, 0xF0, 0x80, 0xC0, 0x30, 0x18, 0xE0, 0x00, 0xE0, 0x18, 0x18, 0xF0, 0x80, 0xC0,
  0x30, 0x18, 0xE0, 0x00, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x00, 0x30, 0x48, 0x48, 0x88, 0x00, 0x00,
  0xF8, 0x10, 0x08, 0x08, 0xF0, 0x10, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0xFB, 0x00, 0x00, 0xFF,
  0x00, 0x00, 0xF0, 0x58, 0x48, 0x48, 0x70, 0x00, 0x00, 0x08, 0xFE, 0x09, 0x09, 0x00, 0xF8, 0x10,
  0x08, 0x08, 0x00, 0xF0, 0x18, 0x08, 0x08, 0x18, 0xF0, 0x00, 0x00, 0xF0, 0x18, 0x08, 0x08, 0x10,
  0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x10, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0xF0, 0x58,
  0x48, 0x48, 0x70, 0x00, 0x00, 0x08, 0xFE, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
  0xFF, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x83, 0x81, 0x80, 0x80, 0x81, 0x83,
  0x80, 0x80, 0x80, 0x80, 0x83, 0x81, 0x80, 0x80, 0x81, 0x83, 0x80, 0x80, 0x80, 0x80, 0x83, 0x81,
  0x80, 0x80, 0x81, 0x83, 0x80, 0x80, 0x80, 0x83, 0x83, 0x80, 0x82, 0x82, 0x82, 0x81, 0x80, 0x80,
  0x83, 0x80, 0x80, 0x80, 0x83, 0x80, 0x80, 0x80, 0x80, 0x83, 0x80, 0x80, 0x83, 0x80, 0x80, 0x83,
  0x80, 0x80, 0x81, 0x83, 0x82, 0x82, 0x81, 0x80, 0x80, 0x80, 0x83, 0x80, 0x80, 0x80, 0x83, 0x80,
  0x80, 0x80, 0x80, 0x81, 0x83, 0x82, 0x82, 0x83, 0x81, 0x80, 0x80, 0x81, 0x8B, 0x92, 0x92, 0x89,
  0x8F, 0x80, 0x80, 0x83, 0x83, 0x80, 0x83, 0x80, 0x80, 0x80, 0x80, 0x83, 0x80, 0x80, 0x81, 0x83,
  0x82, 0x82, 0x81, 0x80, 0x80, 0x80, 0x83, 0x82, 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xFF,
};
const unsigned char F8X16[][16] PROGMEM =
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0
  0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x30, 0x00, 0x00, 0x00, //!1
  0x00, 0x10, 0x0C, 0x06, 0x10, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //"2
  0x40, 0xC0, 0x78, 0x40, 0xC0, 0x78, 0x40, 0x00, 0x04, 0x3F, 0x04, 0x04, 0x3F, 0x04, 0x04, 0x00, //#3
  0x00, 0x70, 0x88, 0xFC, 0x08, 0x30, 0x00, 0x00, 0x00, 0x18, 0x20, 0xFF, 0x21, 0x1E, 0x00, 0x00, //$4
  0xF0, 0x08, 0xF0, 0x00, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x21, 0x1C, 0x03, 0x1E, 0x21, 0x1E, 0x00, //%5
  0x00, 0xF0, 0x08, 0x88, 0x70, 0x00, 0x00, 0x00, 0x1E, 0x21, 0x23, 0x24, 0x19, 0x27, 0x21, 0x10, //&6
  0x10, 0x16, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //'7
  0x00, 0x00, 0x00, 0xE0, 0x18, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x07, 0x18, 0x20, 0x40, 0x00, //(8
  0x00, 0x02, 0x04, 0x18, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x18, 0x07, 0x00, 0x00, 0x00, //)9
  0x40, 0x40, 0x80, 0xF0, 0x80, 0x40, 0x40, 0x00, 0x02, 0x02, 0x01, 0x0F, 0x01, 0x02, 0x02, 0x00, //*10
  0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x1F, 0x01, 0x01, 0x01, 0x00, //+11
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xB0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, //,12
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, //-13
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, //.14
  0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x18, 0x04, 0x00, 0x60, 0x18, 0x06, 0x01, 0x00, 0x00, 0x00, ///15
  0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x10, 0x0F, 0x00, //016
  0x00, 0x10, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //117
  0x00, 0x70, 0x08, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x30, 0x28, 0x24, 0x22, 0x21, 0x30, 0x00, //218
  0x00, 0x30, 0x08, 0x88, 0x88, 0x48, 0x30, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, //319
  0x00, 0x00, 0xC0, 0x20, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x07, 0x04, 0x24, 0x24, 0x3F, 0x24, 0x00, //420
  0x00, 0xF8, 0x08, 0x88, 0x88, 0x08, 0x08, 0x00, 0x00, 0x19, 0x21, 0x20, 0x20, 0x11, 0x0E, 0x00, //521
  0x00, 0xE0, 0x10, 0x88, 0x88, 0x18, 0x00, 0x00, 0x00, 0x0F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, //622
  0x00, 0x38, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, //723
  0x00, 0x70, 0x88, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x1C, 0x22, 0x21, 0x21, 0x22, 0x1C, 0x00, //824
  0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x00, 0x31, 0x22, 0x22, 0x11, 0x0F, 0x00, //925
  0x00, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, //:26
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x00, 0x00, 0x00, 0x00, //;27
  0x00, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, //<28
  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, //=29
  0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, //>30
  0x00, 0x70, 0x48, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x36, 0x01, 0x00, 0x00, //?31
  0xC0, 0x30, 0xC8, 0x28, 0xE8, 0x10, 0xE0, 0x00, 0x07, 0x18, 0x27, 0x24, 0x23, 0x14, 0x0B, 0x00, //@32
  0x00, 0x00, 0xC0, 0x38, 0xE0, 0x00, 0x00, 0x00, 0x20, 0x3C, 0x23, 0x02, 0x02, 0x27, 0x38, 0x20, //A33
  0x08, 0xF8, 0x88, 0x88, 0x88, 0x70, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, //B34
  0xC0, 0x30, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x07, 0x18, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, //C35
  0x08, 0xF8, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, //D36
  0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x23, 0x20, 0x18, 0x00, //E37
  0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00, //F38
  0xC0, 0x30, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x07, 0x18, 0x20, 0x20, 0x22, 0x1E, 0x02, 0x00, //G39
  0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x21, 0x3F, 0x20, //H40
  0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //I41
  0x00, 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, 0x00, //J42
  0x08, 0xF8, 0x88, 0xC0, 0x28, 0x18, 0x08, 0x00, 0x20, 0x3F, 0x20, 0x01, 0x26, 0x38, 0x20, 0x00, //K43
  0x08, 0xF8, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x30, 0x00, //L44
  0x08, 0xF8, 0xF8, 0x00, 0xF8, 0xF8, 0x08, 0x00, 0x20, 0x3F, 0x00, 0x3F, 0x00, 0x3F, 0x20, 0x00, //M45
  0x08, 0xF8, 0x30, 0xC0, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x20, 0x00, 0x07, 0x18, 0x3F, 0x00, //N46
  0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, //O47
  0x08, 0xF8, 0x08, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x01, 0x00, 0x00, //P48
  0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x18, 0x24, 0x24, 0x38, 0x50, 0x4F, 0x00, //Q49
  0x08, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x0C, 0x30, 0x20, //R50
  0x00, 0x70, 0x88, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x38, 0x20, 0x21, 0x21, 0x22, 0x1C, 0x00, //S51
  0x18, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x18, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, //T52
  0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, //U53
  0x08, 0x78, 0x88, 0x00, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x07, 0x38, 0x0E, 0x01, 0x00, 0x00, //V54
  0xF8, 0x08, 0x00, 0xF8, 0x00, 0x08, 0xF8, 0x00, 0x03, 0x3C, 0x07, 0x00, 0x07, 0x3C, 0x03, 0x00, //W55
  0x08, 0x18, 0x68, 0x80, 0x80, 0x68, 0x18, 0x08, 0x20, 0x30, 0x2C, 0x03, 0x03, 0x2C, 0x30, 0x20, //X56
  0x08, 0x38, 0xC8, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, //Y57
  0x10, 0x08, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x20, 0x38, 0x26, 0x21, 0x20, 0x20, 0x18, 0x00, //Z58
  0x00, 0x00, 0x00, 0xFE, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x40, 0x40, 0x40, 0x00, //[59
  0x00, 0x0C, 0x30, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x38, 0xC0, 0x00, //\60
  0x00, 0x02, 0x02, 0x02, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7F, 0x00, 0x00, 0x00, //]61
  0x00, 0x00, 0x04, 0x02, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //^62
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //_63
  0x00, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //`64
  0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x19, 0x24, 0x22, 0x22, 0x22, 0x3F, 0x20, //a65
  0x08, 0xF8, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, //b66
  0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x20, 0x11, 0x00, //c67
  0x00, 0x00, 0x00, 0x80, 0x80, 0x88, 0xF8, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x10, 0x3F, 0x20, //d68
  0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x22, 0x22, 0x22, 0x22, 0x13, 0x00, //e69
  0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0x18, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //f70
  0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x6B, 0x94, 0x94, 0x94, 0x93, 0x60, 0x00, //g71
  0x08, 0xF8, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, //h72
  0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //i73
  0x00, 0x00, 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, //j74
  0x08, 0xF8, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x24, 0x02, 0x2D, 0x30, 0x20, 0x00, //k75
  0x00, 0x08, 0x08, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, //l76
  0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x3F, 0x20, 0x00, 0x3F, //m77
  0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, //n78
  0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, //o79
  0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xA1, 0x20, 0x20, 0x11, 0x0E, 0x00, //p80
  0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0xA0, 0xFF, 0x80, //q81
  0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x20, 0x3F, 0x21, 0x20, 0x00, 0x01, 0x00, //r82
  0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x33, 0x24, 0x24, 0x24, 0x24, 0x19, 0x00, //s83
  0x00, 0x80, 0x80, 0xE0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x00, 0x00, //t84
  0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x10, 0x3F, 0x20, //u85
  0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x01, 0x0E, 0x30, 0x08, 0x06, 0x01, 0x00, //v86
  0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x0F, 0x30, 0x0C, 0x03, 0x0C, 0x30, 0x0F, 0x00, //w87
  0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x31, 0x2E, 0x0E, 0x31, 0x20, 0x00, //x88
  0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x81, 0x8E, 0x70, 0x18, 0x06, 0x01, 0x00, //y89
  0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x21, 0x30, 0x2C, 0x22, 0x21, 0x30, 0x00, //z90
  0x00, 0x00, 0x00, 0x00, 0x80, 0x7C, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x40, 0x40, //{91
  0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, //|92
  0x00, 0x02, 0x02, 0x7C, 0x80, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00, //}93
  0x00, 0x06, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //~94
};
/*=====i2c写命令=====*/
void Writecom(unsigned char COM)
{
  Wire.beginTransmission(Address);
  Wire.write(0x00);
  Wire.write(COM);
  Wire.endTransmission();
}
/*=====i2c写数据=====*/
void Writedata(unsigned char DATA)
{
  Wire.beginTransmission(Address);
  Wire.write(0x40);
  Wire.write(DATA);
  Wire.endTransmission();
}
/*=====OLED 初始化=====*/
void SSD1306_Init()
{
  Writecom(0XAE);//display off
  Writecom(0X00);//set lower column address
  Writecom(0X10);//set higher column address
  Writecom(0X40);//set display start line
  Writecom(0XB0);//set page address
  Writecom(0X81);//set contract control
  Writecom(0XCF);// VCC Generated by Internal DC/DC Circuit
  Writecom(0XA1);//set segment remap  column address 127 is mapped to SEG0
  Writecom(0XA6);//normal / reverse   normal display
  Writecom(0XA8);//multiplex ratio
  Writecom(0X3F);//1/64
  Writecom(0XC8);//Com scan direction remapped mode. Scan from COM[N-1] to COM0
  Writecom(0XD3);//set display offset
  Writecom(0X00);
  Writecom(0XD5);//set osc division
  Writecom(0X80);
  Writecom(0XD9);//set pre-charge period
  Writecom(0X11);
  Writecom(0XDa);//set COM pins
  Writecom(0X12);
  Writecom(0X8d);/*set charge pump enable*/
  Writecom(0X14);
  Writecom(0Xdb);//Set VcomH
  Writecom(0X20);
  Writecom(0XAF);//display ON
}
/*===========================清屏函数===========================*/
void clear(unsigned char data)
{
  Writecom(0XAE);//display OFF
  static unsigned char i, j;
  Writecom(0x00);//set lower column address
  Writecom(0x10);//set higher column address
  for (j = 0; j < 8; j++)
  {
    Writecom(0xB0 + j); //set page address
    for (i = 0; i < 128; i++)
    {
      buffer12864[j][i] = 0x00;
      Writedata(data);
    }
  }
  Writecom(0XAF);//display ON
}
/*===========================画点===========================*/
void Draw_Point(uint8_t x, uint8_t y)
{
  static unsigned char page, higher, lower, cache1, cache2;
  page = y / 8;
  higher = x / 16;
  lower = x % 16;
  cache1 = 0x01 << (y % 8);
  cache2 = buffer12864[page][x] | cache1;
  buffer12864[page][x] = cache2;
  Writecom(0xB0 + page); //set page address, page<=7
  Writecom(0x10 + higher); //set higher column address, higher<=7
  Writecom(0x00 + lower);//set lower column address, lower<=15
  Writedata(cache2);
}
/*===========================叠加新数据===========================*/
void Draw_byte(uint8_t page, uint8_t lie, uint8_t data)
{
  static unsigned char  higher, lower, cache;
  higher = lie / 16;
  lower = lie % 16;
  cache = buffer12864[page][lie] | data;
  buffer12864[page][lie] = cache;
  Writecom(0xB0 + page); //set page address, page<=7
  Writecom(0x10 + higher); //set higher column address, higher<=7
  Writecom(0x00 + lower);//set lower column address, lower<=15
  Writedata(cache);
}
/*===========================覆盖数据===========================*/
void Draw_byte_clear(uint8_t page, uint8_t lie, uint8_t data) //half
{
  static unsigned char  higher, lower, cache;
  higher = lie / 16;
  lower = lie % 16;
  cache = buffer12864[page][lie] | data;
  Writecom(0xB0 + page); //set page address, page<=7
  Writecom(0x10 + higher); //set higher column address, higher<=7
  Writecom(0x00 + lower);//set lower column address, lower<=15
  Writedata(cache);
}
/*===========================叠加写字节===========================*/
void show_char(uint8_t page, uint8_t lie, uint8_t data)
{
  static unsigned char m;
  if ((data <= '~') && (data >= ' '))
  {
    for (int m = 0; m < 8; m++)
    {
      Draw_byte(page, lie + m, pgm_read_byte(&F8X16[data - 32][m]));
      Draw_byte(page + 1, lie + m, pgm_read_byte(&F8X16[data - 32][m + 8]));
    }
  }
}
/*===========================覆盖写字节===========================*/
void show_char_clear(uint8_t page, uint8_t lie, uint8_t data)
{
  static unsigned char m;
  if ((data <= '~') && (data >= ' '))
  {
    for (int m = 0; m < 8; m++)
    {
      Draw_byte_clear(page, lie + m, pgm_read_byte(&F8X16[data - 32][m]));
      Draw_byte_clear(page + 1, lie + m, pgm_read_byte(&F8X16[data - 32][m + 8]));
    }
  }
  else
  {
    for (int m = 0; m < 8; m++)
    {
      Draw_byte_clear(page, lie + m, pgm_read_byte(&F8X16[0][m]));
      Draw_byte_clear(page + 1, lie + m, pgm_read_byte(&F8X16[0][m + 8]));
    }
  }
}
/*===========================叠加写字符串===========================*/
void show_string(uint8_t page, uint8_t lie, String Data)
{
  static unsigned char m;
  for (m = 0; m < (Data.length()); m++)
  {
    show_char(page, lie + m * 8, Data[m]);
  }
}
/*===========================覆盖写字符串===========================*/
void show_string_clear(uint8_t page, uint8_t lie, String data) //专用函数 - 仅显示4位字符
{
  static unsigned char m, n;
  unsigned char Data[] = {'8', '8', '8', '8',}; //用string不知道啥原因报错就用了char类型,效果一样的
  n = (data.length());//读取字符串内字符个数
  switch (n)
  {
    case 1:
      {
        Data[3] = data[0];
        Data[2] = ' ';
        Data[1] = ' ';
        Data[0] = ' ';
      } break;
    case 2:
      {
        Data[3] = data[1];
        Data[2] = data[0];
        Data[1] = ' ';
        Data[0] = ' ';
      } break;
    case 3:
      {
        Data[3] = data[2];
        Data[2] = data[1];
        Data[1] = data[0];
        Data[0] = ' ';
      } break;
    case 4:
      {
        Data[3] = data[3];
        Data[2] = data[2];
        Data[1] = data[1];
        Data[0] = data[0];
      } break;
    case 5:
      {
        Data[3] = data[3];
        Data[2] = data[2];
        Data[1] = data[1];
        Data[0] = data[0];
      } break;
    default:
      {
        ;
      } break;
  }
  for (m = 0; m < 4; m++)
  {
    show_char_clear(page, lie + m * 8, Data[m]);
  }
}
/*===========================开机动画===========================*/
void display_logo(void)
{
  Writecom(0XAE);//display OFF
  static unsigned char x, y, m, n;
  for (x = 0; x < 8; x++)
  {
    for (y = 0; y < 128; y++)
    {
      Draw_byte_clear(x, y, pgm_read_byte(&logo[x][y])); // pgm_read_byte(&logo[x][y]) 读取存在flash里的字模
    }
  }
  Writecom(0XAF);//display ON
  for (m = 0; m < 8; m++) //以下是翻屏
  {
    for (n = 0; n < 64; n++)
    {
      Writecom(0XD3);//set display offset
      Writecom(0X00 + n);
      delay(1);
    }
  }
  for (m = 0; m < 5; m++)
  {
    for (n = 0; n < 64; n++)
    {
      Writecom(0XD3);//set display offset
      Writecom(0X00 + n);
      delay(2);
    }
  }
  Writecom(0XD3);//set display offset
  Writecom(0X00);
  delay(100);
}
/*===========================画框===========================*/
void display_frame(void)
{
  clear(0x00);
  Writecom(0XAE);//display OFF
  static unsigned char x, y, m, n;
  /*=============画框=========*/
  for (x = 0; x < 8; x++)
  {
    Draw_byte(x, 0, 0xff);
    Draw_byte(x, 11, 0xff);
    Draw_byte(x, 74, 0xff);
    Draw_byte(x, 73, 0xff);
    Draw_byte(x, 127, 0xff);
  }
  for (y = 0; y < 128; y++)
  {
    Draw_Point(y, 0);
    Draw_Point(y, 31);
    Draw_Point(y, 32);
    Draw_Point(y, 63);
  }
  for (y = 11; y < 128; y++)
  {
    Draw_Point(y, 16);
    Draw_Point(y, 48);
  }
  /*=============名称显示=======*/
  show_char(0, 2, 'A');
  show_char(2, 2, 'D');
  show_char(4, 2, '5');
  show_char(6, 2, 'V');
  String pu = "U=";
  String pi = "I=";
  show_string(0, 13, pu);
  show_string(2, 13, pi);
  show_string(4, 13, pu);
  show_string(6, 13, pi);
  String p3 = "I(3V3)";
  String p1 = "I(1V8)";
  show_string(0, 77, p3);
  show_string(4, 77, p1);
  /*=============单位显示=========*/
  show_char(0, 63, 'V');
  show_char(2, 63, 'A');
  show_char(4, 63, 'V');
  show_char(6, 63, 'A');
  String pa = "mA";
  show_string(2, 110, pa);
  show_string(6, 110, pa);
  Writecom(0XAF);//display ON
}
void setup()
{
  Wire.begin();  //IIC初始化
  Serial.begin(9600);
  SSD1306_Init();  //SSD1306初始化
  delay(100);
  display_logo();//开机logo
  delay(2000);
  display_frame();//画边框及固定符号
};
void loop()
{
  double Uadj = 0, Iadj = 0, U5v = 0, I5v = 0;
  unsigned int I3v3 = 0, I1v8 = 0;
  I1v8 = analogRead(A0);
  I3v3 = analogRead(A1);
  Uadj = analogRead(A2);
  Iadj = analogRead(A3);
  U5v = analogRead(A6);
  I5v = analogRead(A7);
  // Serial.print("I1v8=");
  // Serial.println(I1v8);
  show_string_clear(2, 77, String(I3v3));
  show_string_clear(6, 77, String(I1v8));
  show_string_clear(0, 30, String(Uadj));
  show_string_clear(2, 30, String(Iadj));
  show_string_clear(4, 30, String(U5v));
  show_string_clear(6, 30, String(I5v));
  delay(100);
}

      完。。。。。。。。。

1 thought on “Arduino i2c协议 驱动ssd1306 OLED

发表评论

电子邮件地址不会被公开。 必填项已用*标注