arduino如何在ssd1306上显示中文字符
来源:互联网 发布:淘宝航空箱定做 编辑:程序博客网 时间:2024/05/23 17:41
一、前言
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\bdfconvbdfconv.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 testmap := <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 rangeaddexpr := <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, | alternativeexample: -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.bdftype 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编码的方法
- arduino如何在ssd1306上显示中文字符
- 【Arduino】【MATLAB】用ssd1306 oled屏显示任意图片
- 如何在QT中显示中文字符
- Arduino驱动SSD1306 OLED
- opencv-在图像上显示字符(不包括中文)
- opencv-在图像上显示字符(不包括中文)
- source-insight中文:utf-8编码的中文字符在source in sight如何正常显示
- source-insight中文:utf-8编码的中文字符在source in sight如何正常显示
- 在openframeworks上显示中文
- Intel Edison arduino代码驱动OLED(SSD1306)
- 怎样在ArcIMS的HTML Viewer上显示中文字符(zz)
- sublime text3在浏览器上打开时,怎样显示中文的字符
- 如何在Intel伽利略上运行Arduino程序
- 如何在终端显示中文
- 在VTK中显示中文字符
- 在XNA中显示中文字符
- 在Ubuntu命令行中显示中文字符
- utf-8编码的中文字符在source in sight如何正常显示
- Stepping Thread Group插件介绍
- oracle查看表空间
- Linux对tomcat的启动、关闭、杀死
- 微信小程序 仿美团城市选择 城市切换
- pku 3461 Oulipo (裸kmp匹配)
- arduino如何在ssd1306上显示中文字符
- 解析程序的健壮性和鲁棒性
- python day02 变色字体进度条输出
- ros源码分析(6)—roslaunch Commandline Tools
- android基于监听的事件处理机制
- Win10 下安装 TensorFlow 遇到的一些问题
- 设计模式(23)-访问者模式
- 移动端flex 经典布局
- [绍棠_swift] Swift中的结构体