基于单片机的LCD1602控制总线程序
第一行显示"Welcome";第二行显示="Happy day";若要显示其他字符,请直接往数组 LCMLineOne[16]和LCMLineTwo[16]填充相应的代码。直接上图,仿真图如下:
源程序如下,可以对比时序方式,理解总线的操作方法。
#includereg51.h>
//#includeabsaCC.h>
#define uchar unsigned char
#define uint unsigned int
#define busy 0x80
uchar xdata LCMWriteCOM _at_ 0x80ff; //写指令寄存器
uchar xdata LCMReadCOM _at_ 0xa0ff ; //读指令寄存器
uchar xdata LCMWritEDAta _at_ 0xc0ff ; //写数据寄存器
uchar xdata LCMReadData _at_ 0xe0ff ; //读数据寄存器
uchar data LCMLineOne[16]="Welcome"; //第一行显示的数据
uchar data LCMLineTwo[16]="Happy day"; //第二行显示的数据
void Delayms(uchar ms)
{
uchar i,j;
for(i=0;ims;i++)
for(j=0;j57;j++)
;
}
//写指令寄存器
void LCMWriteC(uchar COMData)
{ uchar LCMStatus;
do
{
LCMStatus=(LCMReadCOMbusy);
}
while(LCMStatus!=0);
LCMWriteCOM=COMData;
}
//读指令寄存器
uchar LCMReadC()
{uchar LCMStatus;
do
{
LCMStatus=(LCMReadCOMbusy);
}
while(LCMStatus!=0);
LCMStatus=LCMReadCOM;
return(LCMStatus);
}
//读数据寄存器
uchar LCMReadD(uchar addr)
{uchar LCMStatus;
do
{
LCMStatus=(LCMReadCOMbusy);
}
while(LCMStatus!=0);
LCMWriteC(0x80+addr);
LCMStatus=LCMReadData;
return(LCMStatus);
}
//写数据寄存器带地址
void LCMWriteDAdd(uchar addr,uchar LCMData)
{uchar LCMStatus;
do
{
LCMStatus=(LCMReadCOMbusy);
}
while(LCMStatus!=0);
LCMWriteC(0x80+addr);
LCMWriteData=LCMData;
}
//写数据寄存器无地址
void LCMWriteD(uchar LCMData)
{uchar LCMStatus;
do
{
LCMStatus=(LCMReadCOMbusy);
}
while(LCMStatus!=0);
LCMWriteData=LCMData;
}
//初始化
void LCMInit(void)
{ Delayms(15);
LCMWriteCOM=0x38;
Delayms(5);
LCMWriteCOM=0x38;
Delayms(5);
LCMWriteCOM=0x38;
Delayms(5);
LCMWriteC(0x38);
LCMWriteC(0x08);
LCMWriteC(0x01);
LCMWriteC(0x06);
LCMWriteC(0x0c);
}
main()
{ uchar i;
LCMInit();
while(1)
{ LCMWriteC(0x80); //第一行开始地址
for(i=0;i16;i++)
LCMWriteD(LCMLineOne[i]);
LCMWriteC(0x80+0x40); //第二行开始地址
for(i=0;i16;i++)
LCMWriteD(LCMLineTwo[i]);
}
}
评论