#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit en=P2^0; sbit rs=P2^1; sbit key1=P3^0; sbit key2=P3^1; sbit key3=P3^2; sbit ss=P3^5; uchar code table[]=" 2010-08-10 TUS"; uchar code table1[]=" 00:00:00"; uchar num,count,key1num; char shi,fen,miao; void delay(uint z); void write_com(uchar com); void write_date(uchar date); void init(); void write_sfm(uchar add,uchar date); void keyscan(); void main() { init(); while(1) { keyscan(); } } 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; delay(5); P0=com; en=1; delay(5); en=0; } void write_date(uchar date) { rs=1; delay(5); P0=date; en=1; delay(5); en=0; } void init() { rs=0; en=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x80); for(num=0;num<15;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 timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if(count==20) { count=0; miao++; if(miao==60) { miao=0; fen++; if(fen==60) { fen=0; shi++; if(shi==24) { shi=0; } write_sfm(4,shi); } write_sfm(7,fen); } write_sfm(10,miao); } } 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 keyscan() { ss=0; if(key1==0) { delay(5); if(key1==0); { key1num++; while(!key1); if(key1num==1) { TR0=0; write_com(0x80+0x40+10); write_com(0x0f); } if(key1num==2) { write_com(0x80+0x40+7); } if(key1num==3) { write_com(0x80+0x40+4); } if(key1num==4) { key1num=0; TR0=1; write_com(0x0c); } } } if(key1num!=0) { if(key2==0) { delay(5); if(key2==0) { while(!key2); if(key1num==1) { miao++; write_sfm(10,miao); write_com(0x80+0x40+10); if(miao==60) { miao=0; }
} if(key1num==2) { fen++; write_sfm(7,fen); write_com(0x80+0x40+7); if(fen==0) { fen=0; } } } } if(key1num==3) { shi++; write_sfm(4,shi); write_com(0x80+0x40+4); if(shi==24) { shi=0; } } } if(key3==0) { delay(5); if(key3==0) { while(!key3); if(key1num==1) { miao--; if(miao==-1) miao=59; write_sfm(10,miao); write_com(0x80+0x40+10); } if(key1num==2) { fen--; if(fen==-1) fen=59; write_sfm(7,fen); write_com(0x80+0x40+7); } if(key1num==3) { shi--; if(shi==-1) shi=23; write_sfm(4,shi); write_com(0x80+0x40+4); } } } }
|