结合这一篇http://www.gaohaiyan.com/2554.html实现本例。
本例讲在LCD1602中近似得显示中文汉字的方法。参考了 https://www.arduino.cn/thread-21760-1-1.html 和 https://www.arduino.cn/thread-47263-1-1.html 两篇文章,前者使用了I2C模块,后者即本文的无模块方式。
本例还是用了一个高人写的集合类,参看 https://blog.csdn.net/dpjcn1990/article/details/103376284 和 https://github.com/ivanseidel/LinkedList 。
首先,定义一个Hanzi.h文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#ifndef Hanzi_H #define Hanzi_H #include <Arduino.h> #include </workspace/LinkedList.h> // 第三方集合类的绝对路径 class Hanzi { // 初始化自定义字符集合,汉字集合 public: void init(); // 自定义字符集合中是否有这个字 public: bool haveHanzi(String pinyin); // 查找自定义字符 public: byte * getHanzi(String pinyin); // 自定义字符容器 public: LinkedList<byte *> characterList = LinkedList<byte *>(); // 汉字容器 public: LinkedList<String> pinyinList = LinkedList<String>(); // ----------------------------------------------------------- // ------ 以下是自定义的汉字的字符位 ------ // ------ LCD的每个格子为8行5列的点阵 ----- // ------ 每行固定0b起始,之后的1为点亮,0为默认熄灭 ----- // ------ 点亮部分拼接为汉字的图案 ----- // ----------------------------------------------------------- byte tianArr[8] = { //"天"的数据。添、填、田、甜、恬、腆,无论声调,拼音相同的 0b11111, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0b01010, 0b10001, }; byte qiArr[8] = { //"七"的数据。气、起、齐 0b00100, 0b00101, 0b00110, 0b00100, 0b01100, 0b10100, 0b00100, 0b00111, }; byte niArr[8] = { //"尼"的数据。你、拟、泥、逆 0b11111, 0b10001, 0b11111, 0b10000, 0b10101, 0b10110, 0b10100, 0b10111, }; byte haoArr[8] = { //"号"的数据。好、浩、豪、耗 0b11111, 0b10001, 0b11111, 0b11111, 0b01000, 0b01111, 0b00001, 0b00111, }; byte huangArr[8] = { //"皇"的数据。黄、慌、晃、簧 0b01000, 0b11111, 0b11111, 0b11111, 0b00000, 0b11111, 0b00100, 0b11111, }; byte hongArr[8] = { //"洪"的数据。红、宏、鸿、虹、吽 0b01010, 0b11111, 0b01010, 0b11111, 0b00101, 0b11111, 0b00000, 0b01101, }; byte dengArr[8] = { //"灯"的数据。等、登、邓 0b01111, 0b11010, 0b01010, 0b01110, 0b01010, 0b10010, 0b10010, 0b10110, }; byte kaiArr[8] = { //"开"的数据。凯、楷、锴、慨 0b11111, 0b01010, 0b11111, 0b01010, 0b01010, 0b01010, 0b01010, 0b10010, }; byte guanArr[8] = { //"关"的数据。管、观、灌、冠 0b01010, 0b11111, 0b00100, 0b00100, 0b11111, 0b00100, 0b01010, 0b10001, }; }; #endif |
然后,实现自定义的汉字字符Hanzi.cpp。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include </workspace/Hanzi.h> // 汉字集合、字符集合,两者的初始化的顺序要一致 void Hanzi::init() { pinyinList.add("tian"); // "天"的拼音 pinyinList.add("qi"); pinyinList.add("ni"); pinyinList.add("hao"); pinyinList.add("huang"); pinyinList.add("hong"); pinyinList.add("deng"); pinyinList.add("kai"); pinyinList.add("guan"); characterList.add(tianArr); // "天"的自定义字符 characterList.add(qiArr); characterList.add(niArr); characterList.add(haoArr); characterList.add(huangArr); characterList.add(hongArr); characterList.add(dengArr); characterList.add(kaiArr); characterList.add(guanArr); } // 字符库中是否有这个字 bool Hanzi::haveHanzi(String pinyin) { int size = pinyinList.size(); for (int i = 0; i < size; i++) { String py = pinyinList.get(i); if (py.equals(pinyin)) { return true; } } return false; } // 根据汉字查找字符 byte * Hanzi::getHanzi(String pinyin) { int index = 0; int size = pinyinList.size(); for (int i = 0; i < size; i++) { String py = pinyinList.get(i); if (py.equals(pinyin)) { index = i; break; } } return characterList.get(index); } // LinkedList查看这两个链接 // https://blog.csdn.net/dpjcn1990/article/details/103376284 // https://github.com/ivanseidel/LinkedList |
最后使用即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#include <SoftwareSerial.h> #include <LiquidCrystal.h> #include </workspace/Hanzi.h> LiquidCrystal LCD(7, 6, 5, 4, 3, 2); SoftwareSerial BT(12, 13); int yellowLedPin = 10; int redLedPin = 11; String code1 = "黄灯开"; // 中文指令 String code2 = "黄灯关"; String code3 = "红灯开"; String code4 = "红灯关"; Hanzi hanzi; // 自定义的汉字字符控制器 void setup() { pinMode( yellowLedPin, OUTPUT); pinMode( redLedPin, OUTPUT); Serial.begin(9600); BT.begin(9600); LCD.begin(16, 2); LCD.clear(); hanzi.init(); // 初始化汉字字符控制器 LCD.createChar(0, hanzi.getHanzi("huang")); // 把用到的字符数据写入LCD的寄存器 LCD.createChar(1, hanzi.getHanzi("hong")); // LCD1602只能保存8个字符,索引0-7,再多了就会出现混乱 LCD.createChar(2, hanzi.getHanzi("deng")); LCD.createChar(3, hanzi.getHanzi("kai")); LCD.createChar(4, hanzi.getHanzi("guan")); } void loop() { if (BT.available()) { String msg = ""; while (BT.available()) { char data = BT.read(); delay(3); msg += data; } msg.trim(); Serial.println("ard指令:" + msg ); LCD.clear(); if (msg.equals(code1)) { digitalWrite(yellowLedPin, HIGH); LCD.setCursor(0, 0); // 黄 LCD.write(byte(0)); LCD.setCursor(1, 0); // 灯 LCD.write(byte(2)); LCD.setCursor(2, 0); // 开 LCD.write(byte(3)); } else if (msg.equals(code2)) { digitalWrite(yellowLedPin, LOW); LCD.setCursor(0, 0); // 黄 LCD.write(byte(0)); LCD.setCursor(1, 0); // 灯 LCD.write(byte(2)); LCD.setCursor(2, 0); // 关 LCD.write(byte(4)); } else if (msg.equals(code3)) { digitalWrite(redLedPin, HIGH); LCD.setCursor(0, 0); // 红 LCD.write(byte(1)); LCD.setCursor(1, 0); // 灯 LCD.write(byte(2)); LCD.setCursor(2, 0); // 开 LCD.write(byte(3)); } else if (msg.equals(code4)) { digitalWrite(redLedPin, LOW); LCD.setCursor(0, 0); // 红 LCD.write(byte(1)); LCD.setCursor(1, 0); // 灯 LCD.write(byte(2)); LCD.setCursor(2, 0); // 开 LCD.write(byte(4)); } else { LCD.setCursor(0, 0); LCD.print(msg); } } } |
手机向蓝牙发送“黄灯开”,汉字效果如图:
格子的点阵数实在太少,笔画数比较多的字很难显示清晰。
不熟悉c++,代码比较粗糙。
-end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/2567.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设