LCD1602读写操作
时间: 2023-09-14 13:14:44 浏览: 31
LCD1602是一种常见的字符型液晶显示模块,可以显示2行16列的字符。下面是LCD1602的读写操作:
1. 初始化LCD1602
在使用LCD1602前,需要先初始化LCD1602。初始化的过程如下:
```
void initLCD1602()
{
//设置数据总线为输出模式
DDRD = 0xFF;
//延时一段时间
_delay_ms(100);
//设置RS和RW为0,E为1
PORTB &= ~(1<<RS);
PORTB &= ~(1<<RW);
PORTB |= (1<<E);
//发送命令,设置工作模式,显示模式等
LCD_sendCommand(0x38);
LCD_sendCommand(0x0C);
LCD_sendCommand(0x06);
LCD_sendCommand(0x01);
//延时一段时间
_delay_ms(10);
}
```
2. 发送命令
发送命令用于设置LCD1602的工作模式、显示模式等。发送命令的函数如下:
```
void LCD_sendCommand(unsigned char command)
{
//设置RS为0,RW为0,E为1
PORTB &= ~(1<<RS);
PORTB &= ~(1<<RW);
PORTB |= (1<<E);
//发送命令
PORTD = command;
//延时一段时间
_delay_us(50);
//设置E为0
PORTB &= ~(1<<E);
//延时一段时间
_delay_us(50);
}
```
3. 发送数据
发送数据用于向LCD1602中写入要显示的字符。发送数据的函数如下:
```
void LCD_sendData(unsigned char data)
{
//设置RS为1,RW为0,E为1
PORTB |= (1<<RS);
PORTB &= ~(1<<RW);
PORTB |= (1<<E);
//发送数据
PORTD = data;
//延时一段时间
_delay_us(50);
//设置E为0
PORTB &= ~(1<<E);
//延时一段时间
_delay_us(50);
}
```
4. 清屏
清屏用于清除LCD1602中的所有字符。清屏的函数如下:
```
void LCD_clear()
{
//发送命令,清屏
LCD_sendCommand(0x01);
//延时一段时间
_delay_ms(10);
}
```
5. 设置光标位置
设置光标位置用于将光标移动到指定的位置。设置光标位置的函数如下:
```
void LCD_setCursor(unsigned char row, unsigned char col)
{
//计算光标位置
unsigned char position = 0;
if(row == 0)
{
position = col;
}
else if(row == 1)
{
position = col + 0x40;
}
//发送命令,设置光标位置
LCD_sendCommand(0x80 | position);
}
```
以上就是LCD1602的读写操作。