1、基于定时器的51单片机时钟程序基于定时器的51单片机时钟程序51单片机时钟程序,基于定时器写出,同时还有定时、日历、键盘调节功能 #include #include sbit dula=P26; sbit wela=P27; sbit key1=P34; sbit key2=P35; sbit luis=P36; sbit key4=P37; #define uint unsigned int #define uchar unsigned char uchar code table= 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x7
2、7,0x7c, 0x39,0x5e,0x79,0x71; uchar code table2= 0xbf,0x86,0xdb,0xcf, 0xe6,0xed,0xfd,0x87, 0xff,0xef; uchar a,b,d,shis,ges, shif,gef,shim,gem,shi, fen,miao,shiy,gey,shir, ger,yue,ri; void display1(uchar,uchar, uchar,uchar,uchar,uchar); void display2(uchar,uchar, uchar,uchar); void init(); void keysca
3、n1(); void keyscan2(); void delay(uint z) uint x,y; for(x=z;x0;x-) for(y=110;y0;y-); void main() init(); a=0; miao=0; shi=0; fen=0; yue=1; ri=1; while(1) if(luis=1) display1(shis,ges,shif,gef,shim,gem); keyscan1(); else display2(shiy,gey,shir,ger); keyscan2(); void init() d=0xaa; P1=d; TMOD=0x11; TH
4、0=(65536-45872)/256; TL0=(65536-45872)%256; TH1=(65536-45872)/256; TL1=(65536-45872)%256; EA=1; ET0=1; ET1=1; TR0=1; TR1=1; void display1(uchar shis,uchar ges,uchar shif, uchar gef,uchar shim,uchar gem) dula=1; P0=tableshis; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=table2ges; d
5、ula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=tableshif; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=table2gef; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(1); dula=1; P0=tableshim; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(1); dula=1; P0=tablegem; dula=0
6、; P0=0xff; wela=1; P0=0xdf; wela=0; delay(1); void r0() interrupt 1 TH0=(65536-45872)/256; TL0=(65536-45872)%256; a+; if(a=20) a=0; miao+; if(miao=60) miao=0; fen+; if(fen=60) fen=0; shi+; if(shi=24) shi=0; if(yue=1) ri+; if(ri=32) ri=0; yue=yue+1; if(yue=2) ri+; if(ri=29) ri=0; yue=yue+1; if(yue=3)
7、 ri+; if(ri=32) ri=0; yue=yue+1; if(yue=4) ri+; if(ri=31) ri=0; yue=yue+1; if(yue=5) ri+; if(ri=32) ri=0; yue=yue+1; if(yue=6) ri+; if(ri=31) ri=0; yue=yue+1; if(yue=7) ri+; if(ri=31) ri=0; yue=yue+1; if(yue=8) ri+; if(ri=32) ri=0; yue=yue+1; if(yue=9) ri+; if(ri=31) ri=0; yue=yue+1; if(yue=10) ri+;
8、 if(ri=32) ri=0; yue=yue+1; if(yue=11) ri+; if(ri=31) ri=0; yue=yue+1; if(yue=12) ri+; if(ri=32) ri=0; yue=1; shiy=yue/10; gey=yue%10; shir=ri/10; ger=ri%10; shis=shi/10; ges=shi%10; shim=miao/10; gem=miao%10; shif=fen/10; gef=fen%10; void r1() interrupt 3 TH1=(65536-45872)/256; TL1=(65536-45872)%25
9、6; b+; if(b=20) b=0; d=_crol_(d,1); P1=d; void keyscan1() if(key1=0) delay(5); if(key1=0); shi+; if(shi=24) shi=0; while(!key1); if(key2=0) delay(5); if(key2=0); fen+; if(fen=60) fen=0; while(!key2); if(key4=0) delay(5); if(key4=0); delay(5); while(!key4); TR0=TR0; void display2(uchar shiy,uchar gey
10、, uchar shir,uchar ger) dula=1; P0=tableshiy; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=table2gey; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=tableshir; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=table2ger; dula=0; P0=0xff; wela=1; P0=0xf7
11、; wela=0; delay(1); void keyscan2() if(key1=0) delay(5); if(key1=0); yue+; if(yue=13) yue=1; while(!key1); if(key2=0) delay(10); if(key2=0); delay(5); if(key2=0); if(yue=1) ri+; if(ri=32) ri=1; if(yue=2) ri+; if(ri=29) ri=1; if(yue=3) ri+; if(ri=32) ri=1; if(yue=4) ri+; if(ri=31) ri=1; if(yue=5) ri+; if(ri=32) ri=1; if(yue=6) ri+; if(ri=31) ri=1; if(yue=7) ri+; if(ri=31) ri=1; if(yue=8) ri+; if(ri=32) ri=1; if(yue=9) ri+; if(ri=31) ri=1; if(yue=10) ri+; if(ri=32) ri=1; if(yue=11) ri+; if(ri=31) ri=1; if(yue=12) ri+; if(ri=32) ri=1; while(!key2); while(!key2); while(!key2);