(一)简介
上一期教程:【Arduino uno教程 】(七)0.96寸OLED屏幕
上一期教程讲了0.96OLED屏幕使用u8g2库显示英文字符,以及基础的画图
这一期教程:① 用软件取图片,②使用取模软件取中文字符

(二)显示图片
①由于屏幕比较小,所以我们能取模的图片也是比较小的
这里我推荐阿里巴巴矢量图标库,里面的图片可以免费下载,而且做的很好
地址:https://www.iconfont.cn/
image.png


②我们将喜欢的图标下载
选择png下载
image.png


③然后使用在线网站,将png转成jpg,因为后面使用的取模软件只能上传jpg文件
png转jpg网站:https://png2jpg.com/zh/
image.png


④使用Image2Lcd 2.9软件,将jpg文件转换成bmp文件
软件下载:https://wwmg.lanzouj.com/iQiox2fx8lxi
注意宽度和高度最好设置16*16或者32*32,不要过大
设置完之后,要按一下重新载入
image.png


⑤将转换之后的bmp文件,用PCtoLCD2002取模软件打开
PCtoLCD2002下载地址:https://wwmg.lanzouj.com/ivUxr2fx90yj
模式选择图形模式
将bmp文件上传后,点一下齿轮
按照我的设置即可
然后点一下,生成字模
image.png
  1. 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
  2. 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,"C:\Users\XEMOWO\Desktop\liwu.bmp",0
image.png


⑥arduino
复制粘贴下面的变量格式,“liwu”为图片名字,可以随意更改
  1. const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片

  2. };

将刚刚生成的字模粘贴下来,注意把后面的注释删了
  1. const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
  2. 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
  3. 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,
  4. };

然后就可以调用图片了,我们用到u8g2的 u8g2.drawXBMP函数
  1. u8g2.drawXBMP(2, 2, 16, 16, liwu);
第一第二项是x,y的坐标,第三第四项是图片的宽高,宽高不能写错,最后一位是你设置图片的名称

⑦效果演示
99f1e6fca213a2ef0c209a4954a08f7.jpg


⑧完整代码
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3. #include <Wire.h>

  4. #define BOARD_I2C_SCL   A5
  5. #define BOARD_I2C_SDA   A4   

  6. U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);  

  7. const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
  8. 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
  9. 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,
  10. };


  11. void setup() {
  12.   // put your setup code here, to run once:
  13.   Serial.begin(115200);
  14.   Serial.println("Init u8g2 ....");
  15.   u8g2.begin();
  16.   u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体


  17.   delay(100);
  18. }

  19. void loop() {
  20.   u8g2.clearBuffer();  //清除缓存     
  21.   Serial.println("u8g2 showing chars ....");
  22.   u8g2.drawXBMP(2, 2, 16, 16, liwu);
  23.   u8g2.sendBuffer();
  24.   delay(1000);
  25. }





(三)显示中文字符
①使用PCtoLCD2002
将模式选择字符模式
点击齿轮,按照我的图片设置选项
image.png
81c2cddaf6574ff3c6034d6b6d8b166.png


输入生成的文字,如“温湿度”,然后点击生成字模
image.png


②arduino
复制格式
  1. uint8_t day[][32] =
  2. {

  3. };

③将生成的字模粘贴到格式内
注意将每个字,第一行末尾的括号},和第二行开头的括号{,删掉
  1. uint8_t day[][32] =
  2. {
  3. {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
  4. 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/

  5. {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
  6. 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/

  7. {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
  8. 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/

  9. };


④使用  u8g2.drawXBM函数,注意和上面图片教程的函数不一样!
  u8g2.drawXBM(2,2,16,16,day[0]);
第一第二位是显示的x,y
第三第四位显示文字的大小16*16
最后一位是函数内,字的序号
  1.   u8g2.drawXBM(2,2,16,16,day[0]);
  2.   u8g2.drawXBM(18,2,16,16,day[2]);

  3.   u8g2.drawXBM(2,30,16,16,day[1]);
  4.   u8g2.drawXBM(18,30,16,16,day[2]);


⑤效果演示
d86624a3b26803c4f6d81ef0b24e974.jpg


⑥完整代码
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3. #include <Wire.h>

  4. #define BOARD_I2C_SCL   A5
  5. #define BOARD_I2C_SDA   A4   

  6. U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);  

  7. const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
  8. 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
  9. 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,
  10. };

  11. uint8_t day[][32] =
  12. {
  13. {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
  14. 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/

  15. {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
  16. 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/

  17. {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
  18. 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/

  19. };




  20. void setup() {
  21.   // put your setup code here, to run once:
  22.   Serial.begin(115200);
  23.   Serial.println("Init u8g2 ....");
  24.   u8g2.begin();
  25.   u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体


  26.   delay(100);
  27. }

  28. void loop() {
  29.   u8g2.clearBuffer();  //清除缓存     
  30.   Serial.println("u8g2 showing chars ....");

  31.   u8g2.drawXBM(2,2,16,16,day[0]);
  32.   u8g2.drawXBM(18,2,16,16,day[2]);

  33.   u8g2.drawXBM(2,30,16,16,day[1]);
  34.   u8g2.drawXBM(18,30,16,16,day[2]);

  35.   u8g2.sendBuffer();
  36.   delay(1000);
  37. }