编写程序void OLED_ShowChinese(u8 x,u8 y,u8 chr,u8 size,u8 mode)实现在oled上显示汉字
时间: 2024-01-22 15:17:18 浏览: 94
这是一个基于Arduino平台的OLED显示屏库,可以在OLED上显示汉字:
```
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);
// 字库数组
const unsigned char F8X16[] PROGMEM = {
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",0*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",1*/
};
void OLED_Init() {
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
}
void OLED_ShowChinese(unsigned char x, unsigned char y, unsigned char chr, unsigned char size, unsigned char mode) {
unsigned char c = 0, i = 0;
unsigned char ch = chr - ' ';
unsigned char chi = ch * 16;
unsigned char x0 = x, y0 = y;
if (size == 16) {
for (i = 0; i < 16; i++) {
c = pgm_read_byte(&F8X16[chi + i]);
for (unsigned char j = 0; j < 8; j++) {
if ((c >> (7 - j)) & 0x01) {
display.drawPixel(x + j, y + i, 1);
}
else {
if (mode) display.drawPixel(x + j, y + i, 0);
}
}
}
}
display.display();
}
void setup() {
OLED_Init();
}
void loop() {
OLED_ShowChinese(0, 0, '中', 16, 1);
delay(5000);
OLED_ShowChinese(20, 0, '国', 16, 1);
delay(5000);
}
```
这个程序中使用了F8X16字库,可以在16x16的区域中显示汉字。可以根据实际情况修改字库和代码。
阅读全文