基于51單片機的數字時鐘
0.png (29.3 KB, 下載次數: 64)
下載附件
2017-5-14 18:11 上傳
0.png (65.41 KB, 下載次數: 62)
下載附件
2017-5-14 18:11 上傳
單片機源程序如下:
- #include<reg51.h>
- char code led_mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- int second,min,hour;
- int setmin=0,sethour=0;
- int currentmin,currenthour;
- bit flag=0;
- bit timeflag=1;
- bit time24to12=1;
- bit amorpm=0;
- sbit P16=P1^6;
- unsigned char status;
- void delay(int time)
- {
- int i;
- for(;time>0;time--)
- for(i=0;i<250;i++);
- }
- int0_key() interrupt 0{
- flag=1;
- status=P0;
- }
- void sound(int n)
- {
- int i=0;
- P16=1;
- while(i<n)
- {
- P16=!P16;
- i++;
- }
- }
- void main()
- {
- int time;
- IP=0x01;
- IT0=1;
- EX0=1;
- EA=1;
-
- second=50;
- currentmin=58;
- min=currentmin;
- currenthour=20;
- hour=currenthour;
-
- while(1)
- {
- time=0;
- if(second>59)
- {
- second=0;
- currentmin++;
- min++;
- }
- if(min>59)
- {
- min=0;
- hour++;
- }
- if(hour>23)
- {
- hour=0;
- }
- if(currentmin>59)
- {
- currentmin=0;
- sound(10);
- currenthour++;
- }
- if(currenthour>23)
- {
- currenthour=0;
- }
- while(time<50)
- {
- P1=0x01;
- P2=~led_mod[second/10];
- delay(1);
- P1=0x02;
- P2=~led_mod[second%10];
- delay(1);
- P1=0x04;
- P2=~led_mod[min/10];
- delay(1);
- P1=0x08;
- P2=~led_mod[min%10];
- delay(1);
- P1=0x10;
- P2=~led_mod[hour/10];
- delay(1);
- P1=0x20;
- P2=~led_mod[hour%10];
- delay(1);
- time++;
- }
- P1=0x00;
- P2=0x00;
- if(!timeflag)
- delay(100);
- second++;
- if(currentmin==setmin&¤thour==sethour)
- {
- sound(10);
- }
- if(flag)
- {
- switch(status){
- case 0:min++;
- status=0x06;
- break;
- case 1:hour++;
- status=0x06;
- break;
- case 2:if(timeflag)
- {
- currentmin=min;
- min=setmin;
- currenthour=hour;
- hour=sethour;
- }
- else
- {
- setmin=min;
- min=currentmin;
- sethour=hour;
- hour=currenthour;
- }
- timeflag=!timeflag;
- status=0x06;
- break;
- case 3:if(time24to12)
- {
- if(currenthour>12)
- amorpm=0;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
C51的數字時鐘.rar
(16.92 KB, 下載次數: 70)
2017-5-14 17:49 上傳
點擊文件名下載附件
數字時鐘 下載積分: 黑幣 -5
|