arduino如何在ssd1306上显示中文字符

一、前言      

arduino的强大之处是在于它有很多开源库支持,一个不懂硬件的人可以轻易上手。它支持各种各样的外设,有各种通讯接口,那么我们可以无限发挥自己的创意,只要你能想得出来的,arduino都可以实现。本篇文章是讲述arduino怎样驱动oled 1306,使用的接口是I2C。


二、准备硬件和软件

     1.准备一块ssd1306液晶显示模块,一块arduino开发板。

     2.准备arduino IDE,下载地址在:https://www.arduino.cc/

     3.下载ssd 1306的支持库u8g2:下载地址在:https://github.com/olikraus/u8g2  利用命令可以下载下来:

git clone https://github.com/olikraus/u8g2.git
或者在arduino上安装u8g2:(1)打开管理库:

(2)搜索并安装u8b2

三、用arduino驱动oled显示屏

       (1)接线。我用的是esp32,这个1306的屏幕是使用i2c,我只需在esp32上选取两个引脚作为i2c即可,而1306需要3.3v供电,再接两根线供电。

      (2)打开u8b2的例程。

      (3)配置工程的引脚和通讯方式。只需要根据屏幕的通讯方式和接的I2C引脚配置即可,配置方式是选择对应的配置去掉注释。我用的是SSD1306_128X64,用了I2C接口,使用23和22作为i2c的引脚,所以我配置是:

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 23, /* data=*/ 22, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
代码如下:

void setup(void) {
  u8g2.begin();
  u8g2.enableUTF8Print();		// enable UTF8 support for the Arduino print() function
}

void loop(void) {
  u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();
  u8g2.setCursor(0, 15);
  u8g2.print("Hello World!");
  u8g2.setCursor(0, 40);
  u8g2.print("世界你好");		// Chinese "Hello World" 
  u8g2.sendBuffer();
  
  delay(1000);
}
显示效果如下:


三、制作自定义的字库

         u8b2官方的库只能显示很少的中文,如果要显示其他中文就要自己制作字库了。制作字库的工具在目录在:

u8g2\tools\font\bdfconv
bdfconv.exe这个工具是一个dos命令,需要开启cmd.exe在命令行输入命令或者用bat脚本来生成字库。bdfconv.exe命令的使用方法如下:

bdfconv [options] filename
-h          Display this help
-v          Print log messages
-b <n>      Font build mode, 0: proportional, 1: common height, 2: monospace, 3: multiple of 8
-f <n>      Font format, 0: ucglib font, 1: u8g2 font, 2: u8g2 uncompressed 8x8 font (enforces -b 3)
-m 'map'    Unicode ASCII mapping
-M 'mapfile'    Read Unicode ASCII mapping from file 'mapname'
-o <file>   C output file
-n <name>   C indentifier (font name)
-d <file>   Overview picture: Enable generation of bdf.tga and assign BDF font <file> for description
-l <margin> Overview picture: Set left margin
-a          Overview picture: Additional font information (background, orange&blue dot)
-t          Overview picture: Test string (Woven silk pyjamas exchanged for blue quartz.)
-r          Runtime test

map := <mapcmd> { "," <mapcmd> }
mapcmd := <default> | <maprange> | <exclude>
default := "*"
maprange := <range> [  ">" <addexpr> ]        Move specified glyph <range> to target code <num>
exclude := "~" <range>
range := <addexpr> [ "-" <addexpr> ]          Select glyphs within specified range
addexpr := <mulexpr> [ "+" <mulexpr> ]
mulexpr := <num> [ "*" <num> ]
num := <hexnum> | <decnum>
hexnum := "$" <hexdigit> { <hexdigit> }
decnum := <decdigit> { <decdigit> }
decdigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
hexdigit := "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" | <decdigit>
{ } zero, one ore more, [ ] zero or once, | alternative
example:
  -m '32-255'     select gylphs from encoding 32 to 255
  -m '32-255,~64' select gylphs from encoding 32 to 255, exclude '@'
  -m '32,48-57'   select space, '1', '2', ... '9'
build modes:
 -b 0: Most compact, glyph bitmap is minimal
 -b 1: Like -b 0, but glyph bitmap is extended to the height of the largest glyph within the selected glyph list.
       Also the width of the gylphs is extended to cover the delta x advance.
 -b 2: Like -b 1, but glyph width is set to the width of the largest glyph within the selected gylph list.
 -b 3: Like -b 2, but width and height are forced to be a multiple of 8.

        在“bdfconv”目录下有一个脚本 “test_helvb18.bat” ,

bdfconv.exe -v -f 1 -m "32-127" ../bdf/helvB18.bdf -o helvb18_tf.c  -n u8g2_font_helvB18_tf -d ../bdf/helvB18.bdf
type helvb18_tf.c

        我们可以利用这个脚本来生成字库,这个是直接通过“helvB18.bdf”字库文件生成 ".C"的字库数据,而中文字库很大,一般都有几M大小,显然这种直接生成的方法不好用。还有另外一种字库生成方法是按需提取字库,利用一个map表来提取要生成字库的内容,map表里面放置的是unicode编码(需要哪些中文就填这些中文的unicode码)。这种生产方式的命令是:

bdfconv.exe -v ../bdf/unifont.bdf -b 0 -f 1 -M ../build/chinese1.map -d ../bdf/7x13.bdf -n u8g2_font_unifont_zgzt -o u8g2_font_unifont_zgzt.c
从这个命令我们可以知道map文件是 “chinese1.map”,全路径是:“u8g2\tools\font\build”,我们可以在“chinese1.map”文件里面添加我们要显示的中文。生产的字库代码在u8g2_font_unifont_zgzt.c里面,我们可以拷贝里面的内容,然后把它替换" U8g2\src\clib\u8g2_fonts.c " 里面的数组“u8g2_font_unifont_t_chinese1”的内容。


参考资料:

1.https://github.com/olikraus/u8g2

2.https://clz.me/u8g2-bdfconv/

3.查看unicode编码的方法

  


  • 9
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值