#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="00:00:00 XINQI";
uchar code table1[]="2000-01-01 * 1 *";
uchar code table2[]={0x80,0x87,0x86,0x84,0x83,0x81,0x80,
0xc2,0xc3,0xc5,0xc6,0xc8,0xc9,0xcd};
sbit lcde=P2^7;
sbit rd=P2^6;
sbit rw=P2^5;
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
uchar tt,shi,fen,miao,nian,yue=1,ri=1,xinqi=1,temp;
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//写命令
{
lcde=0;
rd=0;
rw=0;
P0=com;
delay(5);
lcde=1;
delay(5);
lcde=0;
}
void write_dat(uchar dat)//写数据
{
lcde=0;
rd=1;
rw=0;
P0=dat;
delay(5);
lcde=1;
delay(5);
lcde=0;
}
void init()
{
uchar num;
lcde=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
for(num=0;num<16;num++)
write_dat(table[num]);
write_com(0xc0);
for(num=0;num<16;num++)
write_dat(table1[num]);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar date)//写时分秒
{
write_com(0x80+add);
write_dat(date/10+'0');
write_dat(date+'0');
}
void write_nyr(uchar addr,uchar dater)//写年月日
{
write_com(0xc0+addr);
write_dat(dater/10+'0');
write_dat(dater+'0');
}
void write_xinqi(uchar addrr,uchar xinqi)//写星期
{
write_com(0xc0+addrr);
write_dat(xinqi+'0');
}
void ksycan()
{
if(k1==0)//功能键
{
delay(5);
if(k1==0)
{
while(!k1);
TR0=0;
temp++;
if(temp==14)
temp=1;
write_com(table2[temp]);//光标定位在秒个位
write_com(0x0f);//开光标闪烁
}
}
if(temp!=0)
{
if(k2==0)//加调整
{
delay(5);
if(k2==0)
{
while(!k2);
if(temp==1)
{
miao=(miao/10)*10+(miao+1);//按一次秒个位+1
write_sfm(6,miao);//刷新秒显示
}
if(temp==2)
{
miao=(miao/10+1)%6*10+(miao);//按一次秒十位+1
write_sfm(6,miao);
}
if(temp==3)
{
fen=(fen/10)*10+(fen+1);
write_sfm(3,fen);
}
if(temp==4)
{
fen=(fen/10+1)%6*10+(fen);
write_sfm(3,fen);
}
if(temp==5)
{
shi=(shi/10)*10+(shi+1);
write_sfm(0,shi);
}
if(temp==6)
{
shi=(shi/10+1)%3*10+(shi);
write_sfm(0,shi);
}
if(temp==7)
{
nian=(nian/10+1)*10+(nian);
write_nyr(2,nian);
}
if(temp==8)
{
nian=(nian/10)*10+(nian+1);
write_nyr(2,nian);
}
if(temp==9)
{
yue++;
if(yue==13)
yue=1;
write_nyr(5,yue);
}
if(temp==10)
{
yue++;
if(yue==13)
yue=1;
write_nyr(5,yue);
}
if(temp==11)
{
ri=(ri/10+1)%4*10+(ri);
write_nyr(8,ri);
}
if(temp==12)
{
ri=(ri/10)*10+(ri+1);
write_nyr(8,ri);
}
if(temp==13)
{
xinqi++;
if(xinqi==8)
xinqi=1;
write_xinqi(13,xinqi);
}
write_com(table2[temp]);//光标定位在当前调整位置
}
}
if(k3==0)//减调整
{
delay(5);
if(k3==0)
{
while(!k3);
if(temp==1)
{
miao=(miao/10)*10+(miao+9);//秒个位-1
write_sfm(6,miao);//刷新秒显示
}
if(temp==2)
{
miao=((miao/10+9))*10+(miao);//秒十位-1
if(miao/10==9)
miao=50+miao;
write_sfm(6,miao);
}
if(temp==3)
{
fen=(fen/10)*10+(fen+9);
write_sfm(3,fen);
}
if(temp==4)
{
fen=((fen/10+9))*10+(fen);
if(fen/10==9)
fen=50+fen;
write_sfm(3,fen);
}
if(temp==5)
{
shi=(shi/10)*10+((shi+9));
write_sfm(0,shi);
}
if(temp==6)
{
shi=((shi/10+9))%7*10+(shi);
write_sfm(0,shi);
}
if(temp==7)
{
nian=(nian/10+9)*10+(nian);
write_nyr(2,nian);
}
if(temp==8)
{
nian=(nian/10)*10+(nian+9);
write_nyr(2,nian);
}
if(temp==9)
{
yue--;
if(yue==0)
yue=12;
write_nyr(5,yue);
}
if(temp==10)
{
yue--;
if(yue==0)
yue=12;
write_nyr(5,yue);
}
if(temp==11)
{
ri=((ri/10+9))%6*10+(ri);
write_nyr(8,ri);
}
if(temp==12)
{
ri=(ri/10)*10+(ri+9);
write_nyr(8,ri);
}
if(temp==13)
{
xinqi--;
if(xinqi==0)
xinqi=7;
write_xinqi(13,xinqi);
}
write_com(table2[temp]);//光标定位在当前调整位置
}
}
}
if(k4==0)//确定键
{
delay(5);
if(k4==0)
{
while(!k4);
temp=0;
write_com(0x0c);//关闭光标闪烁
TR0=1;
}
}
}
void main()
{
init();
while(1)
{
if(tt==20)
{
tt=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
xinqi++;
if(xinqi==8)
xinqi=1;
write_xinqi(13,xinqi);//刷新星期显不
ri++;
if(yue==1)
{
if(ri==32)
{
ri=1;
yue++;
}
}
if(yue==2)
{
if(nian%4==0)//闰年(29天)
{
if(ri==30)
{
ri=1;
yue++;
}
}
else
{
if(ri==29)//平年28天
{
ri=1;
yue++;
}
}
}
if(yue>2)
{
if(yue<=7)//三月到7月
{
if(yue%2==0)//小月30天
{
if(ri==31)
{
ri=1;
yue++;
}
}
else//大月31天
{
if(ri==32)
{
ri=1;
yue++;
}
}
}
if(yue>7)//8到12月
{
if(yue%2==0)//大月31天
{
if(ri==32)
{
ri=1;
yue++;
}
}
else//小月30天
{
if(ri==31)
{
ri=1;
yue++;
}
}
}
}
write_nyr(8,ri);//刷新日显示
if(yue==13)
{
yue=1;
nian++;
if(nian==100)
nian=0;
write_nyr(2,nian);//刷新年显示
}
write_nyr(5,yue);//刷新月显示
}
write_sfm(0,shi);//刷新时显示
}
write_sfm(3,fen);//刷新分显示
}
write_sfm(6,miao);//刷新秒显示
}
ksycan();
}
}
void dingshi() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
tt++;
}