上一期教程:【Arduino uno教程 】(七)0.96寸OLED屏幕
上一期教程讲了0.96OLED屏幕使用u8g2库显示英文字符,以及基础的画图
这一期教程:① 用软件取图片,②使用取模软件取中文字符
(二)显示图片
①由于屏幕比较小,所以我们能取模的图片也是比较小的
这里我推荐阿里巴巴矢量图标库,里面的图片可以免费下载,而且做的很好
地址:https://www.iconfont.cn/
②我们将喜欢的图标下载
选择png下载
③然后使用在线网站,将png转成jpg,因为后面使用的取模软件只能上传jpg文件
png转jpg网站:https://png2jpg.com/zh/
④使用Image2Lcd 2.9软件,将jpg文件转换成bmp文件
软件下载:https://wwmg.lanzouj.com/iQiox2fx8lxi
注意宽度和高度最好设置16*16或者32*32,不要过大
设置完之后,要按一下重新载入
⑤将转换之后的bmp文件,用PCtoLCD2002取模软件打开
PCtoLCD2002下载地址:https://wwmg.lanzouj.com/ivUxr2fx90yj
模式选择图形模式
将bmp文件上传后,点一下齿轮
按照我的设置即可
然后点一下,生成字模
- 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
- 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,"C:\Users\XEMOWO\Desktop\liwu.bmp",0
⑥arduino
复制粘贴下面的变量格式,“liwu”为图片名字,可以随意更改
- const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
- };
将刚刚生成的字模粘贴下来,注意把后面的注释删了
- const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
- 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
- 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,
- };
然后就可以调用图片了,我们用到u8g2的 u8g2.drawXBMP函数
- u8g2.drawXBMP(2, 2, 16, 16, liwu);
⑦效果演示
⑧完整代码
- #include <Arduino.h>
- #include <U8g2lib.h>
- #include <Wire.h>
- #define BOARD_I2C_SCL A5
- #define BOARD_I2C_SDA A4
- U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);
- const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
- 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
- 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,
- };
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println("Init u8g2 ....");
- u8g2.begin();
- u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体
- delay(100);
- }
- void loop() {
- u8g2.clearBuffer(); //清除缓存
- Serial.println("u8g2 showing chars ....");
- u8g2.drawXBMP(2, 2, 16, 16, liwu);
- u8g2.sendBuffer();
- delay(1000);
- }
-
(三)显示中文字符
①使用PCtoLCD2002
将模式选择字符模式
点击齿轮,按照我的图片设置选项
输入生成的文字,如“温湿度”,然后点击生成字模
②arduino
复制格式
- uint8_t day[][32] =
- {
- };
③将生成的字模粘贴到格式内
注意将每个字,第一行末尾的括号},和第二行开头的括号{,删掉
- uint8_t day[][32] =
- {
- {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
- 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/
- {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
- 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/
- {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
- 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/
- };
④使用 u8g2.drawXBM函数,注意和上面图片教程的函数不一样!
u8g2.drawXBM(2,2,16,16,day[0]);
第一第二位是显示的x,y
第三第四位显示文字的大小16*16
最后一位是函数内,字的序号
- u8g2.drawXBM(2,2,16,16,day[0]);
- u8g2.drawXBM(18,2,16,16,day[2]);
- u8g2.drawXBM(2,30,16,16,day[1]);
- u8g2.drawXBM(18,30,16,16,day[2]);
⑤效果演示
⑥完整代码
- #include <Arduino.h>
- #include <U8g2lib.h>
- #include <Wire.h>
- #define BOARD_I2C_SCL A5
- #define BOARD_I2C_SDA A4
- U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ BOARD_I2C_SCL, /* data=*/ BOARD_I2C_SDA, /* reset=*/ U8X8_PIN_NONE);
- const unsigned char liwu[32] U8X8_PROGMEM = {//礼物图片
- 0x00,0x00,0x70,0x0E,0xC8,0x0B,0xFE,0x7F,0x02,0x40,0x02,0x40,0x02,0x40,0xFE,0x7F,
- 0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0x84,0x20,0xFC,0x3F,0x00,0x00,
- };
- uint8_t day[][32] =
- {
- {0x00,0x00,0xC4,0x1F,0x48,0x10,0x48,0x10,0xC1,0x1F,0x42,0x10,0x42,0x10,0xC8,0x1F,
- 0x08,0x00,0xE4,0x3F,0x27,0x25,0x24,0x25,0x24,0x25,0x24,0x25,0xF4,0x7F,0x00,0x00},/*"温",0*/
- {0x00,0x00,0xE4,0x1F,0x28,0x10,0x28,0x10,0xE1,0x1F,0x22,0x10,0x22,0x10,0xE8,0x1F,
- 0x88,0x04,0x84,0x04,0x97,0x24,0xA4,0x14,0xC4,0x0C,0x84,0x04,0xF4,0x7F,0x00,0x00},/*"湿",1*/
- {0x80,0x00,0x00,0x01,0xFC,0x7F,0x44,0x04,0x44,0x04,0xFC,0x3F,0x44,0x04,0x44,0x04,
- 0xC4,0x07,0x04,0x00,0xF4,0x0F,0x24,0x08,0x42,0x04,0x82,0x03,0x61,0x0C,0x1C,0x70},/*"度",2*/
- };
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println("Init u8g2 ....");
- u8g2.begin();
- u8g2.setFont(u8g2_font_ncenB08_tr); //设置字体
- delay(100);
- }
- void loop() {
- u8g2.clearBuffer(); //清除缓存
- Serial.println("u8g2 showing chars ....");
- u8g2.drawXBM(2,2,16,16,day[0]);
- u8g2.drawXBM(18,2,16,16,day[2]);
- u8g2.drawXBM(2,30,16,16,day[1]);
- u8g2.drawXBM(18,30,16,16,day[2]);
- u8g2.sendBuffer();
- delay(1000);
- }
-