这是一个简单的用51单片机制作的电子时钟,请各位大神帮我看看有什么错的没有,有没有值得改进的地方,先谢谢了。
#include <reg51.h>
#define SEG P1 //数码管接p1管脚
#define scanp P2 //扫描接p2管脚
int scan=1;
#define countt0 50000 //T0,mode1,0.05秒 12Mhz
#define th_t0 (65636-countt0)/256
#define tl_t0 (65636-countt0)%256
int count_t0=0;
#define countt1 250 //T1,mode2,0.25ms 12Mhz
#define th_t1 (256-countt1)
#define tl_t1 (256-countt1)
int count_t1=0;
char code tab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};// 共阳数码管
char second=0,minute=0,hour=0;
char disp[6]={second/10,second%10,minute/10,minute%10,hour/10,hour%10};
int i=0;
main()
{
IE=0x8a; // 开TF0,TF1中断
TMOD=0x21;
TR0=1;//启动T1
TH0=th_t0;
TL0=tl_t0;
TR1=1;//启动T2
TH1=th_t1;
TL1=tl_t1;
while(1);
}
void seg(void) interrupt1
{
count_t0++;
if(count_t0==20)
{
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==60)
{
hour=0;
}
}
}
}
}
void disp(void) interrupt3
{
if(count_t1==32)
{
count_t1=0;
SEG=0xff;
scanp=~scan;
SEG=disp[i];
i++;
scan<<=1;
}
}
一周热门 更多>