本帖最后由 half 于 2014-1-2 11:09 编辑
前段时间买了一个12232的中文屏,想和arduino连起来,之前用1602的时候,这里有朋友提供了LiquidCrystal.cpp文件。这次却没有找到中文屏的资料,网上见到的大多是C51的代码,没看到AVR和arduino的。实在没有办法,只能硬着头皮自己写。看了datasheet和C51的程序,看了LiquidCrystal.cpp的代码,写了好久,终于实现了功能。
12232中文屏的资料网上可以找到的,我看到有个朋友写的挺详细的,有兴趣的也可以下载我后面的附件说明书。它分为带中文字库和不带中文字库的,我这次用的是带中文字库的,有个ST7920-GB的字库表,可以在里面找到每个字的编码。
首先接线的时候数据位不要接反,datasheet里面有很详细的讲解,耐心仔细看过后会发现许多,指令发送后要对enable进行操作。C51的程序中可以将中文直接发送出去,但是arduino好像不行,感觉是它对中文支持的问题吧。还有一个就是屏幕有一个检测是否繁忙的功能,这里偷懒,没有写,语句也没有优化。有兴趣的朋友可以把它做成库文件,方便以后使用。
下面把代码贴出来,和大家分享下。
- int rsPin = 13; //HIGH=data; LOW=commandand;
- int rwPin = 11; //HIGH=read; LOW=write;
- int enablePin = 2; //input enable;
- int dataPins[] = {
- 3, 4, 5, 6, 7, 8, 9, 10}; //使用数组来定义总线需要的管脚
- void setup(void)
- {
- pinMode(rsPin,OUTPUT);
- pinMode(rwPin,OUTPUT);
- pinMode(enablePin,OUTPUT);
- }
- void loop(void)
- {
- InitLcd();
- //first line
- WriteCommand(0x80);
- //欢
- DisplayCharacter(0xbb);
- DisplayCharacter(0xb6);
- //迎
- DisplayCharacter(0xd3);
- DisplayCharacter(0xad);
- //您
- DisplayCharacter(0xc4);
- DisplayCharacter(0xfa);
- //使
- DisplayCharacter(0xca);
- DisplayCharacter(0xb9);
- //用
- DisplayCharacter(0xd3);
- DisplayCharacter(0xc3);
- //中
- DisplayCharacter(0xd6);
- DisplayCharacter(0xd0);
- //文
- DisplayCharacter(0xce);
- DisplayCharacter(0xc4);
- //屏
- DisplayCharacter(0xc6);
- DisplayCharacter(0xc1);
- //second line
- WriteCommand(0x90);
- //2014
- DisplayCharacter('2');
- DisplayCharacter('0');
- DisplayCharacter('1');
- DisplayCharacter('4');
- //年
- DisplayCharacter(0xc4);
- DisplayCharacter(0xea);
- // 1
- DisplayCharacter(' ');
- DisplayCharacter('1');
- //月
- DisplayCharacter(0xd4);
- DisplayCharacter(0xc2);
- // 2
- DisplayCharacter(' ');
- DisplayCharacter('2');
- //日
- DisplayCharacter(0xc8);
- DisplayCharacter(0xd5);
- while(1)
- {
- }
- }
- /*------------------初始化-----------------*/
- void InitLcd (void)
- {
- //
- WriteCommand (0x01); /*清屏,地址指针指向00H*/
- delayMicroseconds(2000);
- WriteCommand (0x06); /*光标向右移动*/
- delayMicroseconds(100);
- WriteCommand (0x0c); /*开显示,关游标,关闪烁*/
- delayMicroseconds(100);
- WriteCommand (0x30); /*并行八位模式*/
- delayMicroseconds(100);
- }
- /*-------------------执行命令--------------------*/
- void WriteCommand(unsigned char chrCommand)
- {
- digitalWrite(rsPin,LOW);
- digitalWrite(rwPin,LOW);
- for (int i = 0; i < 8; i++)
- {
- pinMode(dataPins[i], OUTPUT);
- digitalWrite(dataPins[i], (chrCommand >> i) & 0x01);
- }
- PulseEnable();
- }
- /*---------------显示汉字或字符----------------*/
- void DisplayCharacter (unsigned char chrCharacter)
- {
- digitalWrite(rsPin,HIGH);
- digitalWrite(rwPin,LOW);
- for (int i = 0; i < 8; i++)
- {
- pinMode(dataPins[i], OUTPUT);
- digitalWrite(dataPins[i], (chrCharacter >> i) & 0x01);
- }
- PulseEnable();
- }
- /*-------------------enable信号--------------------*/
- void PulseEnable(void)
- {
- digitalWrite(enablePin, LOW);
- delayMicroseconds(1);
- digitalWrite(enablePin, HIGH);
- delayMicroseconds(1); // enable pulse must be >450ns
- digitalWrite(enablePin, LOW);
- delayMicroseconds(100); // commands need > 37us to settle
- }
- //Chen OpenSource
- //2014-1-2
复制代码
|