Arduino基础入门篇31—LCD1602液晶显示

亚洲365bet 📅 2025-07-29 13:55:42 👤 admin 👁️ 6397 ❤️ 109
Arduino基础入门篇31—LCD1602液晶显示

LCD1602也叫1602字符型液晶,是一种专门用来显示字母、数字、符号的点阵型液晶模块,能同时显示16X2即32个字符。本篇我们来认识LCD1602,驱动它显示「Hello World」。

1. LCD1602介绍

对于单片机爱好者和电子爱好者来说,或多或少都曾使用过液晶显示模块。它们都是由若干个点阵字符位组成的,根据显示内容可分为1602、12864等。LCD1602可以显示两行字符,每行16个,显示背景也有所不同,有蓝色白字和黄色白字等。

LCD1602接口说明:

其中:

VL(V0):液晶对比度调整端,用于调节显示对比度,一般接10K电位器进行调整。RS:数据/命令选择。高电平表示数据,低电平表示命令。RW:读写选择。高电平为读,低电平为写。一般我们写入数据进行显示,所以此引脚接地。EN:使能信号,配合数据/命令的读写。D0-D7:双向数据端。可以使用8根数据线并行操作,也可以使用4根数据线串行操作。

关于LCD1602的驱动也非常简单。官方数据手册给出了基本操作时序和初始化设置步骤。本篇我们为了减少线路连接采用4位串行操作方式,一条8位的数据和命令分两次写入1602。

2. 实验材料

Uno R3开发板配套USB数据线面包板及配套连接线LCD1602液晶屏16Pin排针10K电位器

3. 实验步骤

1. 根据原理图搭建电路图。

LCD1602的第1、5、16脚接开发板GND;LCD1602第2、15脚接开发板5V;LCD1602的第4、6、11、12、13、14分别连接开发板数字引脚7、6、5、4、3、2;电位器两端引脚分别连接5V和GND,中间引脚连接LCD1602第3引脚。

实验原理图如下图所示:

实物连接图如下图所示:

2. 新建sketch,拷贝如下代码替换自动生成的代码并进行保存。

/*

* LCD1602_bit4

* LCD1602驱动显示Hello World

*/

int LCD1602_RS = 7;

int LCD1602_EN = 6;

int DB[4] = { 2, 3, 4, 5};

/*

* LCD写命令

*/

void LCD_Command_Write(int command)

{

int i, temp;

digitalWrite( LCD1602_RS, LOW);

digitalWrite( LCD1602_EN, LOW);

temp = command & 0xf0;

for (i = DB[0]; i <= 5; 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 <= 5; i++)

{

digitalWrite(i, temp & 0x80);

temp <<= 1;

}

digitalWrite( LCD1602_EN, HIGH);

delayMicroseconds(1);

digitalWrite( LCD1602_EN, LOW);

}

/*

* LCD写数据

*/

void LCD_Data_Write(int dat)

{

int i = 0, temp;

digitalWrite( LCD1602_RS, HIGH);

digitalWrite( LCD1602_EN, LOW);

temp = dat & 0xf0;

for (i = DB[0]; i <= 5; 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 <= 5; i++)

{

digitalWrite(i, temp & 0x80);

temp <<= 1;

}

digitalWrite( LCD1602_EN, HIGH);

delayMicroseconds(1);

digitalWrite( LCD1602_EN, LOW);

}

/*

* LCD设置光标位置

*/

void LCD_SET_XY( int x, int y )

{

int address;

if (y == 0) address = 0x80 + x;

else address = 0xC0 + x;

LCD_Command_Write(address);

}

/*

* LCD写一个字符

*/

void LCD_Write_Char( int x, int y, int dat)

{

LCD_SET_XY( x, y );

LCD_Data_Write(dat);

}

/*

* LCD写字符串

*/

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 = 2; i <= 7; 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_Write_String(2, 0, "Hello World!");

LCD_Write_String(6, 1, "--TonyCode");

}

3. 连接开发板,设置好对应端口号和开发板类型,进行程序下载。

4. 实验现象

LCD1602显示字符「Hello World! --TonyCode」,通过调节电位器可调节显示对比度。

更多内容,欢迎关注我的公众号。 微信扫一扫下方二维码即可关注:

相关推荐

[分享]安装时setup.exe无反应的4种原因与解决
28365备用网址官方网站

[分享]安装时setup.exe无反应的4种原因与解决

📅 06-30 👁️ 900
中国最好的小米产地是哪里?
365bet备用

中国最好的小米产地是哪里?

📅 06-30 👁️ 6184
兰州市苹果手机专卖店地址电话_兰州苹果专卖店服务网点地址查询