Arduino Uno 驱动OLED进阶 显示中英文字

level
发布于 2020-11-6 17:48
浏览
0收藏

Arduino Uno 驱动OLED进阶 显示中英文字-鸿蒙开发者社区

实现文字的显示
这代码上实现在指定的坐标显示指定的中英文字

 

先看看效果图

Arduino Uno 驱动OLED进阶 显示中英文字-鸿蒙开发者社区

相关开源代码:

//显示中英文字符程序
 
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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 != 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, 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() {
 
}
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 != 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, 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() {

}

可能有人好奇这些乱七八糟的怎么来的?↓

Arduino Uno 驱动OLED进阶 显示中英文字-鸿蒙开发者社区

这其实就用到  字模提取V2.2  的软件了 ,打开软件,具体操作如下:

 

Arduino Uno 驱动OLED进阶 显示中英文字-鸿蒙开发者社区

把点阵生成的数组,套入程序的相关变量,调用相关的函数就可以实现显示中文。

 

其实取模软件,就是把中文字转换成点阵,实现在没有中文字库程序中显示中文。

我想有字库的也是这样的点阵库,如有理解错误请指出。

 

原文作者:ling3ye

分类
已于2020-11-6 17:48:15修改
收藏
回复
举报
回复
    相关推荐