关于Arduino&SSD1306OLED(IIC)显示的学习

1、 前期准备

  1. arduino UNO开发板
  2. OLED 显示屏
  3. 导线若干
  4. 取模软件zimoV2.2
    这里推荐一个在线图片取模软件
    http://tools.clz.me/

2、引脚接线

关于Arduino&SSD1306OLED(IIC)显示的学习_第1张图片
OLED 显示屏有四个引脚,分别是:
SDA(数据线) SCK(时钟线) VDD(3.3V) GND
在UNO开发板上I2C接口,SDA对应D4,SCK对应D5
在MEGA2560开发板上I2C接口,SDA对应D20, SCL对应D21

3、通信地址

背面图:
关于Arduino&SSD1306OLED(IIC)显示的学习_第2张图片
模块背面的IIC ADRESSSELECT表示该模块在IIC通信作为从机时的地址,当中间的脚用电阻和左边接起来时,地址为0x78,当和右边接起来时,地址为0x7A。
关于Arduino&SSD1306OLED(IIC)显示的学习_第3张图片
这款屏幕尺寸约为0.96英寸,由SSD1306驱动,驱动接口I2C,I2C地址(0x3c 默认/0x3d)。
不同型号有不同的通信地址,可通过程序查询:

#include   
   
void setup(){  
  Wire.begin();  
  Serial.begin(9600);  
  Serial.println("\nI2C Scanner");  
}  
void loop(){  
  byte error, address;  
  int nDevices;  
  Serial.println("Scanning...");  
  nDevices = 0;  
  for (address = 1; address < 127; address++ ){  
    // The i2c_scanner uses the return value of  
    // the Write.endTransmisstion to see if  
    // a device did acknowledge to the address.  
    Wire.beginTransmission(address);  
    error = Wire.endTransmission();  
    if (error == 0){  
      Serial.print("I2C device found at address 0x");  
      if (address < 16)  
        Serial.print("0");  
      Serial.print(address, HEX);  
      Serial.println(" !");  
      nDevices++;  
    }else if (error == 4){  
      Serial.print("Unknow error at address 0x");  
      if (address < 16)  
        Serial.print("0");  
      Serial.println(address, HEX);  
    }  
  }  
  if (nDevices == 0)  
    Serial.println("No I2C devices found\n");  
  else  
    Serial.println("done\n");  
  delay(5000); // wait 5 seconds for next scan  
} 

上传到arduino板上,打开串口可查看地址
关于Arduino&SSD1306OLED(IIC)显示的学习_第4张图片

4、编程与程序解读

注:编程之前需要添加Adafruit_SSD1306和Adafruit_GFX这两个库文件

官方例程:

/*********************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/category/63_98

This example is for a 128x64 size display using I2C to communicate
3 pins are required to interface (2 I2C and one reset)

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada  for Adafruit Industries.  
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/

#include 
#include 
#include 

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  
  display.display(); // show splashscreen
  delay(2000);
  display.clearDisplay();   // clears the screen and buffer

  // draw a single pixel
  display.drawPixel(10, 10, WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw many lines
  testdrawline();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw rectangles
  testdrawrect();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw multiple rectangles
  testfillrect();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw mulitple circles
  testdrawcircle();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw a white circle, 10 pixel radius
  display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

  testdrawroundrect();
  delay(2000);
  display.clearDisplay();

  testfillroundrect();
  delay(2000);
  display.clearDisplay();

  testdrawtriangle();
  delay(2000);
  display.clearDisplay();
   
  testfilltriangle();
  delay(2000);
  display.clearDisplay();

  // draw the first ~12 characters in the font
  testdrawchar();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw scrolling text
  testscrolltext();
  delay(2000);
  display.clearDisplay();

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.println(3.141592);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("0x"); display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);

  // miniature bitmap display
  display.clearDisplay();
  display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
  display.display();

  // invert the display
  display.invertDisplay(true);
  delay(1000); 
  display.invertDisplay(false);
  delay(1000); 

  // draw a bitmap icon and 'animate' movement
  testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
}


void loop() {
  
}


void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  uint8_t icons[NUMFLAKES][3];
  srandom(666);     // whatever seed
 
  // initialize
  for (uint8_t f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS] = random() % display.width();
    icons[f][YPOS] = 0;
    icons[f][DELTAY] = random() % 5 + 1;
    
    Serial.print("x: ");
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(" y: ");
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(" dy: ");
    Serial.println(icons[f][DELTAY], DEC);
  }

  while (1) {
    // draw each icon
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, WHITE);
    }
    display.display();
    delay(200);
    
    // then erase it + move it
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS],  logo16_glcd_bmp, w, h, BLACK);
      // move it
      icons[f][YPOS] += icons[f][DELTAY];
      // if its gone, reinit
      if (icons[f][YPOS] > display.height()) {
	icons[f][XPOS] = random() % display.width();
	icons[f][YPOS] = 0;
	icons[f][DELTAY] = random() % 5 + 1;
      }
    }
   }
}


void testdrawchar(void) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);

  for (uint8_t i=0; i < 168; i++) {
    if (i == '\n') continue;
    display.write(i);
    if ((i > 0) && (i % 21 == 0))
      display.println();
  }    
  display.display();
}

void testdrawcircle(void) {
  for (int16_t i=0; i0; i-=5) {
    display.fillTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
  }
}

void testdrawroundrect(void) {
  for (int16_t i=0; i=0; i-=4) {
    display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
    display.display();
  }
  delay(250);
  
  display.clearDisplay();
  for (int16_t i=display.width()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
    display.display();
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
    display.display();
  }
  delay(250);

  display.clearDisplay();
  for (int16_t i=0; i

程序与函数讲解

以下面简单显示程序为例

#include 

#include 
#include 
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
 
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
 
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()				//初始化
{
  Serial.begin(9600);
  delay(500);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64) ,0x3C为I2C协议通讯地址,需根据实际情况更改
}
void loop()
{
  test_SSD1306();			//调用测试函数
}

void test_SSD1306(void)		//测试函数
{
/*-----------------点亮全屏检测屏幕是否有不正常点亮现象-----------------------------*/
  
  display.fillScreen(WHITE);
  display.display();
  delay(2000);

/*------------------------------画点 点坐标(64,32)-------------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.drawPixel(64, 32, WHITE);
  display.display();
  delay(2000);

  /*-------------------------- 画线 从(0,0)到(128,64)----------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.drawLine(0, 0,128,64, WHITE);
  display.display();
  delay(2000);

/*--------------.画空心矩形  左上角坐标(x0,y0)  右下角坐标(x1,y1)------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.drawRect(0,0,128,64,WHITE);
  display.display();
  delay(2000);

/*-----------------------实心矩形---------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.fillRect(0,0,128,64,WHITE);
  display.display();
  delay(2000);

/*------------------------画空心圆-------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.drawCircle(64,32,20,WHITE);
  display.display();
  delay(2000);

/*----------------------画实心圆---------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.fillCircle(128,64,20,WHITE);
  display.display();
  delay(2000);

/*---------------------画空心三角形-------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.drawTriangle(64,0,0,63,128,63,WHITE);
  display.display();
  delay(2000);
 
/*------------------------画实心三角形-----------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.fillTriangle(64,0,0,63,128,63,WHITE);
  display.display();
  delay(2000);
 
/*-----------------------空心圆角矩形------------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.drawRoundRect(0,0,128,64,5,WHITE);
  display.display();
  delay(2000);
 
/*----------------------画实心圆角矩形-----------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.fillRoundRect(0,0,128,64,5,WHITE);
  display.display();
  delay(2000);

/*------------------------显示英文 数字---------------------*/
  display.clearDisplay();   // clears the screen and buffer
  display.setTextSize(1);	//选择字号
  display.setTextColor(WHITE);	//字体颜色
  display.setCursor(0,0);		//起点坐标
  display.println("Hello, Arduino!");
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.println(3.141592);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("0x"); display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);
  }

关于显示,首先要建立坐标概念
关于Arduino&SSD1306OLED(IIC)显示的学习_第5张图片
这种型号的OLED 显示屏就是一个128(width)X64(height)点阵。在坐标系中,左上角是原点,X轴水平向右数值增加,Y轴竖直向下数值增大。

1.begin()  
  初始化I2C地址,在Arduino setup调用。
  
2.clearDisplay()  
  把Buffer缓存区清零,清除屏幕显示的东西,消影
  
  3.display()  
  把Buffer缓存区的数据显示到屏幕上,任意一个需要显示的操作都需要调用这个方法

4.drawPixel(int16_t x, int16_t y, uint16_t color)    
  画点 点坐标 (x,y)		(x,y,color)

5.drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) 
  画线 从(x0,y0)到(x1,y1)  (x0,y0,x1,y1,color)

6.drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)  
  画空心矩形  左上角坐标(x0,y0)  宽度w 高度h		(x0,y0,w,h,color)

7.fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)  
  画实心矩形  左上角坐标(x0,y0)  宽度w 高度h		(x0,y0,w,h,color)

8.drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)  
  画空心圆,圆心(x0,y0),半径 r		(x0,y0,r,color)

9.fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) 
  画实心圆,圆心(x0,y0),半径 r		(x0,y0,r,color)

10.drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)       
  画空心三角形,上顶点坐标(x0,y0),左顶点坐标(x1,y1),右顶点坐标(x2,y2)
  (x0,y0,x1,y1,x2,y2,color)

11.fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,  int16_t x2, int16_t y2, uint16_t color)  
  画实心三角形,上顶点坐标(x0,y0),左顶点坐标(x1,y1),右顶点坐标(x2,y2)
  (x0,y0,x1,y1,x2,y2,color)

12.drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,int16_t radius, uint16_t color)        
  画空心圆角矩形,左上角坐标(x0,y0)  宽度w 高度h,圆角半径 radius
  (x0,y0,w,h,radius,color)

13.fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,int16_t radius, uint16_t color)     
  画实心圆角矩形,左上角坐标(x0,y0)  宽度w 高度h,圆角半径 radius

/*14.drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,int16_t w, int16_t h, uint16_t color) 
  
  画任意图形,左上角坐标(x,y),图形数据 bitmap,图形高度h 宽度w
  图形以点阵的显示形式生成代码,bitmap可以是真正图片的数据,也可以是单个文字的字模。


15.ShowCN_16(int16_t x, int16_t y, const uint8_t *bitmap,uint8_t size,uint16_t color)  
  显示一行文字(16X16字模),左上角坐标(x,y),字模bitmap,字数size	*/

至于图片的静态和动态显示问题,在稍后学习之后再做更新

文字的显示

  1. 单文字显示

文字的显示需要运用取模软件取模,这里用的是zimoV2.2
在这里插入图片描述
打开软件
关于Arduino&SSD1306OLED(IIC)显示的学习_第6张图片
以文字“一”为例
新建一个16*16的像素块关于Arduino&SSD1306OLED(IIC)显示的学习_第7张图片
在文字区输入 汉字“一”, Ctrl+回车生成文字,
在取模方式项选择C51格式生成代码
关于Arduino&SSD1306OLED(IIC)显示的学习_第8张图片
复制代码代码添加到下面程序中

#include 
#include 
#include 
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
 
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
 
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
/*-----显示文字一,把代码放入数组中---*/
static const uint8_t PROGMEM Strong_16x16[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0C,0x00,0x0E,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,
};
void setup()   {                
  Serial.begin(9600);
  delay(500);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64) 
}
void loop() {
  test_SSD1306();
}
void test_SSD1306(void){
  display.clearDisplay();   // clears the screen and buffer
  display.drawBitmap(16,16,Strong_16x16,16,16,WHITE);   //(16,16)起点坐标,
  display.display();
  delay(2000);
}

其中display.drawBitmap(16,16,Strong_16x16,16,16,WHITE);
括号里面存放的依次是 起点坐标(16,16),Strong_16x16,显示区域大小(16 * 16),颜色
关于Arduino&SSD1306OLED(IIC)显示的学习_第9张图片
起点为(0,0)
关于Arduino&SSD1306OLED(IIC)显示的学习_第10张图片
起点为(16,16)
关于Arduino&SSD1306OLED(IIC)显示的学习_第11张图片
显示区域为(32,16)
关于Arduino&SSD1306OLED(IIC)显示的学习_第12张图片
显示区域为(128,64),实际为一堆乱码
关于其他文字显示出错的问题,原因是取模软件取模不正确需要在参数设置里面修改关于Arduino&SSD1306OLED(IIC)显示的学习_第13张图片
观察显示的文字大小是否为16*16,如果不是,选择‘文字输入区字体选择’更改字体
关于Arduino&SSD1306OLED(IIC)显示的学习_第14张图片
如果还有问题点击 其它选项更改取模方式
关于Arduino&SSD1306OLED(IIC)显示的学习_第15张图片
这样基本上就能解决显示出错的问题。

  1. 多字显示
    多字显示跟单字显示类似,用取模软件分别提取出文字的代码
#include 
#include 
#include 
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
 
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
 
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
static const uint8_t PROGMEM Welcome_16x16[] ={
0x00,0x80,0x00,0x80,0xFC,0x80,0x04,0xFC,0x05,0x04,0x49,0x08,0x2A,0x40,0x14,0x40,
0x10,0x40,0x28,0xA0,0x24,0xA0,0x45,0x10,0x81,0x10,0x02,0x08,0x04,0x04,0x08,0x02,//欢0
0x00,0x00,0x20,0x80,0x13,0x3C,0x12,0x24,0x02,0x24,0x02,0x24,0xF2,0x24,0x12,0x24,
0x12,0x24,0x12,0xB4,0x13,0x28,0x12,0x20,0x10,0x20,0x28,0x20,0x47,0xFE,0x00,0x00,//迎1
0x01,0x00,0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x11,0x10,0x09,0x10,0x09,0x20,
0xFF,0xFE,0x03,0x80,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,0x01,0x00,0x01,0x00,//来2
0x00,0x04,0xFF,0x84,0x08,0x04,0x10,0x24,0x22,0x24,0x41,0x24,0xFF,0xA4,0x08,0xA4,
0x08,0x24,0x08,0x24,0x7F,0x24,0x08,0x24,0x08,0x04,0x0F,0x84,0xF8,0x14,0x40,0x08,//到3
0x00,0x40,0x00,0x40,0x10,0x40,0x08,0x40,0x08,0x40,0x00,0x40,0x01,0x40,0x00,0x80,//守4
0x10,0x00,0x08,0x7C,0xFF,0x44,0x20,0x7C,0x20,0x44,0x26,0x7C,0x38,0x44,0x20,0x94,
0x01,0x08,0x7F,0xFC,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,//望5
0x01,0x00,0x11,0x00,0x11,0x00,0x1F,0xF8,0x21,0x00,0x41,0x00,0x01,0x00,0xFF,0xFE,
0x04,0x40,0x04,0x40,0x04,0x40,0x08,0x40,0x08,0x42,0x10,0x42,0x20,0x3E,0xC0,0x00,//先6
0x10,0x40,0x10,0x40,0x3C,0xFC,0x20,0x88,0x41,0x50,0xBC,0x20,0x10,0xD8,0x13,0x26,
0xFC,0xF8,0x10,0x20,0x10,0xF8,0x10,0x20,0x17,0xFE,0x18,0x20,0x10,0x20,0x00,0x20,//锋7

}; 

static const uint8_t PROGMEM Author_16x16[] ={
0x00,0x00,0x03,0x80,0x01,0xC0,0x30,0xFC,0x3F,0xFE,0x71,0xDE,0x70,0xC0,0x01,0xFC,
0x3F,0xE0,0x0C,0xC0,0x0E,0xC0,0x06,0xC0,0x00,0xC0,0x03,0xC0,0x03,0xC0,0x01,0x80,
0x00,0x00,0x0C,0x00,0x0E,0xFC,0x03,0xCC,0x7F,0xFC,0x38,0xCC,0x1F,0xFC,0x1F,0xFC,
0x39,0xBC,0x07,0xF0,0x01,0x80,0x07,0xF0,0x01,0x80,0x07,0xFE,0x3F,0xFE,0x00,0x00,
0x00,0x00,0x03,0x80,0x01,0x80,0x0F,0x80,0x0F,0xF0,0x0F,0xE0,0x19,0x98,0x17,0xFC,
0x7F,0xC0,0x07,0xC0,0x06,0xC0,0x0C,0xC6,0x18,0xC6,0x70,0xFF,0x00,0x7C,0x00,0x00,
0x00,0x00,0x0C,0xE0,0x0E,0xF0,0x1C,0xF8,0x1F,0xF0,0x3B,0x70,0x3E,0xFC,0x7D,0xFF,
0xDF,0xF0,0x79,0xF0,0x1E,0x7C,0x1F,0xF0,0x1C,0x60,0x18,0x60,0x00,0x60,0x00,0x60,
};
void setup()   {                
  Serial.begin(9600);
  delay(500);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64) 
  
  void loop() {
  test_SSD1306();
}
void test_SSD1306(void){
  display.clearDisplay();   // clears the screen and buffer
  display.ShowCN_16(0,0, Welcome_16x16,sizeof(Welcome_16x16)/32,WHITE);
  display.ShowCN_16(48,16, Author_16x16,sizeof(Author_16x16)/32,WHITE);    
  display.display();
  delay(2000); 
}
}

显示效果
关于Arduino&SSD1306OLED(IIC)显示的学习_第16张图片

图片的显示

图片的显示与文字显示基本相同,具体步骤如下:

  1. 选择显示的图片

  2. 将图片的大小和格式更改为128*64,单色
    在这里插入图片描述

  3. 打开取模软件,选择在这里插入图片描述
    打开图片,然后生成代码
    关于Arduino&SSD1306OLED(IIC)显示的学习_第17张图片
    复制代码移植到程序中

#include 
#include 
#include 
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
 
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
 
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//显示图片
static const uint8_t PROGMEM photo_128x64[] = {

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,
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,0x03,0x80,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x07,0x80,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xE0,0x00,0x00,0x3F,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x7F,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3F,0xE0,0x00,0x00,0x3F,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3F,0xE0,0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0xC0,0x05,0x80,0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0x80,0x0D,0x80,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0x80,0x0D,0x80,0x07,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x1D,0xC0,0x07,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x1D,0xC0,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x3D,0xE0,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x3D,0xE0,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x3D,0xF0,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x7D,0xF0,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0xFD,0xF8,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x01,0xFD,0xFC,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x03,0xFD,0xFE,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x07,0xFD,0xFF,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFE,0x0F,0xFD,0xFF,0xC3,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xFF,0x1F,0xF8,0xFF,0xC7,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0x3F,0xF0,0x7F,0xF7,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xE0,0x3F,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xC0,0x1F,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0x80,0x0F,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xFE,0x00,0x03,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xFC,0x00,0x01,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0F,0xFE,0x00,0x01,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0x00,0x07,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xF0,0x3F,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xC0,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,0x01,0xFD,0xC3,0xFF,0x7F,0x71,0x9C,0xCF,0xFB,0xF3,0x8E,0x00,0x00,0x00,
0x00,0x00,0x03,0xFF,0xE3,0xFF,0x7F,0xFB,0x9D,0xEF,0xFF,0xFB,0x8E,0x00,0x00,0x00,
0x00,0x00,0x03,0xFF,0xE7,0xFF,0x7F,0xFB,0xFB,0xFF,0xFF,0xFB,0x8E,0x00,0x00,0x00,
0x00,0x00,0x03,0x87,0xFF,0x7E,0x7F,0xFF,0xFB,0xF1,0xC7,0x1B,0xFE,0x00,0x00,0x00,
0x00,0x00,0x03,0x87,0x7E,0x7E,0x7F,0x9F,0xF7,0xF9,0xCF,0x1B,0xFE,0x00,0x00,0x00,
0x00,0x00,0x03,0xFF,0x7E,0x7F,0x7F,0x9E,0xF7,0x3D,0xC7,0xFB,0x8E,0x00,0x00,0x00,
0x00,0x00,0x03,0xFF,0x3C,0x7F,0xF7,0xDE,0xFE,0x1F,0xC7,0xFB,0x8E,0x00,0x00,0x00,
0x00,0x00,0x00,0xFE,0x18,0x7F,0x71,0xEC,0x6E,0x1F,0xC3,0xF3,0x8E,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,
void setup()   {                
  Serial.begin(9600);
  delay(500);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64) 
}
 
void loop() {
  test_SSD1306();
}
 
void test_SSD1306(void){
  display.clearDisplay();   // clears the screen and buffer
  display.drawBitmap(0,0,photo_128x64,128,62,WHITE);
  display.display();
  delay(2000);

};

效果图
关于Arduino&SSD1306OLED(IIC)显示的学习_第18张图片

OLED 显示串口信息

OLED 实时读取串口信息,可应用于诸多适用场合,方便人们在第一时间识别和处理一些信息
(需要串口助手)

#include 
#include 
#include 
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
 
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
 
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
 
void setup()  
{                
  Serial.begin(9600);
  delay(500);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64) 
}
 
void loop() 
{
	String inString = "";
	while (Serial.available()>0){
	char inChar = Serial.read();      //
	inString += (char)inChar;
	delay(10);
}
  
if(inString!="")
{
	display.setTextSize(2);		//设置字体大小
	display.setTextColor(WHITE);	//设置字体颜色白色
	display.setCursor(36,25);	//设置字体的起始位置
	display.println(inString);	//显示输入数据
	display.display();	//显示信息
	delay (1000);  
	display.clearDisplay();
  }

程序参考于https://blog.csdn.net/weixin_40825989/article/details/81148177
效果图
关于Arduino&SSD1306OLED(IIC)显示的学习_第19张图片
关于Arduino&SSD1306OLED(IIC)显示的学习_第20张图片

你可能感兴趣的:(Arduino)