实验二十一:LCD1602液晶屏

本次试验使用arduino 直接驱动1602 液晶显示文字

1602 液晶在应用中非常广泛,最初的1602 液晶使用的HD44780 控制器,现在各个厂家的1602 模块基本上都是采用了与之兼容的IC,所以特性上基本都是一致的。

1602LCD 主要技术参数

  • 显示容量为16×2 个字符;
  • 芯片工作电压为4.5~5.5V;
  • 工作电流为2.0mA(5.0V);
  • 模块最佳工作电压为5.0V;
  • 字符尺寸为2.95×4.35(W×H)mm。

1602 液晶接口引脚定义

在这里插入图片描述
接口说明:

1、两组电源 一组是模块的电源 一组是背光板的电源 一般均使用5V 供电。本次试验背光使用3.3V 供电也可以工作。
2、VL 是调节对比度的引脚,串联不大于5KΩ 的电位器进行调节。本次实验使用1KΩ 的电阻来设定对比度。其连接分高电位与低电位接法,本次使用低电位接法,串联1KΩ 电阻后接GND。
3、RS 是很多液晶上都有的引脚 是命令/数据选择引脚 该脚电平为高时表示将进行数据操作;为低时表示进行命令操作。
4、RW 也是很多液晶上都有的引脚 是读写选择端 该脚电平为高是表示要对液晶进行读操作;为低时表示要进行写操作。
5、E 同样很多液晶模块有此引脚 通常在总线上信号稳定后给一正脉冲通知把数据读走,在此脚为高电平的时候总线不允许变化。
6、D0—D7 8 位双向并行总线,用来传送命令和数据。
7、BLA 是背光源正极,BLK 是背光源负极。

1602 液晶的基本操作分以下四种:

在这里插入图片描述
下图就是1602 液晶实物图
在这里插入图片描述
在这里插入图片描述

实验连接图

1602 直接与arduino 通信,根据产品手册描述,分8 位连接法与4 位连接法,咱们先使用8位连接法进行实验。硬件连接方式如下图
在这里插入图片描述
在这里插入图片描述


代码如下:

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+3);
delay(10);
// 写入欢迎信息
LcdDataWrite(‘W’);
LcdDataWrite(‘e’);
LcdDataWrite(‘l’);
LcdDataWrite(‘c’);
LcdDataWrite(‘o’);
LcdDataWrite(‘m’);
LcdDataWrite(‘e’);
LcdDataWrite(’ ‘);
LcdDataWrite(‘t’);
LcdDataWrite(‘o’);
delay(10);
LcdCommandWrite(0xc0+1); // 定义光标位置为第二行第二个位置
delay(10);
LcdDataWrite(‘g’);
LcdDataWrite(‘e’);
LcdDataWrite(‘e’);
LcdDataWrite(‘k’);
LcdDataWrite(’-’);
LcdDataWrite(‘w’);
LcdDataWrite(‘o’);
LcdDataWrite(‘r’);
LcdDataWrite(‘k’);
LcdDataWrite(‘s’);
LcdDataWrite(‘h’);
LcdDataWrite(‘o’);
LcdDataWrite(‘p’);
delay(5000);
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(10);
LcdDataWrite(‘I’);
LcdDataWrite(’ ‘);
LcdDataWrite(‘a’);
LcdDataWrite(‘m’);
LcdDataWrite(’ ‘);
LcdDataWrite(‘h’);
LcdDataWrite(‘o’);
LcdDataWrite(‘n’);
LcdDataWrite(‘g’);
LcdDataWrite(‘y’);
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);
}

///
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+3);
delay(10);
// 写入欢迎信息
LcdDataWrite(‘W’);
LcdDataWrite(‘e’);
LcdDataWrite(‘l’);
LcdDataWrite(‘c’);
LcdDataWrite(‘o’);
LcdDataWrite(‘m’);
LcdDataWrite(‘e’);
LcdDataWrite(’ ‘);
LcdDataWrite(‘t’);
LcdDataWrite(‘o’);
delay(10);
LcdCommandWrite(0xc0+1); // 定义光标位置为第二行第二个位置
delay(10);
LcdDataWrite(‘g’);
LcdDataWrite(‘e’);
LcdDataWrite(‘e’);
LcdDataWrite(‘k’);
LcdDataWrite(’-’);
LcdDataWrite(‘w’);
LcdDataWrite(‘o’);
LcdDataWrite(‘r’);
LcdDataWrite(‘k’);
LcdDataWrite(‘s’);
LcdDataWrite(‘h’);
LcdDataWrite(‘o’);
LcdDataWrite(‘p’);
delay(5000);
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(10);
LcdDataWrite(‘I’);
LcdDataWrite(’ ‘);
LcdDataWrite(‘a’);
LcdDataWrite(‘m’);
LcdDataWrite(’ ‘);
LcdDataWrite(‘h’);
LcdDataWrite(‘o’);
LcdDataWrite(‘n’);
LcdDataWrite(‘g’);
LcdDataWrite(‘y’);
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);
}

4 位接法

在正常使用下,8 位接法基本把arduino 的数字端口占满了,如果想要多接几个传感器就没有端口了,这种情况下怎么处理呢,咱们可以使用4 位接法。

实验接线图

4 位接法的硬件连接方法如下图

在这里插入图片描述
在这里插入图片描述
硬件接好后把下面的代码上传到控制板上,看看效果。

int LCD1602_RS=12;
int LCD1602_RW=11;
int LCD1602_EN=10;
int DB[] = { 6, 7, 8, 9};
char str1[]=“Welcome to”;
char str2[]=“geek-workshop”;
char str3[]=“this is the”;
char str4[]=“4-bit interface”;
void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0) address = 0x80 + x;
else address = 0xC0 + x;
LCD_Command_Write(address);
}
void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y );
LCD_Data_Write(dat);
}
void LCD_Write_String(int X,int Y,char *s)
{
LCD_SET_XY( X, Y ); //设置地址
while (*s) //写字符串
{
LCD_Data_Write(*s);
s ++;
}
}
void setup (void)
{
int i = 0;
for (i=6; i <= 12; i++)
{
pinMode(i,OUTPUT);
}
delay(100);
LCD_Command_Write(0x28);//4 线 2 行 5x7
delay(50);
LCD_Command_Write(0x06);
delay(50);
LCD_Command_Write(0x0c);
delay(50);
LCD_Command_Write(0x80);
delay(50);
LCD_Command_Write(0x01);
delay(50);
}
void loop (void)
{
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(3,0,str1);//第1 行,第4 个地址起
delay(50);
LCD_Write_String(1,1,str2);//第2 行,第2 个地址起
delay(5000);
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(0,0,str3);
delay(50);
LCD_Write_String(0,1,str4);
delay(5000);
}

普通浏览复制代码保存代码打印代码

int LCD1602_RS=12;
int LCD1602_RW=11;
int LCD1602_EN=10;
int DB[] = { 6, 7, 8, 9};
char str1[]=“Welcome to”;
char str2[]=“geek-workshop”;
char str3[]=“this is the”;
char str4[]=“4-bit interface”;
void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0) address = 0x80 + x;
else address = 0xC0 + x;
LCD_Command_Write(address);
}
void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y );
LCD_Data_Write(dat);
}
void LCD_Write_String(int X,int Y,char *s)
{
LCD_SET_XY( X, Y ); //设置地址
while (*s) //写字符串
{
LCD_Data_Write(*s);
s ++;
}
}
void setup (void)
{
int i = 0;
for (i=6; i <= 12; i++)
{
pinMode(i,OUTPUT);
}
delay(100);
LCD_Command_Write(0x28);//4 线 2 行 5x7
delay(50);
LCD_Command_Write(0x06);
delay(50);
LCD_Command_Write(0x0c);
delay(50);
LCD_Command_Write(0x80);
delay(50);
LCD_Command_Write(0x01);
delay(50);
}
void loop (void)
{
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(3,0,str1);//第1 行,第4 个地址起
delay(50);
LCD_Write_String(1,1,str2);//第2 行,第2 个地址起
delay(5000);
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(0,0,str3);
delay(50);
LCD_Write_String(0,1,str4);
delay(5000);
}