|
是用51hei單片機開發板 電路圖詳見:http://www.zg4o1577.cn/f/51hei-5.pdf 的數碼管部分, 用2個74hc573 鎖存,p0口作為數據口 ,p3.6和p3.7分別是段和位的鎖存端口.
- #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 ACT[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- sbit D=P3^6;
- sbit V=P3^7;
- sbit P2_4=P2^4;
- sbit P2_6=P2^6;
- sbit P2_7=P2^7;
- //============
- uchar status_flag;
- uint year;
- uchar month;
- uchar day;
- uchar temp_year_1,temp_month;
- uchar temp_year_h;
- //==============
- void key_S1(void);
- void key_S2(void);
- void key_S4(void);
- void delay(uint k);
- uchar conv(uint year,uchar month);
- //============
- void main(void)
- { uchar i;
- uint temp1,temp2;
- while(1)
- { key_S4();
- switch(status_flag)
- {
- case 0:key_S1();temp1=temp_year_1;
- key_S2();temp2=temp_year_h;break;
- case 1:key_S1();month=temp_month;break;
- default :break;
- }
- year=temp1+(temp2*100);
- //=============
- day=conv(year,month);
- //===============
- for(i=0;i<40;i++)
- {
- switch(status_flag)
- {
- case 0:D=1;P0=SEG7[year%10];D=0;P0=0xff;V=1;P0=ACT[7];V=0;delay(5);
- D=1;P0=SEG7[(year%100)/10];D=0;P0=0xff;V=1;P0=ACT[6];V=0;delay(5);
- D=1;P0=SEG7[(year/100)%10];D=0;P0=0xff;V=1;P0=ACT[5];V=0;delay(5);
- D=1;P0=SEG7[year/1000];D=0;P0=0xff;V=1;P0=ACT[4];V=0;delay(5);break;
- case 1:D=1;P0=SEG7[month%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay(3);
- D=1;P0=SEG7[month/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay(3);break;
- case 2:if(day){D=1;P0=SEG7[day%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay(3);
- D=1;P0=SEG7[day/10];D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay(3);}
- else{D=1;P0=0x00;D=0;P0=0xff;V=1;P0=0xff;V=0;delay(3);
- D=1;P0=0x00;D=0;P0=0xff;V=1;P0=0xff;V=0;delay(3);}break;
- default :break;
- }
- }
- }
- }
- //==============
- void delay(uint k)
- {
- uint i,j;
- for(i=0;i<k;i++){
- for(j=0;j<121;j++)
- {;}}
- }
- //==============
- void key_S1(void)
- {
- P2=0xff;
- if(!P2_4)
- {
- switch(status_flag)
- {
- case 0:temp_year_1++;
- if(temp_year_1>99)temp_year_1=0;break;
- case 1:temp_month++;
- if(temp_month>12)temp_month=1;break;
- default :break;
- }
- }
- }
- //===========
- void key_S2(void)
- {
- P2=0xff;
- if(!P2_7)
- {
- switch(status_flag)
- {
- case 0:temp_year_h++;
- if(temp_year_h>99)temp_year_h=0;break;
- default :break;
- }
- }
- }
- //===============
- void key_S4(void)
- {
- P2=0xff;
- if(!P2_6)status_flag++;
- if(status_flag>2)status_flag=0;
- }
- //=====================
- uchar conv(uint year,uchar month)
- {uchar len;
- switch(month)
- {
- case 1:len=31;break;
- case 3:len=31;break;
- case 5:len=31;break;
- case 7:len=31;break;
- case 8:len=31;break;
- case 10:len=31;break;
- case 12:len=31;break;
- case 4:len=30;break;
- case 6:len=30;break;
- case 9:len=30;break;
- case 11:len=30;break;
- case 2:if(year%4==0&&year%100!=0||year%400==0)len=29;
- else len=28;break;
- default:return 0;break;
- }
- return len;
- }
復制代碼
|
|