#include <reg51.h> #define LCD_DB P0 sbit LCD_RS = P2^0; sbit LCD_RW = P2^1; sbit LCD_EN = P2^2; void init_lcd(); void write_command(unsigned char command); void write_data(unsigned char data); void display_string(unsigned char x, unsigned char y, unsigned char *string); void delay(unsigned int i); void main() { unsigned char i; unsigned char data_buffer[16] = "Hello, World!"; init_lcd(); display_string(0, 0, data_buffer); // ????? SCON = 0x50; TMOD = 0x20; TH1 = 0xFD; TL1 = 0xFD; TR1 = 1; while(1) { // ?????????? while(RI == 0); RI = 0; // ?????????????? data_buffer[0] = SBUF; // ?LCD????? display_string(0, 0, data_buffer); } } void init_lcd() { write_command(0x38); // ????:8????,2???,5x7???? write_command(0x0C); // ????:???,???,??? write_command(0x06); // ????:?????,????,????? write_command(0x01); // ????:????,???????? delay(10); // ??10?? } void write_command(unsigned char command) { LCD_RS = 0; LCD_RW = 0; LCD_DB = command; LCD_EN = 1; delay(5); LCD_EN = 0; } void write_data(unsigned char data) { LCD_RS = 1; LCD_RW = 0; LCD_DB = data; LCD_EN = 1; delay(5); LCD_EN = 0; } void display_string(unsigned char x, unsigned char y, unsigned char *string) { unsigned char i; if(x < 16) { if(y == 0) { write_command(0x80 + x); } else if(y == 1) { write_command(0xC0 + x); } else { return; } } else { return; } for(i = 0; string[i] != '\0' && i < 16 - x; i++) { write_data(string[i]); } } void delay(unsigned int i) { unsigned int j, k; for(j = 0; j < i; j++) { for(k = 0; k < 1000; k++); } }
时间: 2023-08-23 16:03:50 浏览: 152
duojikongzhi.rar_Unsigned_c++include <reg51>
这是一段基于8051单片机控制的LCD显示程序,可以通过串口输入数据并在LCD上显示。其中使用了函数init_lcd()用于初始化LCD,write_command()用于写入LCD指令,write_data()用于写入LCD数据,display_string()用于在指定位置显示字符串,delay()用于延时。程序通过串口接收数据,将其存储在data_buffer数组中,并在LCD上显示。
阅读全文