#include"REG51.H"
#define LCM_Data P0
#define Busy 0x80 /*用于检测LCM状态字中的Busy标识*/
#define XMS 5500
#define uchar unsigned char
#define uint unsigned int
sbit LCM_RW=P2^1; /*定义引脚*/
sbit LCM_RS=P2^0;
sbit LCM_E=P2^2;
sbit WEI_LE=P1^1; //定义位控制线
sbit DUAN_LE=P1^0; //定义段控制线
uchar MScond=0; /*ms*/
uchar Scond=0; /*s*/
uchar Minute=0; /*min*/
uchar Hour=0; /*h*/
uchar buf[8];
void WriteDataLCM(uchar WDLCM);
void WriteCommandLCM(uchar WCLCM,BuysC);
uchar ReadDataLCM(void);
uchar ReadStatusLCM(void);
void LCMInit(void);
void DisplayOneChar(uchar X, uchar Y, uchar DData);
void DisplayStrChar(uchar X, uchar Y, uchar *DData);
void Delay5Ms(void);
void Delay400Ms(void);
uchar code Time[] = {"Now Time:"};
/**************************************************************************/
void timer0(void) interrupt 1 using 0
{
TH0=0xb1;
TL0=0xeb;
MScond=MScond+1;
if(MScond==50)
{
MScond=0;
Scond=Scond+1;
if(Scond==60)
{
Scond=0;
Minute=Minute+1;
if(Minute==60)
{
Minute=0;
Hour=Hour+1;
if(Hour==24)Hour=0;
}
}
}
}
/***************************************************************************/
void BCD(void)
{
buf[7]=Scond%10+0x30;
buf[6]=Scond/10+0x30;
buf[5]=0x3a;
buf[4]=Minute%10+0x30;
buf[3]=Minute/10+0x30;
buf[2]=0x3a;
buf[1]=Hour%10+0x30;
buf[0]=Hour/10+0x30;
}
/*************************************************************************/
void main(void)
{
uchar TMP;
TMOD=0x01;
TH0=0xb1;
TL0=0xeb;
TR0=1;
ET0=1;
EA=1;
P0=0xFF;
DUAN_LE=1;
DUAN_LE=0;
P0=0x00;
WEI_LE=1;
WEI_LE=0; /*关闭锁存器*/
Delay400Ms(); /*启动等待,等LCM讲入工作状态*/
LCMInit(); /*LCM初始化*/
Delay5Ms(); /*延时片刻(可不要)*/
DisplayStrChar(0, 0, Time);
while(1)
{if(TMP!=Scond){TMP=Scond;BCD();DisplayStrChar(8, 1, buf);}}
}
/*写数据*/
void WriteDataLCM(uchar WDLCM)
{
ReadStatusLCM(); /*检测忙*/
LCM_Data=WDLCM;
LCM_RS=1;
LCM_RW=0;
LCM_E=0; /*若晶振速度太高可以在这后加小的延时*/
LCM_E=0; /*延时*/
LCM_E=1;
}
/*写指令*/
void WriteCommandLCM(uchar WCLCM,BuysC) /*BuysC为0时忽略忙检测*/
{
if (BuysC) ReadStatusLCM(); /*根据需要检测忙*/
LCM_Data=WCLCM;
LCM_RS=0;
LCM_RW=0;
LCM_E=0;
LCM_E=0;
LCM_E=1;
}
/*读数据*/
uchar ReadDataLCM(void)
{
LCM_RS=1;
LCM_RW=1;
LCM_E=0;
LCM_E=0;
LCM_E=1;
return(LCM_Data);
}
/*读状态*/
uchar ReadStatusLCM(void)
{
LCM_Data=0xFF;
LCM_RS=0;
LCM_RW=1;
LCM_E=0;
LCM_E=0;
LCM_E=1;
while (LCM_Data & Busy); /*检测忙信号*/
return(LCM_Data);
}
void LCMInit(void) /*LCM初始化*/
{
LCM_Data=0;
WriteCommandLCM(0x38,0); /*三次显示模式设置,不检测忙信号*/
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,1); /*显示模式设置,开始要求每次检测忙信号*/
WriteCommandLCM(0x08,1); /*关闭显示*/
WriteCommandLCM(0x01,1); /*显示清屏*/
WriteCommandLCM(0x06,1); /* 显示光标移动设置*/
WriteCommandLCM(0x0C,1); /* 显示开及光标设置*/
}
/*按指定位置显示一个字符*/
void DisplayOneChar(uchar X, uchar Y, uchar DData)
{
Y&=0x1;
X&=0xF; /*限制X不能大于15,Y不能大于1*/
if (Y)X |=0x40; /*当要显示第二行时地址码+0x40;*/
X|=0x80; /*算出指令码*/
WriteCommandLCM(X, 0); /*这里不检测忙信号,发送地址码*/
WriteDataLCM(DData);
}
/*按指定位置显示一串字符*/
void DisplayStrChar(uchar X, uchar Y, uchar *DData)
{
uchar ListLength;
ListLength=0;
Y&=0x1;
X&=0xF; /*限制X不能大于15,Y不能大于1*/
while(DData[ListLength]>0) /*若到达字串尾则退出*/
{
if (X