51單片機 + ds12c887時鐘芯片 開發(fā)的電子時鐘
主要代碼如下
void delay(int z)
{
}
void write_com(char com)
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void didi()
{
}
void write_data(char date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
char num;
EA=1;
EX0=1;
IT0=1;
wela=0;
dula=0;
lcden=0;
set_alarm(17,13,30);
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<15;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(table2[num]);
delay(5);
}
}
void write_time(char clock,char address1)
{
char ge,shi;
shi=clock/10;
ge=clock%10;
write_com(0x80+0x40+address1);
write_data(0x30+shi);
delay(10);
write_data(0x30+ge);
}
void keyscan1()
{
if(s17==0)
{
delay(50);
if(s17==0&flag1==1)
{
flag1=0;
}
if((s17==0)&&(s13num==1))
{
if(miao==59)
{
miao=0;
}
miao++;
write_time(miao,0x08);
}
if((s17==0)&&(s13num==2))
{
if(fen==59)
{
fen=0;
}
fen++;
write_time(fen,0x05);
}
if((s17==0)&&(s13num==3))
{
if(shi==23)
{
shi=0;
}
shi++;
write_time(shi,0x02);
}
while(!s17);
}
}
void keyscan2()
{
if(s21==0)
{
delay(50);
if(s21==0&flag1==1)
{
flag1=0;
}
if((s21==0)&&(s13num==1))
{
if(miao==0)
{
miao=59;
}
miao--;
write_time(miao,0x08);
}
if((s21==0)&&(s13num==2))
{
if(fen==0)
{
fen=59;
}
fen--;
write_time(fen,0x05);
}
if((s21==0)&&(s13num==3))
{
if(shi==0)
{
shi=23;
}
shi--;
write_time(shi,0x02);
}
while(!s21);
}
}
void keyscan()
{
rd=0;
if(s13==0)
{
delay(5);
if(s13==0)
{
flag=1;
TR0=0;
s13num++;
}
while(!s13);
if(flag1==1)
{
s13num=4;
flag1=0;
}
write_com(0x0f);
}
if(s13num==1)
{
write_com(0x80+0x40+0x08);
}
if(s13num==2)
{
write_com(0x80+0x40+0x05);
}
if(s13num==3)
{
write_com(0x80+0x40+0x02);
}
if(s13num==4)
{
s13num=0;
flag=0;
write_com(0x0c);
write_ds(4,shi);
write_ds(2,fen);
write_ds(0,miao);
}
keyscan1();
keyscan2();
}
void write_ds(char add,char date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsrw=0;
P0=date;
dsrw=1;
dsas=1;
dscs=1;
}
char read_ds(char add)
{
char ds_date;
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void set_time()
{
write_ds(4,15);
write_ds(2,03);
write_ds(0,50);
}
void set_alarm(char ashi,char afen,char amiao)
{
write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);
}
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);
write_time(miao,0x08);
write_time(fen,0x05);
write_time(shi,0x02);
}
}
}
void ext0() interrupt 0
{
char c;
beep=0;
c=read_ds(0x0C);
flag1=1;
}
評論