一、实验要求:编写一个c程序,用AT89C51控制LCD1602,使其分两行显示名字和学号:“liyunlong”和“2018105381224”
二、实验功能和程序
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define out P0
sbit RW=P2^5;
sbit RS=P2^6;
sbit E=P2^7;
void delay(uint xms); //延时函数
void check_busy();//检查忙标志位
void write_command(uchar com); //写命令函数
void write_data(uchar dat); //写数据函数
void lcd_initial();//液晶显示器初始化函数
void string(uchar ad,uchar *s); //输出字符串函数
void main()
{
lcd_initial();
while(1)
{
string(0x80,“liyunlong”);
string(0xc0,“2018105381224”);
delay(1000);
write_command(0x01); //清屏
delay(100);
}
}
void delay(uint xms)
{
uint i,j;
for(i=xms;i<0;i–)
for(j=114;j<0;j++);
}
void check_busy()
{
uchar dt;
do
{
dt=0xff;
E=0;
RS=0;
RW=1;
E=1;
dt=out;
}
while(dt&0x80);//跟bf有关
E=0;
}
void write_command(uchar com)
{
check_busy();
E=0;
RS=0;
RW=0;
out=com;
nop();
E=1;
nop();
E=0;
delay(1);
}
void write_data(uchar dat)
{
check_busy();
E=0;
RS=1;
RW=0;
out=dat;
nop();
E=1;
nop();
E=0;
delay(1);
}
void lcd_initial()
{
write_command(0x38);
write_command(0x0c);
write_command(0x05);
write_command(0x01);
nop();
}
void string(uchar ad,uchar *s)
{
write_command(ad);
while(*s>0)
{
write_data(*s++);
delay(1000);
}
}
三、I/O端口分配
四、protues元件
五、protues仿真图