51单片机基本的源码(1602液晶显示)
扫描二维码
随时随地手机看文章
#include
#define uc unsigned char
#define ui unsigned int
sbit RS=P3^5;
sbit RW=P3^6;
sbit E=P3^4;
void read_busy()//判断忙不忙;
{
uc busy;
P0=0xff;//
RS=0;
RW=1;
do
{
E=1;
busy=P0;
E=0;
}
while(busy&0x80);//若忙,则一直循环(0x10000000的1为禁止状态)
}
void write_cmd(uc cmd)//命令函数
{
read_busy();//执行上个语言,判断忙不忙
RS=0;
RW=0;
P0=cmd;
E=1;
E=0;
}
void write_dat(uc dat)//写命令
{
read_busy();
RS=1;
RW=0;
P0=dat;
E=1;
E=0;
}
void main()
{
write_cmd(0x38);//显示模式
write_cmd(0x0f);//开显示,显示光标,并闪烁
write_cmd(0x06);//指针加一,光标加一
write_cmd(0x01);//清0
write_cmd(0x80|0x03);//第一行第四个开始
write_dat(0+'0');//输入的字符
write_dat(0+'1');
write_dat(0+'2');
}