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

发布了13 篇原创文章 · 获赞 5 · 访问量 1012

猜你喜欢

转载自blog.csdn.net/weixin_44868057/article/details/104977613
今日推荐