利用实验台上的六个数码管及单片机定时器,设计一个电子时钟,在六位数码显示器上实时显示计时值。格式如下:
XX XX XX 由左向右分别为:时、分、秒
程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar Count,s,m,h;
- sbit Dot = P0^7;
- uchar code DSY_CODE[]=
- {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40}; //0~f关
- uchar buff[]={0,0,0x11,0,0,0x11,0,0};
- void DelayMS(uint x)
- {
- uchar i;
- while(--x)
- {
- for(i=0;i<120;i++);
- }
- }
- void main()
- {
- uchar i,j;
- P0 = 0x00;
- P2 = 0xff;
- Count=0;
- s=00;
- m=00;
- h=00;
- TMOD=0x01;
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- IE=0x82;
- TR0=1;
- while(1)
- {
- j = 0x7f;
- buff[7]=s%10;
- buff[6]=s/10;
- buff[4]=m%10;
- buff[3]=m/10;
- buff[1]=h%10;
- buff[0]=h/10;
- for(i=0;i<8;i++)
- {
- j=_crol_(j,1);
- P2=j;
- P0=DSY_CODE[buff[i]];
- DelayMS(2);
- }
- }
- }
- void Time0() interrupt 1
- {
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- if(++Count !=20) return;
- {Count=0;
- s++;
- if (s==60)
- {s=0;
- m++;
- if(m==60)
- {m=0;
- h++;
- if(h==24)
- {h=0;}
- }
- }
- }
- }
复制代码 |