#include //包含头文件
#include
typedef unsigned char BYTE; //数据类型的宏定义
typedef unsigned int WORD;
BYTE count,num;
***it rs = P2^0; //定义液晶的使能信号rs R/W E分别与单片机的P2.0-P2.2相连
***it rw = P2^1;
***it ep = P2^2;
***it keyen=P3^0;
***it keyjia=P3^1;
***it keyjian=P3^2;
BYTE year=13,mon=12,day=30,hour=23,min=59,sec=58,week=7;
void delay(int z)
{ int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
// 延时子程序
}
void lcd_wcmd(BYTE cmd)
{ // 写入指令数据到LCD
rs = 0;
rw = 0;
ep = 0;
delay(2);
P0 = cmd;
delay(2);
ep = 1;
delay(5);
ep = 0;
delay(2);
}
void lcd_wdat(BYTE dat)
{ //写入字符显示数据到LCD
rs = 1;
rw = 0;
ep = 0;
P0 = dat;
delay(2);
ep = 1;
delay(5);
ep = 0;
delay(2);
}
void lcd_***c(BYTE *ch)
{
while(*ch!=0)
lcd_wdat(*ch++);
}
void lcd_ymd(BYTE add,BYTE cmd)
{ BYTE shi,ge;
shi=cmd/10;
ge=cmd%10;
lcd_wcmd(0x80+add);
lcd_wdat(0x30+shi);
lcd_wdat(0x30+ge);
}
void lcd_sfm(BYTE add,BYTE cmd)
{ BYTE shi,ge;
shi=cmd/10;
ge=cmd%10;
lcd_wcmd(0xc0+add);
lcd_wdat(0x30+shi);
lcd_wdat(0x30+ge);
}
void lcd_week( BYTE cmd)
{
lcd_wcmd(0x80+13);
if(cmd==1) lcd_***c("MON");
if(cmd==2) lcd_***c("TUE");
if(cmd==3) lcd_***c("WON");
if(cmd==4) lcd_***c("TRI");
if(cmd==5) lcd_***c("FRI");
if(cmd==6) lcd_***c("STA");
if(cmd==7) lcd_***c("SUN");
}
void lcd_init()
{ //LCD初始化设定
lcd_wcmd(0x38);
delay(1);
lcd_wcmd(0x0c);
delay(1);
lcd_wcmd(0x06);
delay(1);
lcd_wcmd(0x01); //清除LCD的显示内容
delay(1);
lcd_wcmd(0x80);
lcd_***c(" 20 - - ");
lcd_wcmd(0xc4);
lcd_***c(" : : ") ;
lcd_ymd(4,year);
lcd_ymd(7,mon);
lcd_ymd(10,day);
lcd_sfm(4,hour);
lcd_sfm(7,min);
lcd_sfm(10,sec);
lcd_week(week);
}
void keyscan()
{
if(keyen==0)
{
delay(200);
if(keyen==0)
TR0=0;
num++;
switch(num)
{
case 1:
lcd_wcmd(0x0f);
lcd_wcmd(0xc0+11);break;
case 2:
lcd_wcmd(0xc0+8);break;
case 3:
lcd_wcmd(0xc0+5);break;
case 4:
lcd_wcmd(0x80+15);break;
case 5:
lcd_wcmd(0x80+11);break;
case 6:
lcd_wcmd(0x80+8);break;
case 7:
lcd_wcmd(0x80+5);break;
case 8:
TR0=1;
lcd_wcmd(0x0c);
num=0;
break;
}
}
if(num!=0)
{
if(keyjia==0)
{
delay(200);
if(keyjia==0)
switch(num)
{
case 1:
sec++;
if(sec==60)
sec=0;
lcd_sfm(10,sec);
lcd_wcmd(0xc0+11);break;
case 2:
min++;
if(min==60)
min=0;
lcd_sfm(7,min);
lcd_wcmd(0xc0+8);break;
case 3:
hour++;
if(hour==24)
hour=0;
lcd_sfm(4,hour);
lcd_wcmd(0xc0+5);break;
case 4:
week++;
if(week==8)
week=0;
lcd_week(week);
lcd_wcmd(0x80+13);break;
case 5:
day++;
if(day==31)
day=0;
lcd_ymd(10,day);
lcd_wcmd(0x80+11);break;
case 6:
mon++;
if(mon==13)
mon=0;
lcd_ymd(7,mon);
lcd_wcmd(0x80+8);break;
case 7:
year++;
if(year==100)
year=0;
lcd_ymd(4,year);
lcd_wcmd(0x80+5);break;
}
}
}
}
/*
if(num==1) //按键按一下
{
lcd_wcmd(0x0f);//光标显示
lcd_wcmd(0xc0+11);//在秒位闪
}
if(num==2) //按键按俩下
{
lcd_wcmd(0xc0+8);//在分位闪
}
if(num==3) //按键按三下
{
lcd_wcmd(0xc0+5);//在时位闪
}
if(num==4) //按键按四下
{
lcd_wcmd(0x0c);//在个位闪
TR0=1;
num=0;
}*/
main()
{
lcd_init();
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
keyscan();
if(num==0)
{
if(count==20)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{min=0;
hour++;
if(hour==24)
{
hour=0;
day++;
week++;
if(week==8)
week=1;
if(day==31)
{day=0;
mon++;
if(mon==13)
{
mon=1;
year++;
if(year==100)
year=0;
}
lcd_ymd(4,year);
}
lcd_ymd(7,mon);
}
lcd_week(week);
lcd_ymd(10,day);
}
lcd_sfm(4,hour);
}
lcd_sfm(7,min);
}
lcd_sfm(10,sec);
}
}
}
void time0 () interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
} |