帮我写写这个电子时钟的一个确定键(退出键)。谢谢谢啊啊
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P2^0;
sbit lcden=P2^2;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit rw=P2^1;
sbit beep=P2^3;
uchar count,s1num,flag;
char miao,shi,fen;
uint xq,ri,yue,nian;
uchar code table[]=" 2014-12-31 MON";
uchar code table1[]=" 23:59:50";
uchar code week[]="MONTHUWEDTUSFRISATSUN";
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*void beeps()
{
beep=1;
delay(5);
beep=0;
} */
void write_com(uchar com)
{
rw=0;
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uchar num;
dula=0;
wela=0;
lcden=0;
nian=2014;
yue=12;
ri=31;
fen=59;
miao=50;
shi=23;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<13;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_week(uint xq)
{
uchar i,j;
i=3*(xq-1);
write_com(0x80+13);
for(j=0;j<3;j++)
{
write_date(week[i+j]);
}
}
void write_nyr(uchar add,uchar dat)
{
uchar shi,ge;
shi=dat/10;
ge=dat%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_n(uchar add,uint dat)
{
uchar qian,bai,shi,ge;
qian=dat/1000;
bai=dat/100%10;
shi=dat/10%10;
ge=dat%100%10;
write_com(0x80+add);
write_date(0x30+qian);
write_date(0x30+bai);
write_date(0x30+shi);
write_date(0x30+ge);
}
void keyscan()
{
beep=0;
if(s1==0)
{
beep=1;
delay(5);
if(s1==0)
{ s1num++;
while(!s1);
beep=0;
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
write_com(0x80+13);
}
if(s1num==5)
{
write_com(0x80+10);
}
if(s1num==6)
{
write_com(0x80+7);
}
if(s1num==7)
{
write_com(0x80+2);
}
if(s1num==8)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
if(s1num!=0)
{
if(s2==0)
{
beep=1;
delay(5);
if(s2==0)
{
while(!s2);
beep=0;
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(11,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(8,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(5,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
xq++;
if(xq==8)
xq=1;
write_week(xq);
}
if(s1num==5)
{
ri++;
/*if(yue<8&&yue>0)
{
if(yue%2)
{
if(ri==32)
ri=1;
}
else if(ri==31)
ri=1;
}
else if(yue<13&&yue>7)
{
if(yue%2)
{
if(ri==31)
ri=1;
}
else if(ri==32)
ri=1;
} */
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==32)
ri=1;
}
if(yue==2)
{
if(nian%400==0||(nian%100!=0&&nian%4==0))
{
if(ri==30)
ri=1;
}
else
{
if(ri==29)
ri=1;
}
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri==31)
ri=1;
}
write_nyr(10,ri);
}
if(s1num==6)
{
yue++;
if(yue==13)
yue=1;
write_nyr(7,yue);
}
if(s1num==7)
{
nian++;
write_n(2,nian);
}
}
}
if(s3==0)
{
beep=1;
delay(5);
if(s3==0)
{
while(!s3);
beep=0;
if(s1num==1)
{
/* if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}*/
miao--;
if(miao==-1)
miao=59;
write_sfm(11,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(8,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(5,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
xq--;
if(xq==0)
xq=7;
write_week(xq);
}
if(s1num==5)
{
ri--;
/* if(yue<8&&yue>0)
{
if(yue%2==1)
{
if(ri==0)
ri=31;
}
if(yue%2==0)
if(ri==0)
ri=30;
}
if(yue<13&&yue>7)
{
if(yue%2==1)
{
if(ri==0)
ri=30;
}
if(yue%2==0)
if(ri==0)
ri=31;
} */
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==0)
ri=31;
}
if(yue==2)
{
if(nian%400==0||(nian%100!=0&&nian%4==0))
{
if(ri==0)
ri=29;
}
else
{
if(ri==0)
ri=28;
}
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri==0)
ri=30;
}
write_nyr(10,ri);
}
if(s1num==6)
{
yue--;
if(yue==0)
yue=12;
write_nyr(7,yue);
}
if(s1num==7)
{
nian--;
write_n(2,nian);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
// while(1);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
xq++;
if(xq==8)
xq=1;
ri++;
/* if(yue<8&&yue>0)
{
if(yue%2==1)
{
if(ri==32)
{
ri=1;
flag=1;
}
}
if(yue%2==0)
{
if(ri==31)
{
ri=1;
flag=1;
}
}
}
if(yue<13&&yue>7)
{
if(yue%2==1)
&n