小弟用C语言和C54做了一个时钟,但是出了点问题,望高手指教
程序贴在下面,就是程序开始时,会在00-00-00延时2-3S才会跳到00-00-01。还有就是每当在00-01-00这种秒满60后分进1时,也会停留2S左右才跳到00-01-01。请高手指教。
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit latch1=P2^2;
sbit latch2=P2^3;
uchar tt,shi,fen,flag1,flag2,a,b,c,d,e,f,i,s;
char miao;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
void delay()
{
uint x,y;
for(x=1;x>0;x--)
for(y=0;y<100;y++);
}
void init();
void display(uchar ,uchar ,uchar ,uchar ,uchar,uchar,uchar,uchar);
void main()
{
init();
while(1)
{
if(tt==20)
{
tt=0;
e=miao/10;
f=miao%10;
miao++;
if(miao==61)
{
miao=0;
e=0;
f=0;
flag1=1;
}
}
if(flag1==1)
{
flag1=0;
fen++;
c=fen/10;
d=fen%10;
if(fen==60)
{
fen=0;
c=0;
d=0;
flag2=1;
}
}
if(flag2==1)
{
flag2=0;
shi++;
a=shi/10;
b=shi%10;
if(shi==25)
shi=0;
}
display(a,b,16,c,d,16,e,f);
}
}
void enter0 () interrupt 1
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
tt++;
}
void init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
latch2=1;
P1=0x3f;
latch2=0;
P1=0xff;
latch1=1;
P1=0xdb;
latch1=0;
}
void display(uchar a,uchar b,uchar i,uchar c,uchar d,uchar s,uchar e,uchar f)
{
latch2=1;
P1=table[a];
latch2=0;
P1=0xff;
latch1=1;
P1=0xfe;
latch1=0;
delay();
latch2=1;
P1=table[b];
latch2=0;
P1=0xff;
latch1=1;
P1=0xfd;
latch1=0;
delay();
latch2=1;
P1=table[i];
latch2=0;
P1=0xff;
latch1=1;
P1=0xfb;
latch1=0;
delay();
latch2=1;
P1=table[c];
latch2=0;
P1=0xff;
latch1=1;
P1=0xf7;
latch1=0;
delay();
latch2=1;
P1=table[d];
latch2=0;
P1=0xff;
latch1=1;
P1=0xef;
latch1=0;
delay();
latch2=1;
P1=table[s];
latch2=0;
P1=0xff;
latch1=1;
P1=0xdf;
latch1=0;
delay();
latch2=1;
P1=table[e];
latch2=0;
P1=0xff;
latch1=1;
P1=0xfbf;
latch1=0;
delay();
latch2=1;
P1=table[f];
latch2=0;
P1=0xff;
latch1=1;
P1=0x7f;
latch1=0;
delay();
} |