#include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit dula=P2^6; sbit wela=P2^7; sbit rs=P3^5; sbit lcden=P3^4; sbit key1=P3^0; sbit key2=P3^1; sbit key3=P3^2; sbit rd=P3^7; uchar tt,num,a,b,k1num; int miao=0,shi=0,fen=0,nian=0,yue=1,ri=1,xingqi=6; uchar code table[]=" 2000-01-01 SAT"; uchar code table1[]=" 00:00:00"; uchar code table2[]="MONTUEWEDTHUFRISATSUN"; void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); }
void write_com(uchar com) { 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; 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<12;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_nyr(uchar add,uchar date) { uchar shi,ge; shi=date/10; ge=date%10; write_com(0x80+add); write_date(0x30+shi); write_date(0x30+ge); }
void regulation_week() { if(yue<3) {if(nian==0) {xingqi=(99+99/4+19/4-2*19+26*(yue+13)/10+ri-1); } else {xingqi=((nian-1)+(nian-1)/4+20/4-2*20+26*(yue+13)/10+ri-1); } } else {xingqi=(nian+nian/4+20/4-2*20+26*(yue+1)/10+ri-1); } while(xingqi<0) {xingqi=xingqi+7; } xingqi=xingqi%7; if(xingqi==0) xingqi=7; } void keyscan( ) { rd=0; if(key1==0) { delay(5); if(key1==0) { k1num++; while(!key1); if(k1num==1) { TR0=0; write_com(0x80+5); write_com(0x0f); } if(k1num==2) { write_com(0x80+8); } if(k1num==3) { write_com(0x80+11); } if(k1num==4) { write_com(0x80+0x40+10); } if(k1num==5) { write_com(0x80+0x40+7); } if(k1num==6) { write_com(0x80+0x40+4); } if(k1num==7) { k1num=0; write_com(0x0c); TR0=1; }
} } if(k1num!=0) { if(key2==0) { delay(5); if(key2==0) { while(!key2); if(k1num==1) { nian++; if(nian%4!=0&&ri>28) { ri=28; write_nyr(10,ri); } write_nyr(4,nian); regulation_week(); write_com(0x80+13); a=(xingqi-1)*3; write_date(table2[a]); write_date(table2[a+1]); write_date(table2[a+2]); write_com(0x80+5); } if(k1num==2) { yue++; if(yue==13) yue=1; if(yue==2) { if(nian%4==0&&ri>29) ri=29; else if(ri>28) ri=28; write_nyr(10,ri); } else if((yue==4||yue==6||yue==9||yue==11)&&ri>30) { ri=30; write_nyr(10,ri); } write_nyr(7,yue); regulation_week(); write_com(0x80+13); a=(xingqi-1)*3; write_date(table2[a]); write_date(table2[a+1]); write_date(table2[a+2]); write_com(0x80+8); } if(k1num==3) { ri++; if(yue==2) { if(nian%4==0) { if(ri==30) ri=0; } else { if(ri==29) ri=0; } } else if(yue==4||yue==6||yue==9||yue==11) { if(ri==31) ri=1; } else { if(ri==32) ri=1; } write_nyr(10,ri); regulation_week(); write_com(0x80+13); a=(xingqi-1)*3; write_date(table2[a]); write_date(table2[a+1]); write_date(table2[a+2]); write_com(0x80+11); } if(k1num==4) { miao++; if(miao==60) miao=0; write_sfm(10,miao); write_com(0x80+0x40+10); } if(k1num==5) { fen++; if(fen==60) fen=0; write_sfm(7,fen); write_com(0x80+0x40+7); } if(k1num==6) { shi++; if(shi==24) shi=0; write_sfm(4,shi); write_com(0x80+0x40+4); }
} } if(key3==0) { delay(5); if(key3==0) { while(!key3); if(k1num==2) { yue--; if(yue==0) yue=12; if(yue==2) { if(nian%4==0&&ri>29) ri=29; else if(ri>28) ri=28; write_nyr(10,ri); } else if((yue==4||yue==6||yue==9||yue==11)&&ri>30) { ri=30; write_nyr(10,ri); } write_nyr(7,yue); regulation_week(); write_com(0x80+13); a=(xingqi-1)*3; write_date(table2[a]); write_date(table2[a+1]); write_date(table2[a+2]); write_com(0x80+8); } if(k1num==3) { ri--; if(ri==0) { if(yue==2) { if(nian%4==0) ri=29; else ri=28; } else if(yue==4||yue==6||yue==9||yue==11) { ri=30; } else { ri=31; } } write_nyr(10,ri); regulation_week(); write_com(0x80+13); a=(xingqi-1)*3; write_date(table2[a]); write_date(table2[a+1]); write_date(table2[a+2]); write_com(0x80+11); } if(k1num==4) { miao--; if(miao==-1) miao=59; write_sfm(10,miao); write_com(0x80+0x40+10); } if(k1num==5) { fen--; if(fen==-1) fen=59; write_sfm(7,fen); write_com(0x80+0x40+7); } if(k1num==6) { shi--; if(shi==-1) shi=23; write_sfm(4,shi); write_com(0x80+0x40+4); } } } }
} void main() { init(); while(1) { keyscan(); }
}
void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt++; if(tt==18) { tt=0; miao++; if(miao==60) { miao=0; fen++; if(fen==60) { fen=0; shi++; if(shi==24) { shi=0; ri++; write_com(0x80+13); xingqi++; if(xingqi==8) xingqi=1; a=(xingqi-1)*3; write_date(table2[a]); write_date(table2[a+1]); write_date(table2[a+2]); if(yue==2) { if(nian%4==0) { if(ri==30) { ri=0; yue++; if(yue==13) { yue=1; nian++; write_nyr(7,yue); write_nyr(4,nian); } else { write_nyr(7,yue); } } } else { if(ri==29) { ri=0; yue++; if(yue==13) { yue=1; nian++; write_nyr(7,yue); write_nyr(4,nian); } else { write_nyr(7,yue); } } } } else if(yue==4||yue==6||yue==9||yue==11) { if(ri==31) { ri=1; yue++; if(yue==13) { yue=1; nian++; write_nyr(7,yue); write_nyr(4,nian); } else { write_nyr(7,yue); } } } else { if(ri==32) { ri=1; yue++; if(yue==13) { yue=1; nian++; write_nyr(7,yue); write_nyr(4,nian); } else { write_nyr(7,yue); } } } write_nyr(10,ri); } write_sfm(4,shi); } write_sfm(7,fen); } write_sfm(10,miao); } } |