使用U8g2库驱动I2C 0.96“or1.3“OLED屏幕相关接口函数选择

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用U8g2库驱动I2C 0.96“or1.3“OLED屏幕相关接口函数选择相关的知识,希望对你有一定的参考价值。

U8g2库驱动I2C 0.96"or1.3"OLED屏幕相关接口函数选择

这里仅讨论使用U8g2库驱动I2C屏幕

  • 以ESP8266作为开发板
  • Nodemcu1.0
  • SDA:D2 ,SCL:D1
  • 实例所使用的OLED屏幕为1.3"实测

相关的接口函数选择(踩坑踩出来的经验)

有些接口函数并没有在示例程序代码中找到定义。

  • 接口文件路径:C:\\Users\\Administrator\\Documents\\Arduino\\libraries\\U8g2\\src里的U8g2lib.h
//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* reset=*/ 8);

//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE);   // 0.96寸屏幕All Boards without Reset of the Display
//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE);   //0.96寸屏幕 pure SW emulated I2C

//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ D1, /* data=*/ D2);   //0.96寸屏幕, HW I2C 
//U8G2_SH1106_128X64_NONAME_F_SW_I2C u8g2(U8G2_R2,  /*SCL*/  D1,  /*SDA*/  D2,   /*reset*/  U8X8_PIN_NONE);//1.3寸屏幕支持U8G2_R旋转显示
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /*reset*/  U8X8_PIN_NONE,  /*SCL*/  SCL,  /*SDA*/  SDA );//1.3寸屏幕支持U8G2_R旋转显示与上面的接口注意区分,SDA和SCL位置不一样
//U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);//1.3寸屏幕可以在U8G2_R0模式下显示正常,不支持其他旋转模式
  • U8G2_R0:旋转角度显示,U8G2_R0:0°,U8G2_R1:90°,U8G2_R2:180°

*实例代码

/*
  UTF8.ino
  Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
*/

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define SDA (4)
#define SCL (5)
/*
  U8g2lib Example Overview:
    Frame Buffer Examples: clearBuffer/sendBuffer. Fast, but may not work with all Arduino boards because of RAM consumption
    Page Buffer Examples: firstPage/nextPage. Less RAM usage, should work with all Arduino boards.
    U8x8 Text Only Example: No RAM usage, direct communication with display controller. No graphics, 8x8 Text only.
    
  This is a page buffer example.    
*/

// Please UNCOMMENT one of the contructor lines below
// U8g2 Contructor List (Picture Loop Page Buffer)
// The complete list is available here: https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
// Please update the pin numbers according to your setup. Use U8X8_PIN_NONE if the reset pin is not connected

//U8G2_SSD1306_128X64_NONAME_1_3W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* reset=*/ 8);
//U8G2_SSD1306_128X64_NONAME_1_3W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* reset=*/ 8);
//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* reset=*/ 8);

//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE);   // 0.96寸屏幕All Boards without Reset of the Display
//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE);   //0.96寸屏幕 pure SW emulated I2C

//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ D1, /* data=*/ D2);   //0.96寸屏幕, HW I2C 
//U8G2_SH1106_128X64_NONAME_F_SW_I2C u8g2(U8G2_R2,  /*SCL*/  D1,  /*SDA*/  D2,   /*reset*/  U8X8_PIN_NONE);//1.3寸屏幕支持U8G2_R旋转显示
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /*reset*/  U8X8_PIN_NONE,  /*SCL*/  SCL,  /*SDA*/  SDA );//1.3寸屏幕支持U8G2_R旋转显示与上面的接口注意区分,SDA和SCL位置不一样
//U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);//1.3寸屏幕可以在U8G2_R0模式下显示正常,不支持其他旋转模式
// End of constructor list

void setup(void) {

  /* U8g2 Project: SSD1306 Test Board */
  u8g2.begin(); 
  u8g2.enableUTF8Print(); //使print支持UTF8字集 
 // u8g2.setDisplayRotation(U8G2_R2);//设置显示器的旋转角度
}

void loop(void) {
    u8g2.clearBuffer();
  drawLogo();
  drawURL();
  u8g2.sendBuffer();
  delay(1000);
u8g2.setFont(u8g2_font_unifont_t_chinese2); 
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();
  u8g2.setCursor(25, 15);
  u8g2.print("hello world");
  u8g2.sendBuffer();
  delay(1000);
  u8g2.setCursor(25, 40);
    u8g2.print("你好世界");
  u8g2.sendBuffer();
  delay(1000);
}
void drawLogo(void)
{
    u8g2.setFontMode(1);  // Transparent
#ifdef MINI_LOGO

    u8g2.setFontDirection(0);
    u8g2.setFont(u8g2_font_inb16_mf);
    u8g2.drawStr(0, 22, "U");
    
    u8g2.setFontDirection(1);
    u8g2.setFont(u8g2_font_inb19_mn);
    u8g2.drawStr(14,8,"8");
    
    u8g2.setFontDirection(0);
    u8g2.setFont(u8g2_font_inb16_mf);
    u8g2.drawStr(36,22,"g");
    u8g2.drawStr(48,22,"\\xb2");
    
    u8g2.drawHLine(2, 25, 34);
    u8g2.drawHLine(3, 26, 34);
    u8g2.drawVLine(32, 22, 12);
    u8g2.drawVLine(33, 23, 12);
#else

    u8g2.setFontDirection(0);
    u8g2.setFont(u8g2_font_inb24_mf);
    u8g2.drawStr(0, 30, "U");
    
    u8g2.setFontDirection(1);
    u8g2.setFont(u8g2_font_inb30_mn);
    u8g2.drawStr(21,8,"8");
        
    u8g2.setFontDirection(0);
    u8g2.setFont(u8g2_font_inb24_mf);
    u8g2.drawStr(51,30,"g");
    u8g2.drawStr(67,30,"\\xb2");
    
    u8g2.drawHLine(2, 35, 47);
    u8g2.drawHLine(3, 36, 47);
    u8g2.drawVLine(45, 32, 12);
    u8g2.drawVLine(46, 33, 12);
    
#endif
}

void drawURL(void)
{
#ifndef MINI_LOGO
  u8g2.setFont(u8g2_font_4x6_tr);
  if ( u8g2.getDisplayHeight() < 59 )
  {
    u8g2.drawStr(89,20,"github.com");
    u8g2.drawStr(73,29,"/olikraus/u8g2");
  }
  else
  {
    u8g2.drawStr(1,54,"github.com/olikraus/u8g2");
  }
#endif
}


将1.3"OLED能使用的接口函数在实例中都已经加以注释说明,希望后面的人,避坑!

以上是关于使用U8g2库驱动I2C 0.96“or1.3“OLED屏幕相关接口函数选择的主要内容,如果未能解决你的问题,请参考以下文章

基于Lua框架下Air103使用U8g2库驱动I2C OLED图像显示

Arduino ESP8266 +0.96“Oled屏幕基于U8g2库实现充电和wifi图标动态显示

Arduino ESP32 0.96OLED I2C屏幕驱动显示

ESP8266驱动SSD1306 ESP8266 for Arduino(NodeMCU U8G2库)

ESP8266 + 0.96“ I2C OLED双色屏显示

U8g2库I2C总线再次突破性调试成功