Arduino SSD1306 OLED IIC

1、 前期準備

arduino UNO開發板
OLED 顯示屏
導線若干
取模軟件zimoV2.2
這裏推薦一個在線圖片取模軟件
http://tools.clz.me/

2、引腳接線

OLED 顯示屏有四個引腳,分別是:
SDA(數據線) SCK(時鐘線) VDD(3.3V) GND
在UNO開發板上I2C接口,SDA對應D4,SCK對應D5
在MEGA2560開發板上I2C接口,SDA對應D20, SCL對應D21

3、通信地址

        模塊背面的IIC ADRESSSELECT表示該模塊在IIC通信作爲從機時的地址,當中間的腳用電阻和左邊接起來時,地址爲0x78,當和右邊接起來時,地址爲0x7A。

這款屏幕尺寸約爲0.96英寸,由SSD1306驅動,驅動接口I2C,I2C地址(0x3c 默認/0x3d)。
不同型號有不同的通信地址,可通過程序查詢:

#include <Wire.h>  
   
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板上,打開串口可查看地址

串口
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 <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#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; i<display.height(); i+=2) {
    display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
    display.display();
  }
}

void testfillrect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<display.height()/2; i+=3) {
    // alternate colors
    display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
    display.display();
    color++;
  }
}

void testdrawtriangle(void) {
  for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
    display.drawTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    display.display();
  }
}

void testfilltriangle(void) {
  uint8_t color = WHITE;
  for (int16_t i=min(display.width(),display.height())/2; i>0; 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<display.height()/2-2; i+=2) {
    display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
    display.display();
  }
}

void testfillroundrect(void) {
  uint8_t color = WHITE;
  for (int16_t i=0; i<display.height()/2-2; i+=2) {
    display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
  }
}
   
void testdrawrect(void) {
  for (int16_t i=0; i<display.height()/2; i+=2) {
    display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
    display.display();
  }
}

void testdrawline() {  
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, 0, i, display.height()-1, WHITE);
    display.display();
  }
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(0, 0, display.width()-1, i, WHITE);
    display.display();
  }
  delay(250);
  
  display.clearDisplay();
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, display.height()-1, i, 0, WHITE);
    display.display();
  }
  for (int16_t i=display.height()-1; 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<display.height(); i+=4) {
    display.drawLine(display.width()-1, 0, 0, i, WHITE);
    display.display();
  }
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE); 
    display.display();
  }
  delay(250);
}

void testscrolltext(void) {
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  display.clearDisplay();
  display.println("scroll");
  display.display();
 
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);    
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
}

程序與函數講解
以下面簡單顯示程序爲例

#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#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);
  }

關於顯示,首先要建立座標概念

在這裏插入圖片描述

這種型號的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    */

至於圖片的靜態和動態顯示問題,在稍後學習之後再做更新
文字的顯示
單文字顯示
文字的顯示需要運用取模軟件取模,這裏用的是zimoV2.2

打開軟件

在這裏插入圖片描述

以文字“一”爲例

在這裏插入圖片描述
新建一個16*16的像素塊
在文字區輸入 漢字“一”, Ctrl+回車生成文字,
在取模方式項選擇C51格式生成代碼

在這裏插入圖片描述

複製代碼代碼添加到下面程序中

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#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),顏色

在這裏插入圖片描述起點爲(0,0)

在這裏插入圖片描述起點爲(16,16)


關於其他文字顯示出錯的問題,原因是取模軟件取模不正確需要在參數設置裏面修改

在這裏插入圖片描述
觀察顯示的文字大小是否爲16*16,如果不是,選擇‘文字輸入區字體選擇’更改字體

在這裏插入圖片描述

如果還有問題點擊 其它選項更改取模方式在這裏插入圖片描述

這樣基本上就能解決顯示出錯的問題。

多字顯示
多字顯示跟單字顯示類似,用取模軟件分別提取出文字的代碼
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#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); 
}
}


顯示效果

顯示效果

 

圖片的顯示
圖片的顯示與文字顯示基本相同,具體步驟如下:

選擇顯示的圖片

將圖片的大小和格式更改爲128*64,單色

在這裏插入圖片描述


打開取模軟件,選擇打開圖片,然後生成代碼

在這裏插入圖片描述

複製代碼移植到程序中

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#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);

};


效果圖在這裏插入圖片描述


OLED 顯示串口信息
OLED 實時讀取串口信息,可應用於諸多適用場合,方便人們在第一時間識別和處理一些信息
(需要串口助手)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#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
效果圖

顯示效果在這裏插入圖片描述
————————————————
 

圖片發自簡書App

用windows畫圖軟件繪圖,大小設置爲128 * 64
保存爲黑白BMP圖片

打開“字模提取v2.2”軟件,

 

image.png

按照圖片所示設置參數

 

 

最後選擇C51格式

 

複製代碼,替換 XBM示例文件中相應的部分

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章