3.20 液晶 LCD1602 实验
实验现象:
- 液晶第一页显示 Welcome to use!
- 液晶第二页显示I am Huangdi
- 液晶第三页显示I am the admin
- 液晶的对比度可以通过调节对比度电位器改变。
理论学习:
- LCD1602 液晶是一款通用简易液晶,可以显示 ASCII 码的英文字母、数字和标点符号(不能显示中文)。
Lcd1602是16*2的字符阵 ,其中 0x80 地址对应液晶的第一行第一个列,之后每列地址加一,第一行最后一列的地址位 0x8f。第二行的地址从 0x80+0x40 开始,最后一列的地址位 0x80+0x4f。
操作方法一
- 液晶屏的码表
0x20~0x7F为标准的ASCII码,0xA0~0xFF为日文字符和希腊文字符,其余字符码(0x10~0x1F及0x80~0x9F)没有定义。 以下是1602的16进制ASCII码表地址:读的时候,先读左边那列,再读上面那行,如:感叹号!的ASCII为0x21,字母B的ASCII为0x42(前面加0x表示十六进制)。
操作方法二
- LCD1602 总共可以显示 2 行*16 个字符。程序可调用库函数 LCD1602.h 的 Init_LCD1602();对程序进行初始化,然后设置液晶指针位置 LCD1602_write_com(0x80);
- 用户可以通过LCD1602_write_data函数写单个字符,或者通过LCD1602_write_word写字符串。
- LCD1602液晶的详细指标和时序图请参考 1602液晶中文说明.pdf(Arduino独家整理资料包\7.芯片及模块资料\1602液晶中文说明.pdf)
实验代码
由于方法二需要调用库函数Lcd1602.h,因此这里使用实验方法一
int DI = 12; int RW = 11; int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//使用数组来定义总线需要的管脚 int Enable = 2; void LcdCommandWrite(int value) { // 定义所有引脚 int i = 0; for (i=DB[0]; i <= DI; i++) //总线赋值 { digitalWrite(i,value & 01);//因为1602液晶信号识别是D7-D0(不是D0-D7),这里是用来反转信号。 value >>= 1; } digitalWrite(Enable,LOW); delayMicroseconds(1); digitalWrite(Enable,HIGH); delayMicroseconds(1); // 延时1ms digitalWrite(Enable,LOW); delayMicroseconds(1); // 延时1ms } void LcdDataWrite(int value) { // 定义所有引脚 int i = 0; digitalWrite(DI, HIGH); digitalWrite(RW, LOW); for (i=DB[0]; i <= DB[7]; i++) { digitalWrite(i,value & 01); value >>= 1; } digitalWrite(Enable,LOW); delayMicroseconds(1); digitalWrite(Enable,HIGH); delayMicroseconds(1); digitalWrite(Enable,LOW); delayMicroseconds(1); // 延时1ms } void setup (void) { int i = 0; for (i=Enable; i <= DI; i++) { pinMode(i,OUTPUT); } delay(100); // 短暂的停顿后初始化LCD // 用于LCD控制需要 LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小 delay(64); //LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小 //delay(50); //LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小 //delay(20); //LcdCommandWrite(0x06); // 输入方式设定 // 自动增量,没有显示移位 //delay(20); //LcdCommandWrite(0x0E); // 显示设置 // 开启显示屏,光标显示,无闪烁 //delay(20); //LcdCommandWrite(0x01); // 屏幕清空,光标位置归零 //delay(100); //LcdCommandWrite(0x80); // 显示设置 // 开启显示屏,光标显示,无闪烁 //delay(20); } void loop (void) { LcdCommandWrite(0x01); // 屏幕清空,光标位置归零 delay(10); LcdCommandWrite(0x80); delay(10); // 写入欢迎信息 LcdDataWrite('W'); LcdDataWrite('e'); LcdDataWrite('l'); LcdDataWrite('c'); LcdDataWrite('o'); LcdDataWrite('m'); LcdDataWrite('e'); LcdDataWrite(' '); LcdDataWrite('t'); LcdDataWrite('o'); LcdDataWrite(' '); LcdDataWrite('u'); LcdDataWrite('s'); LcdDataWrite('e'); LcdDataWrite('!'); delay(10); delay(5000); LcdCommandWrite(0x01); // 屏幕清空,光标位置归零 delay(10); LcdDataWrite('I'); LcdDataWrite(' '); LcdDataWrite('a'); LcdDataWrite('m'); LcdDataWrite(' '); LcdDataWrite('h'); LcdDataWrite('u'); LcdDataWrite('a'); LcdDataWrite('n'); LcdDataWrite('g'); LcdDataWrite('d'); LcdDataWrite('i'); delay(3000); LcdCommandWrite(0x02); //设置模式为新文字替换老文字,无新文字的地方显示不变。 delay(10); LcdCommandWrite(0x80+5); //定义光标位置为第一行第六个位置 delay(10); LcdDataWrite('t'); LcdDataWrite('h'); LcdDataWrite('e'); LcdDataWrite(' '); LcdDataWrite('a'); LcdDataWrite('d'); LcdDataWrite('m'); LcdDataWrite('i'); LcdDataWrite('n'); delay(5000); }