Arduino网络编程实战-OLED显示中文

OLED显示中文

1、OLED介绍

OLED显示屏是指有机电激发光二极管(OrganicLight-EmittingDiode,OLED)由于同时具备自发光,不需背光源、对比度高、厚度薄、视角广、反应速度快、可用于挠曲性面板、使用温度范围广、构造及制程较简单等优异之特性,被认为是下一代的平面显示器新兴应用技术。

在前面的文章中,对OLED做了详细的介绍,请参考:

本实例将详细介绍如果在OLED中显示中文。

1、硬件准备

  • Arduino Mego2560开发板一块
  • SSD1306 I2C OLED显示屏一块
  • 数据线一条
  • 杜邦线若干

OLED与Arduino连线如下:

  • OLED:VCC <–> Arduino Mega2560 3.3V
  • OLED:GND <–> Arduino Mega2560 GND
  • OLED:SDA <–> Arduino Mega2560 SDA(20引脚)
  • OLED:SCL <–> Arduino Mega2560 SCL(21引脚)

2、软件准备

3、中文字符数据制作

1)启动PctoLCD2002软件,进入字符模式

在这里插入图片描述

2)输入需要使用的中文字符

在这里插入图片描述

3)生成中文字符数据设置

在这里插入图片描述

4)生成中文字符数据

在这里插入图片描述

最后保存字模数据,或复制到代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const uint8\_t cn_huan [] PROGMEM =
{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\*/

const uint8\_t cn_ying [] PROGMEM =
{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\*/

const uint8\_t cn_shi [] PROGMEM =
{0x10,0x40,0x10,0x40,0x1F,0xFE,0x20,0x40,0x20,0x40,0x67,0xFC,0x64,0x44,0xA4,0x44,
0x27,0xFC,0x20,0x40,0x22,0x40,0x21,0x40,0x20,0x80,0x21,0x40,0x22,0x30,0x2C,0x0E};/\*"使",2\*/

const uint8\_t cn_yong [] PROGMEM =
{0x00,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,
0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x41,0x08,0x41,0x28,0x80,0x10};/\*"用",3\*/

const uint8\_t cn_tian [] PROGMEM =
{0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,
0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06};/\*"天",4\*/

const uint8\_t cn_qi [] PROGMEM =
{0x10,0x00,0x10,0x00,0x3F,0xFC,0x20,0x00,0x4F,0xF0,0x80,0x00,0x3F,0xF0,0x00,0x10,
0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x0A,0x00,0x0A,0x00,0x06,0x00,0x02};/\*"气",5\*/

const uint8\_t cn_yu [] PROGMEM =
{0x00,0x00,0xF9,0xFE,0x08,0x20,0x50,0x40,0x21,0xFC,0x11,0x04,0xFD,0x24,0x25,0x24,
0x29,0x24,0x21,0x24,0x21,0x24,0x21,0x44,0x20,0x50,0x20,0x88,0xA1,0x04,0x42,0x02};/\*"预",6\*/

const uint8\_t cn_bao [] PROGMEM =
{0x10,0x00,0x11,0xFC,0x11,0x04,0x11,0x04,0xFD,0x14,0x11,0x08,0x11,0x00,0x15,0xFC,
0x19,0x44,0x31,0x44,0xD1,0x28,0x11,0x28,0x11,0x10,0x11,0x28,0x51,0x44,0x21,0x82};/\*"报",7\*/

const int cn_allArray_LEN = 8;
const uint8\_t\* cn_allArray[8] = {
cn_huan,cn_ying,cn_shi,cn_yong,cn_tian,cn_qi,cn_yu,cn_bao
};


4、代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <SPI.h>
#include <Wire.h>
#include <Adafruit\_GFX.h>
#include <Adafruit\_SSD1306.h>
#include <SdFat.h>
#include <ArduinoJson.h>

#define SCREEN\_WIDTH 128 // OLED display width, in pixels
#define SCREEN\_HEIGHT 64 // OLED display height, in pixels

#define LOGO\_HEIGHT 32
#define LOGO\_WIDTH 32

#define OLED\_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN\_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const uint8\_t cn_huan [] PROGMEM =
{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\*/

const uint8\_t cn_ying [] PROGMEM =
{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\*/

const uint8\_t cn_shi [] PROGMEM =
{0x10,0x40,0x10,0x40,0x1F,0xFE,0x20,0x40,0x20,0x40,0x67,0xFC,0x64,0x44,0xA4,0x44,
0x27,0xFC,0x20,0x40,0x22,0x40,0x21,0x40,0x20,0x80,0x21,0x40,0x22,0x30,0x2C,0x0E};/\*"使",2\*/

const uint8\_t cn_yong [] PROGMEM =
{0x00,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,
0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x41,0x08,0x41,0x28,0x80,0x10};/\*"用",3\*/

const uint8\_t cn_tian [] PROGMEM =
{0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,
0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06};/\*"天",4\*/

const uint8\_t cn_qi [] PROGMEM =
{0x10,0x00,0x10,0x00,0x3F,0xFC,0x20,0x00,0x4F,0xF0,0x80,0x00,0x3F,0xF0,0x00,0x10,
0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x0A,0x00,0x0A,0x00,0x06,0x00,0x02};/\*"气",5\*/

const uint8\_t cn_yu [] PROGMEM =
{0x00,0x00,0xF9,0xFE,0x08,0x20,0x50,0x40,0x21,0xFC,0x11,0x04,0xFD,0x24,0x25,0x24,
0x29,0x24,0x21,0x24,0x21,0x24,0x21,0x44,0x20,0x50,0x20,0x88,0xA1,0x04,0x42,0x02};/\*"预",6\*/

const uint8\_t cn_bao [] PROGMEM =
{0x10,0x00,0x11,0xFC,0x11,0x04,0x11,0x04,0xFD,0x14,0x11,0x08,0x11,0x00,0x15,0xFC,
0x19,0x44,0x31,0x44,0xD1,0x28,0x11,0x28,0x11,0x10,0x11,0x28,0x51,0x44,0x21,0x82};/\*"报",7\*/

const int cn_allArray_LEN = 8;
const uint8\_t\* cn_allArray[8] = {
cn_huan,cn_ying,cn_shi,cn_yong,cn_tian,cn_qi,cn_yu,cn_bao
};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

// SSD1306\_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}

// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); //设置字体大小
display.setTextColor(SSD1306_WHITE);//开像素点发光

// testdrawbitmap(cn\_allArray[0],16,16);
for(int i = 0;i < 8;i++){
display.drawBitmap(i \* 16, 0, cn_allArray[i], 16, 16, 1);
delay(1);
}
display.display();
/\*
display.drawBitmap(32, 32, str1, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(48, 32, str2, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(64, 32, str3, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(80, 32, str4, 16, 16, 1); //画出字符对应点阵数据
display.display();//开显示 \*/
}

void loop() {
// put your main code here, to run repeatedly:

}

void testdrawbitmap(uint8\_t\* datas,int width,int height) {
//display.clearDisplay();
// display.fillRect((display.width() - width ) / 2,
// (display.height() - height) / 2,width, height,SSD1306\_BLACK);
display.drawBitmap(
(display.width() - width ) / 2,
(display.height() - height) / 2,
datas, width, height, 1);
display.display();
delay(1000);
}

5、运行结果

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/123025412