#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P2^5;
sbit rw=P2^6;
sbit en=P2^7;
sbit s1=P1^4;
sbit s2=P1^5;
sbit s3=P1^6;
//sbit s4=P1^7;
sbit s4=P3^2;
sbit s5=P3^3;
sbit s6=P3^4;
sbit key1=P1^0;
sbit beep=P2^3;
sbit dscs=P2^0;
sbit dsas=P2^1;
sbit dsrw=P2^2;
sbit dsds=P2^4;
sbit dsirq=P3^2;
uchar code table[]=" 20 - - ";
uchar code table1[]=" : : ";
uchar miao,shi,fen,day,month,year,week,s1num,flag,flag1;
void delay(uint z);
void set_time();
void set_alarm(uchar,uchar,uchar);
void write_ds(uchar,uchar);
uchar read_ds(uchar);
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void init()
{
uchar num;
en=0;
EA=1;
EX0=1;
IT0=1;
set_time();
set_alarm(18,50,0);
write_ds(0x0B,0x26);
read_ds(0x0c);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_data(table1[num]);
delay(5);
}
}
void write_nyr(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_week(uchar week)
{
write_com(0x80+13);
switch(week)
{
case 1:write_data('M');delay(5);
write_data('O');delay(5);
write_data('N');
break;
case 2:write_data('T');delay(5);
write_data('U');delay(5);
write_data('E');
break;
case 3:write_data('W');delay(5);
write_data('E');delay(5);
write_data('D');
break;
case 4:write_data('T');delay(5);
write_data('H');delay(5);
write_data('U');
break;
case 5:write_data('F');delay(5);
write_data('R');delay(5);
write_data('I');
break;
case 6:write_data('S');delay(5);
write_data('A');delay(5);
write_data('T');
break;
case 7:write_data('S');delay(5);
write_data('U');delay(5);
write_data('N');
break;
}
}
void write_ds(uchar add,uchar date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsrw=0;
P0=date;
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)
{
uchar ds_date;
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
ds_date=P0;
dsas=1;
dsds=1;
dscs=1;
return ds_date;
}
void set_time()
{
//write_ds(4,11);
//write_ds(2,17);
write_ds(7,28);
write_ds(8,8);
write_ds(9,12);
write_ds(6,2);
}
void set_alarm(uchar ashi,uchar afen,uchar amiao)
{
//write_ds(9,ayear);
//write_ds(8,amonth);
//write_ds(7,aday);
write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);
}
void keyscan()
{
key1=0;
if(flag1==1)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
flag1=0;
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
flag1=0;
}
}
}
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
flag=1;
flag1=0;
while(!s1);
if(s1num==1)
{
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
if(s1num==4)
{
write_com(0x80+10);
//write_com(0x0f);
}
if(s1num==5)
{
write_com(0x80+7);
}
if(s1num==6)
{
write_com(0x80+4);
}
if(s1num==7)
{
write_com(0x80+13);
}
if(s1num==8)
{
s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(7,day);
write_ds(8,month);
write_ds(9,year);
write_ds(6,week);
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s1num==4)
{
day++;
if(day==32)
day=1;
write_nyr(10,day);
write_com(0x80+10);
}
if(s1num==5)
{
month++;
if(month==13)
month=1;
write_nyr(7,month);
write_com(0x80+7);
}
if(s1num==6)
{
year++;
if(year==99)
year=0;
write_nyr(4,year);
write_com(0x80+4);
}
if(s1num==7)
{
week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+13);
}
}
}
}
if(s1num!=0)
{
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s1num==4)
{
day--;
if(day==0)
shi=31;
write_nyr(10,day);
write_com(0x80+10);
}
if(s1num==5)
{
month--;
if(month==0)
month=12;
write_nyr(7,month);
write_com(0x80+7);
}
if(s1num==6)
{
year--;
if(year==-1)
year=99;
write_nyr(4,year);
write_com(0x80+4);
}
if(s1num==7)
{
week--;
if(week==0)
week=7;
write_week(week);
write_com(0x80+13);
}
}
}
}
}
void didi()
{
beep=0;
delay(50);
beep=1;
delay(100);
beep=0;
delay(50);
beep=1;
}
void main()
{
init();
while(1)
{
keyscan();
if(flag1==1)
didi();
if(flag==0)
{
miao=read_ds(0);
fen=read_ds(2);
shi=read_ds(4);
day=read_ds(7);
month=read_ds(8);
year=read_ds(9);
week=read_ds(6);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
write_nyr(10,day);
write_nyr(7,month);
write_nyr(4,year);
write_week(week);
}
}
}
void delay(uint z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void exter0() interrupt 0
{
uchar c;
flag1=1;
c=read_ds(0x0c);
}