单片机源程序如下:
- #include "reg51.h"
- #include "intrins.h"
- sbit EN = P1^0;
- unsigned char Hour=0,Minute=0,Second=0,FLAG = 0;
- unsigned long int count0;
- /************* 数码管数值数组**************/
- unsigned char code Number[]={
- // 0 1 2 3 4 5 6 7 8 9 A B C D E F
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- void Delay100ms();
- void ini_init();
- void display();
- void delay(unsigned int i);
-
- int main()
- {
- char i;
- ini_init();
- while(1)
- {
- display();
- switch(P1)
- {
- case 0XFE:while(P1==0XFE);TR1 = ~TR1; break;
- case 0XFD:while(P1==0XFD);Hour++;Hour>=24?(Hour = 0):(Hour = Hour); break;
- case 0XFB:while(P1==0XFB);Minute++;(Minute>=60)?(Minute = 0):(Minute = Minute); break;
- case 0XF7:while(P1==0XF7);Second++;(Second>=60)?(Second = 0):(Second = Second); break;
- case 0XEF:while(P1==0XEF);Hour = Minute = Second =0;break;
- case 0X7F:while(P1==0X7F);P2=0X00;P0 = 0X00; TR1 = 0;FLAG = ~FLAG;break;
- default:
- break;
- }
- }
- }
- void Delay100ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- _nop_();
- _nop_();
- i = 5;
- j = 52;
- k = 195;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- void ini_init()
- {
- TMOD&=0x00;
- TMOD|=0x20;
- TH1=(256-250)>>8;
- TL1=(256-250);
- TR1=0;
- ET1=1;
- EA = 1; //open global interrupt switch
- }
- void T1_isr() interrupt 3
- {
- if(count0--==0)
- {
- count0 = 4000;
- Second++;
- if(Second>=60)
- {
- Second = 0;
- Minute++;
- if(Minute>=60)
- {
- Minute=0;
- Hour++;
- if(Hour>=24)
- Hour = 0;
- }
- }
-
- }
- }
- void delay(unsigned int i)
- {
- char j;
- for(i; i > 0; i--)
- for(j = 200; j > 0; j--);
- }
- void display()
- {
- if(FLAG == 0)
- {
- P2 = 0X7F;
- P0 = Number[Hour/10];
- delay(100);
-
- P2 = 0XBF;
- P0 = Number[Hour%10];
- delay(100);
-
- P2 = 0XEF;
- P0 = Number[Minute/10];
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
数字电子钟.rar
(27.05 KB, 下载次数: 25)
|