附录:代码下载:http://www.51hei.com/f/smgpp.rar 部分代码: #include关键字:数码管 电子钟 89C52RC#define uchar unsigned char #define uint unsigned int #define delaytime 4 //译码器的控制位 sbit LA=P2^2; sbit LB=P2^3; sbit LC=P2^4; //键盘的控制位 sbit SetMode=P2^5; sbit Plus=P2^6; sbit Minus=P2^7; sbit duanxuan=P1^0; //键盘的状态位 bit Sethour,Setmin,Setsec; //时间数据位 uchar hour,min,sec,count,state; //共阳数码管数字0~9的码值 uchar code table[11]= { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00 }; void show(); void display_wei(uchar wei) { wei=wei-1; LA=wei%2; LB=(wei/2)%2; LC=(wei/2/2)%2; } void display_duan(uchar duan) { duanxuan=1; P3=duan; duanxuan=0; } void init() { EA=1; TH0=(65536-50000)/256; TL0=(65536-50000)%256; ET0=1; TMOD=0x01; TR0=1; hour=0; min=0; sec=0; count=0; Sethour=0; Setmin=0; Setsec=0; state=1; } void timer() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if(count==10) state=~state; if(count==20) { state=~state; count=0; sec++; } if(sec==60) { sec=0; min++; } if(min==60) { min=0; hour++; } if(hour==24) hour=0; } void delay(uint ms) { unsigned char i; while(ms--) { for(i=0;i<120;i++); } } void show() { if(Sethour==1&&Setmin==0&&Setsec==0&&state==1) { display_wei(1); display_duan(table[10]); delay(delaytime); display_wei(2); display_duan(table[10]); delay(delaytime); } else { display_wei(1); display_duan(table[hour%100/10]); delay(delaytime); display_wei(2); display_duan(table[hour%10]); delay(delaytime); } display_wei(3); display_duan(0x40); delay(delaytime); if(Sethour==1&&Setmin==1&&Setsec==0&&state==1) { display_wei(4); display_duan(table[10]); delay(delaytime); display_wei(5); display_duan(table[10]); delay(delaytime); } else { display_wei(4); display_duan(table[min%100/10]); delay(delaytime); display_wei(5); display_duan(table[min%10]); delay(delaytime); } display_wei(6); display_duan(0x40); delay(delaytime); if(Sethour==1&&Setmin==1&&Setsec==1&&state==1) { display_wei(7); display_duan(table[10]); delay(delaytime); display_wei(8); display_duan(table[10]); delay(delaytime); } else { display_wei(7); display_duan(table[sec%100/10]); delay(delaytime); display_wei(8); display_duan(table[sec%10]); delay(delaytime); } } void process_Plus() { if(Plus==0) { delay(7); if(Plus==0) { while(show(),Plus==0) delay(7); if(Sethour==1&&Setmin==0&&Setsec==0) hour++; if(Sethour==1&&Setmin==1&&Setsec==0) min++; if(Sethour==1&&Setmin==1&&Setsec==1) sec=0; } } } void process_Minus() { if(Minus==0) { delay(7); if(Minus==0) { while(show(),Minus==0) delay(7); if(Sethour==1&&Setmin==0&&Setsec==0) { hour--; if(hour==255) hour=23; } if(Sethour==1&&Setmin==1&&Setsec==0) { min--; if(min==255) min=59; } if(Sethour==1&&Setmin==1&&Setsec==1) { sec=0; } } } } void main() { init(); while(show(),process_Plus(),process_Minus(),1) { if(SetMode==0) { delay(10); if(SetMode==0) { while(show(),SetMode==0); delay(10); if(SetMode==1) { if(Sethour==0&&Setmin==0&&Setsec==1) { Sethour=0; Setmin=0; Setsec=0; } if(Sethour==1&&Setmin==1&&Setsec==1) { Sethour=0; Setmin=0; Setsec=1; } if(Sethour==1&&Setmin==1&&Setsec==0) Setsec=1; if(Sethour==1&&Setmin==0&&Setsec==0) Setmin=1; if(Sethour==0&&Setmin==0&&Setsec==0) Sethour=1; } } } } }
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』