提供一个8051单片机的程序和电路设计,实现在字符型LCD液晶屏上以左移的效果显示“Welcome to China!We are family!”。
时间: 2024-05-29 09:10:33 浏览: 50
以下是一个8051单片机的程序和电路设计,实现在字符型LCD液晶屏上以左移的效果显示“Welcome to China!We are family!”。
程序:
#include <reg51.h>
#define LCD_data P2
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^2;
void delay(unsigned int t)
{
unsigned int i, j;
for(i=0; i<t; i++)
{
for(j=0; j<1275; j++);
}
}
void LCD_init()
{
RW = 0;
RS = 0;
EN = 0;
delay(20);
LCD_data = 0x38;
EN = 1;
delay(5);
EN = 0;
delay(5);
LCD_data = 0x0C;
EN = 1;
delay(5);
EN = 0;
delay(5);
LCD_data = 0x06;
EN = 1;
delay(5);
EN = 0;
delay(5);
LCD_data = 0x01;
EN = 1;
delay(5);
EN = 0;
delay(5);
}
void LCD_write(unsigned char dat, unsigned char control)
{
RW = 0;
RS = control;
EN = 0;
LCD_data = dat;
EN = 1;
delay(5);
EN = 0;
}
void LCD_display(unsigned char *str)
{
unsigned char i;
while(*str)
{
LCD_write(*str++, 1);
}
}
void LCD_shift(unsigned char row, unsigned char shift)
{
unsigned char i;
for(i=0; i<shift; i++)
{
LCD_write(0x1C, 0);
LCD_write(0x80 + row, 0);
}
}
void main()
{
unsigned char str[] = "Welcome to China! We are family!";
unsigned char i, j;
LCD_init();
while(1)
{
for(i=0; i<strlen(str); i++)
{
LCD_shift(0, 1);
LCD_display(str+i);
delay(500);
}
delay(1000);
LCD_write(0x01, 0);
}
}
电路设计:
本设计使用了基于8051单片机的LCD液晶屏驱动电路,其中P1.0、P1.1和P1.2分别用于RS、RW和EN引脚的控制,P2口用于LCD数据线的控制。其中,LCD液晶屏采用16x2的字符型显示屏,可以显示32个字符。
阅读全文