|
是用51hei單片機開發板 電路圖詳見:http://www.zg4o1577.cn/f/51hei-5.pdf 的數碼管部分, 用2個74hc573 鎖存,p0口作為數據口 ,p3.6和p3.7分別是段和位的鎖存端口.P2口上面是按鍵
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- //======================
- uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar code ACT[4]={0xfe,0xfd,0xfb,0xf7};
- sbit D=P3^6;
- sbit V=P3^7;
- uint hour;
- uchar min,sec,cnt;
- uchar status;
- void delay(uint k);
- //==========
- union deda
- {
- uint dhour;
- uchar dmin;
- uchar dsec;
- };
- union deda dis_buff;
- //============
- void initial(void)
- {
- TMOD=0x01;
- TH0=-(50000/256);
- TL0=-(50000%256);
- ET0=1;
- TR0=1;
- EA=1;
- }
- //============
- void time0(void) interrupt 1
- {
- TH0=-(50000/256);
- TL0=-(50000%256);
- cnt++;
- if(cnt>=20){sec++;cnt=0;}
- if(sec>=60){min++;sec=0;status++;}
- if(min>=60){hour++;min=0;}
- if(hour>9999){hour=0;}
- if(status>2){status=0;}
- switch(status)
- {
- case 0:dis_buff.dhour=hour;break;
- case 1:dis_buff.dmin=min;break;
- case 2:dis_buff.dsec=sec;break;
- default:break;
- }
- }
- //============
- void main(void)
- {
- initial();
- for(;;)
- {
- switch(status)
- {
- case 0:{D=1;P0=SEG7[dis_buff.dhour%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay(2);
- D=1;P0=SEG7[(dis_buff.dhour%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay(2);
- D=1;P0=SEG7[(dis_buff.dhour%1000)/100];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay(2);
- D=1;P0=SEG7[dis_buff.dhour/1000];D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay(2);}break;
- case 1:{D=1;P0=SEG7[dis_buff.dmin%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay(2);
- D=1;P0=SEG7[dis_buff.dmin/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay(2);}break;
- case 2:{D=1;P0=SEG7[dis_buff.dsec%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay(2);
- D=1;P0=SEG7[dis_buff.dsec/10];D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay(2);}break;
- default:break;
- }
- }
- }
- //==============
- void delay(uint k)
- {
- uint data i,j;
- for(i=0;i<k;i++){
- for(j=0;j<121;j++)
- {;}}
- }
復制代碼
|
|