用软件实现时钟计数,下载到开发板上,数码管上数字一闪一闪的,刚开始学,不知为什么?求助。程序如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned char
uchar code led_7seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60};
uchar hour=23,minute=59,second=50,n=0;
uint count=0;
void init_timer0(void) //timer0每2MS中断一次
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL1=(65536-1000)%256;
ET0=1;
TR0=1;
EA=1;
}
void d1_10ms(void)
{
uchar i;
for(i=200;i>0;i--){}
}
uchar kbscan(void)
{
uchar sccode,recode;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
d1_10ms();
if((P1&0xf0)!=0xf0)
{
sccode=0xfe;
while((sccode&0x10)!=0)
{
P1=sccode;
if((P1&0xf0)!=0xf0)
{
recode=(P1&0xf0)|0xf0;
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return(0);
}
void timer0_int(void) interrupt 1 using 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
count++;
switch(n&&0x07)
{
case 0:{P0=led_7seg[hour/10];P2=0xfe;}break;
case 1:{P0=led_7seg[hour%10];P2=0xfd;}break;
//case 2:{P0=led_7seg[10];P2=0xfb;}break;
case 3:{P0=led_7seg[minute/10];P2=0xf7;}break;
case 4:{P0=led_7seg[minute%10];P2=0xef;}break;
//case 5:{P0=led_7seg[10];P2=0xdf;}break;
case 6:{P0=led_7seg[second/10];P2=0xbf;}break;
case 7:{P0=led_7seg[second%10];P2=0x7f;}break;
default:break;
}
if(count==500) //1MS时间到调整显示时间
{
count=0;
second++;
if(second==60)
{
second=0;
minute++;
}
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
void main(void)
{
uchar key;
init_timer0();
while(1)
{
key=kbscan();
switch(key)
{
case 0x11:{if(hour<23)hour++;else hour=0;}break;
case 0x21:{if(minute<59)minute++;else minute=0;}break;
default:break;
}
}
}
欢迎光临 (http://www.51hei.com/bbs/) | Powered by Discuz! X3.1 |