51单片机串行口实现数据在lcd上显示代码
时间: 2024-03-16 15:43:26 浏览: 12
以下是一个简单的示例代码,用于将串行口接收到的数据显示在LCD上,使用的是51单片机和ST7920 LCD模块:
```c
#include <reg51.h>
#define LCD_RS P0_0
#define LCD_RW P0_1
#define LCD_EN P0_2
#define LCD_DAT P1
void delay(unsigned int t)
{
while(t--);
}
void lcd_init()
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
delay(50000);
LCD_DAT = 0x30;
LCD_EN = 1;
delay(50000);
LCD_EN = 0;
delay(5000);
LCD_DAT = 0x30;
LCD_EN = 1;
delay(5000);
LCD_EN = 0;
delay(1000);
LCD_DAT = 0x30;
LCD_EN = 1;
delay(1000);
LCD_EN = 0;
delay(1000);
LCD_DAT = 0x38;
LCD_EN = 1;
delay(1000);
LCD_EN = 0;
LCD_DAT = 0x08;
LCD_EN = 1;
delay(1000);
LCD_EN = 0;
LCD_DAT = 0x01;
LCD_EN = 1;
delay(1000);
LCD_EN = 0;
LCD_DAT = 0x06;
LCD_EN = 1;
delay(1000);
LCD_EN = 0;
LCD_DAT = 0x0c;
LCD_EN = 1;
delay(1000);
LCD_EN = 0;
}
void lcd_write_byte(unsigned char data, unsigned char rs)
{
LCD_RS = rs;
LCD_RW = 0;
LCD_DAT = data;
LCD_EN = 1;
delay(100);
LCD_EN = 0;
}
void lcd_write_str(unsigned char *str)
{
while(*str)
{
lcd_write_byte(*str, 1);
str++;
}
}
void main()
{
unsigned char buf[16];
unsigned char i = 0;
lcd_init();
while(1)
{
if(RI)
{
buf[i] = SBUF;
i++;
if(i == 16)
{
i = 0;
lcd_write_byte(0x80, 0);
lcd_write_str(buf);
}
RI = 0;
}
}
}
```
这个代码的主要思路是使用串口中断接收数据,并将接收到的数据存储到缓冲区中,当缓冲区满了之后,将数据写入到LCD中进行显示。在代码中,我们使用了ST7920 LCD模块,但具体的写入方式和其他LCD模块也是类似的。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)