arduino+oled顯示字

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

首先下載一個外部庫u8glib
然後去在這裏插入圖片描述
在這裏插入圖片描述

#include "U8glib.h"
 
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);      // I2C / TWI
 
void draw(void) {
 
// graphic commands to redraw the complete screen should be placed here
 
u8g.setFont(u8g_font_unifont);
 
//u8g.setFont(u8g_font_osb21);
 
u8g.drawStr( 0, 22, "Hello World!");
 
}
 
void setup(void) {
 
}
 
void loop(void) {
 
// picture loop
 
u8g.firstPage();
 
do {
 
draw();
 
} while( u8g.nextPage() );
 
// rebuild the picture after some delay
 
delay(50);
 
}

在這裏插入圖片描述
說明U8glib的庫成功了
然後在導入

#include "U8glib.h"
 
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);      // I2C / TWI
 
void draw(void) {
 
// graphic commands to redraw the complete screen should be placed here
 
u8g.setFont(u8g_font_unifont);
 
//u8g.setFont(u8g_font_osb21);
 
u8g.drawStr( 0, 22, "Hello World!");
 
}
 
void setup(void) {
 
}
 
void loop(void) {
 
// picture loop
 
u8g.firstPage();
 
do {
 
draw();
 
} while( u8g.nextPage() );
 
// rebuild the picture after some delay
 
delay(50);
 
}
 

改代碼時通過點陣將自己的名字換成點陣即可
在這裏插入圖片描述
一個名字佔17個
在這裏插入圖片描述

//顯示中英文字符程序
 
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 #include "U8glib.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
#define LOGO16_GLCD_HEIGHT 16 //定義顯示高度
#define LOGO16_GLCD_WIDTH  16 //定義顯示寬度
 
//中文:凌  (這就是存儲點陣變量,str_1可以修改成自己喜歡的名稱,用函數display.drawBitmap()調用就可以了)
static const unsigned char PROGMEM str_1[] =
{
0x00,0x40,0x40,0x40,0x23,0xF8,0x20,0x40,0x00,0x40,0x07,0xFE,0x11,0x10,0x12,0x88,
0x24,0x84,0xE1,0xF0,0x23,0x10,0x24,0xA0,0x20,0x40,0x20,0xA0,0x23,0x10,0x0C,0x0C
  };
 
//中文:順
static const unsigned char PROGMEM str_2[] =
{
0x04,0x00,0x45,0xFE,0x54,0x20,0x54,0x40,0x55,0xFC,0x55,0x04,0x55,0x24,0x55,0x24,
0x55,0x24,0x55,0x24,0x55,0x24,0x55,0x44,0x54,0x50,0x54,0x88,0x85,0x04,0x06,0x02
  };
 
//中文:實
static const unsigned char PROGMEM str_3[] =
{
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x84,0x04,0x80,0x04,0x80,0x10,0x80,
0x08,0x80,0x08,0x80,0xFF,0xFE,0x01,0x40,0x02,0x20,0x04,0x10,0x18,0x08,0x60,0x04
  };
 
//中文:驗
static const unsigned char PROGMEM str_4[] =
{
0x00,0x20,0xF8,0x20,0x08,0x50,0x48,0x50,0x48,0x88,0x49,0x04,0x4A,0xFA,0x7C,0x00,
0x04,0x44,0x04,0x24,0x1D,0x24,0xE4,0xA8,0x44,0x88,0x04,0x10,0x2B,0xFE,0x10,0x00
  };
 
//中文:室
static const unsigned char PROGMEM str_5[] =
{
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x3F,0xF8,0x04,0x00,0x08,0x20,
0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00
  };
 
#if (SSD1306_LCDHEIGHT != 32)
#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, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
 
  display.clearDisplay();
 
  //英文字符顯示
  display.setTextSize(1);             //設置字體大小
  display.setTextColor(WHITE);        //設置字體顏色白色
  display.setCursor(0,0);             //設置字體的起始位置
  display.println("Hello, world!");   //輸出字符並換行
 
  display.setTextColor(BLACK, WHITE); //設置字體黑色,字體背景白色
  display.println(3.141592);          //輸出數字並換行
 
  display.setTextSize(2);             //設置字體大小
  display.setTextColor(WHITE);        //設置字體白色
  display.print("0x");                //輸出字符
  display.println(0xDEADBEEF, HEX);   //輸出爲ASCII編碼的十六進制
  //display.display();                  //顯示以上
 
  //中文字符顯示
  display.drawBitmap(26, 32, str_1, 16, 16, 1); //在座標X:26  Y:16的位置顯示中文字符凌
  display.drawBitmap(42, 32, str_2, 16, 16, 1); //在座標X:42  Y:16的位置顯示中文字符順
  display.drawBitmap(58, 32, str_3, 16, 16, 1);
  display.drawBitmap(74, 32, str_4, 16, 16, 1);
  display.drawBitmap(90, 32, str_5, 16, 16, 1);
  display.display();                  //把緩存的都顯示
}
 
void loop() {
 
}

參考博客:
https://blog.csdn.net/qq_42860728/article/details/84310160
https://blog.csdn.net/MENGHUANBEIKE/article/details/75666266
https://blog.csdn.net/ling3ye/article/details/53399305

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