|
转载自:零知开源快速入门19-1602液晶屏显示
http://www.lingzhilab.com/forum. ... d=206&fromuid=2
(出处: 零知实验室)
液晶屏在我们电子制作中是一种常用的输出显示设备,下面就用很常用的1602液晶屏进行演示液晶屏的应用。
一、工具原料
- 电脑,windows系统
- 零知开发板
- micro-usb线
- 1602液晶屏1个
- 电位器1个
- 面包板一个+若干跳线
二、硬件连接
1、电路原理图
2、电路连接
三、方法步骤
1、打开零知实验室软件开发工具,然后新建项目,输入以下代码:
- /**
- * 文件: lcd1602-test.ino by 零知实验室(www.lingzhilab.com)
- * -- 零知开源,让电子制作变得更简单! --
- * 时间: 2018/06/27 15:17
- * 说明: lcd1602 显示屏, 4位data的方式接线
- **/
- int LCD1602_RS = 6;
- int LCD1602_RW = 5;
- int LCD1602_EN = 4;
- int DB[] = {0,1,2,3};
- char str1[] = "Welcome to";
- char str2[] = "lingzhilab";
- void LCDWrCmd(int command);
- void LCDWrData(int dat);
- void LCD_WR_String(int x, int y, char *s);
- void lcd1602_init(void);
- // the setup routine runs once when you press reset:
- void setup() {
- // put your setup code here, to run once:
- int i =0;
- for(i=0; i<=6; i++)
- {
- pinMode(i, OUTPUT);
- }
- lcd1602_init(); //初始化1602
- }
- // the loop routine runs over and over again forever:
- void loop() {
- // put your main code here, to run repeatedly:
- LCDWrCmd(0x01);
- delay(50);
- LCD_WR_String(0,0,str1);
- delay(50);
- LCD_WR_String(4,1,str2);
- delay(5000);
- }
- void LCDWrCmd(int command)
- {
- int i,temp;
- digitalWrite(LCD1602_EN, LOW);
- delayMicroseconds(1);
- digitalWrite(LCD1602_RW, LOW);
- digitalWrite(LCD1602_RS, LOW);
- temp = command & 0xf0;
- for (i = DB[0]; i <= 3; i++)
- {
- digitalWrite(i,temp&0x80);
- temp<<=1;
- }
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN, HIGH);
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN, LOW);
- temp = (command&0x0f)<<4;
- for(i=DB[0]; i<=3; i++)
- {
- digitalWrite(i, temp&0x80);
- temp<<=1;
- }
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN,HIGH);
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN,LOW);
- delayMicroseconds(1);
- }
- void LCDWrData(int dat)
- {
- int i, temp;
- digitalWrite(LCD1602_EN, LOW);
- delayMicroseconds(1);
- digitalWrite(LCD1602_RS, HIGH);
- digitalWrite(LCD1602_RW, LOW);
- temp = dat&0xf0;
- for(i=DB[0]; i<=3; i++)
- {
- digitalWrite(i, temp&0x80);
- temp<<=1;
- }
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN, HIGH);
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN, LOW);
- temp = (dat&0x0f)<<4;
- for(i=DB[0]; i<=3; i++)
- {
- digitalWrite(i, temp&0x80);
- temp<<=1;
- }
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN, HIGH);
- delayMicroseconds(1);
- digitalWrite(LCD1602_EN, LOW);
- delayMicroseconds(1);
- }
- void LCD_SET_XY(int x, int y)
- {
- int address;
- if(y==0)
- address = 0x80+x;
- else
- address = 0xc0+x;
- LCDWrCmd(address);
- }
- void LCD_WR_Char(int x, int y, int dat)
- {
- LCD_SET_XY(x, y);
- delayMicroseconds(50);
- LCDWrData(dat);
- }
- void LCD_WR_String(int x, int y, char *s)
- {
- int count = 0;
- while(*s)
- {
- LCD_SET_XY(x+count, y);
- //delayMicroseconds(500);
- delay(2);
- count++;
- LCDWrData(*s);
- s++;
- }
- }
- void lcd1602_init(void)
- {
- delay(100);
- LCDWrCmd(0x28);
- delay(64);
- LCDWrCmd(0x28);
- delay(50);
- LCDWrCmd(0x06);
- delay(50);
- LCDWrCmd(0x0c);
- delay(50);
- LCDWrCmd(0x80);
- delay(50);
- LCDWrCmd(0x01);
- delay(50);
- }
[color=rgb(51, 102, 153) !important]复制代码
2、按照之前的方式先【编译】,然后【上传】到开发板中。
四、成果展示
上面步骤成功完成后,我们调节一下电位器,根据实际情况调节到最佳对比度,就可以看到效果如下:
|
|