#include "1602.h"
#include "DS1302.h"
sbit set=P3^2;
sbit jia1=P3^0;
sbit jian1=P3^1;
uchar slect=0;
uchar bcdtoasc(uchar cha)
{
return ((cha/16)*10+(cha%16));
}
uchar asctobcd(uchar cha)
{
return ((cha/10)*16+(cha%10));
}
void disp_week()
{ switch(week)
{
case 1:{write_lcd(0xcd,tab[0]); break;}
case 2:{write_lcd(0xcd,tab[1]); break;}
case 3:{write_lcd(0xcd,tab[2]); break;}
case 4:{write_lcd(0xcd,tab[3]); break;}
case 5:{write_lcd(0xcd,tab[4]); break;}
case 6:{write_lcd(0xcd,tab[5]); break;}
case 7:{write_lcd(0xcd,tab[6]); break;}
}
}
void disp_time()
{
write_com(0x87);
write_bcd(year);
write_com(0x8a);
write_bcd(mon);
write_com(0x8d);
write_bcd(day);
write_com(0xc5);
write_bcd(hour);
write_com(0xc8);
write_bcd(min);
write_com(0xcb);
write_bcd(sec);
disp_week();
}
void keyscan()
{
set=1;
jia1=1;
jian1=1;
stop_time(1);
if(set==0)
{delay(10);
if(set==0) {
delay(200); slect++;
}
if(slect==8) {slect=0; stop_time(0);write_com(0x0c);}
}
if(slect)
{
switch(slect)
{ case 1:{
write_com(0xcb);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
sec=read_1302(0x81);
sec=sec&0x7f;
sec=bcdtoasc(sec);
sec+=1;
if(sec==60)
sec=0;
sec=asctobcd(sec);
write_1302(0x80,sec);
write_com(0x0c);
write_bcd(sec);
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
sec=read_1302(0x81);
sec=sec&0x7f;
sec=bcdtoasc(sec);
sec-=1;
if(sec<0)sec=59;
sec=asctobcd(sec);
write_1302(0x80,sec);
write_com(0x0c);
write_bcd(sec);
}
}
break;
}
case 2:{write_com(0xc8);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
min=read_1302(0x83);
min=bcdtoasc(min);
min+=1;
if(min==60)
min=0;
min=asctobcd(min);
write_1302(0x82,min);
write_com(0x0c);
write_bcd(min);
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
min=read_1302(0x83);
min=bcdtoasc(min);
min-=1;
if(min<0)min=59;
min=asctobcd(min);
write_1302(0x82,min);
write_com(0x0c);
write_bcd(min);
}
}
break;}
case 3:{write_com(0xc5);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
hour=read_1302(0x85);
hour=bcdtoasc(hour);
hour+=1;
if(hour==24)
hour=0;
hour=asctobcd(hour);
write_1302(0x84,hour);
write_com(0x0c);
write_bcd(hour);
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
hour=read_1302(0x85);
hour=bcdtoasc(hour);
hour-=1;
if(hour<0)hour=23;
hour=asctobcd(hour);
write_1302(0x84,hour);
write_com(0x0c);
write_bcd(hour);
}
}
break;}
case 4:{write_com(0x8d);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
mon=read_1302(0x89);
year=read_1302(0x8d);
day=read_1302(0x87);
day=bcdtoasc(day);
day+=1;
year=bcdtoasc(year);
if(year%4==0)
{
if(mon==2)
{if(day==29)day=1;}
else if(day==30)day=1;
}
else{ /*switch(mon)
{case 1:{if(day==32)day=1;break;}
case 2:{if(day==30)day=1;break;}
case 3:{if(day==32)day=1;break;}
case 4:{if(day==31)day=1;break;}
case 5:{if(day==32)day=1;break;}
case 6:{if(day==31)day=1;break;}
case 7:{if(day==32)day=1;break;}
case 8:{if(day==32)day=1;break;}
case 9:{if(day==31)day=1;break;}
case 0x10:{if(day==32)day=1;break;}
case 0x11:{if(day==31)day=1;break;}
case 0x12:{if(day==32)day=1;break;}
} */
/* if(mon==1|mon==3|mon==5|mon==7|mon==8|mon==0x10|mon==0x12)
if(day==32)day=0; */
if(mon==4|mon==6|mon==9|mon==0x11)
{if(day==31)day=1;}
else if(day==32)day=1;
}
day=asctobcd(day);
write_1302(0x86,day);
write_com(0x0c);
write_bcd(day);
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
day=read_1302(0x87);
day=bcdtoasc(day);
day-=1;
if(day<1)
{ mon=read_1302(0x89);
year=read_1302(0x8d);
switch(mon)
{ case 0x01: {day=31; break;}
case 0x02: {day=29; break;}
case 3: {day=31; break;}
case 4: {day=30; break;}
case 5: {day=31; break;}
case 6: {day=30; break;}
case 7: {day=31; break;}
case 8: {day=31; break;}
case 9: {day=30; break;}
case 0x10: {day=31; break;}
case 0x11: {day=30; break;}
case 0x12: {day=31; break;}
}
year=bcdtoasc(year);
if(year%4==0)if(mon==2)day=28;
}
day=asctobcd(day);
write_1302(0x86,day);
write_com(0x0c);
write_bcd(day);
}
}
break;}
case 5:{write_com(0x8a);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
mon=read_1302(0x89);
mon=bcdtoasc(mon);
mon+=1;
if(mon==13)
mon=1;
mon=asctobcd(mon);
write_1302(0x88,mon);
write_com(0x0c);
write_bcd(mon);
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
mon=read_1302(0x89);
mon=bcdtoasc(mon);
mon-=1;
if(mon<1)mon=12;
mon=asctobcd(mon);
write_1302(0x88,mon);
write_com(0x0c);
write_bcd(mon);
}
}
break;}
case 6:{write_com(0x87);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
year=read_1302(0x8d);
year=bcdtoasc(year);
year+=1;
if(year==100)
year=0;
year=asctobcd(year);
write_1302(0x8c,year);
write_com(0x0c);
write_bcd(year);
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
year=read_1302(0x8d);
year=bcdtoasc(year);
year-=1;
if(year<0)year=99;
year=asctobcd(year);
write_1302(0x8c,year);
write_com(0x0c);
write_bcd(year);
}
}
break;}
case 7:{write_com(0xcd);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
week=read_1302(0x8b);
week=bcdtoasc(week);
week+=1;
if(week==8)
week=1;
week=asctobcd(week);
write_1302(0x8a,week);
write_com(0x0c);
disp_week();
/* switch(week)
{
case 1:{write_lcd(0xcd,tab[0]); break;}
case 2:{write_lcd(0xcd,tab[1]); break;}
case 3:{write_lcd(0xcd,tab[2]); break;}
case 4:{write_lcd(0xcd,tab[3]); break;}
case 5:{write_lcd(0xcd,tab[4]); break;}
case 6:{write_lcd(0xcd,tab[5]); break;}
case 7:{write_lcd(0xcd,tab[6]); break;}
} */
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);
week=read_1302(0x8b);
week=bcdtoasc(week);
week-=1;
if(week<1)week=7;
week=asctobcd(week);
write_1302(0x8a,week);
write_com(0x0c);
disp_week();
/* switch(week)
{
case 1:{write_lcd(0xcd,tab[0]); break;}
case 2:{write_lcd(0xcd,tab[1]); break;}
case 3:{write_lcd(0xcd,tab[2]); break;}
case 4:{write_lcd(0xcd,tab[3]); break;}
case 5:{write_lcd(0xcd,tab[4]); break;}
case 6:{write_lcd(0xcd,tab[5]); break;}
case 7:{write_lcd(0xc