很困惑的一个问题——数码管秒表

2019-07-15 18:52发布

以下这段程序时间走的比实际的快很多,不像是误差造成的。如在while(1)主循环里面随便加条语句后就正常了,不知道是什么原因.......求解!
-----------------------------------------------------------------
#include<reg51.h>

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;


unsigned char code LedChar[] = {0x3f,0x06,0x5b,0x4f,
                               0x66,0x6d,0x7d,0x07,
                               0x7f,0x6f,0x77,0x7c,
                               0x39,0x5e,0x79,0x71};

unsigned char LedBuff[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned int cnt = 0;
unsigned long sec = 0;

void main()
{
    EA = 1;
    TMOD = 0x01;
    TH0 = 0xFC;
    TL0 = 0x67;
    TR0 = 1;
    ET0 = 1;   

    while(1)
    {

        if(cnt >= 1000)
        {
            cnt = 0;

            sec++;
            LedBuff[0] = LedChar[sec%10];
            LedBuff[1] = LedChar[sec/10%10];
            LedBuff[2] = LedChar[sec/100%10];
            LedBuff[3] = LedChar[sec/1000%10];
            LedBuff[4] = LedChar[sec/10000%10];
            LedBuff[5] = LedChar[sec/100000%10];     
        }
        **如果在这个位置随便加条语句后就正常了**       
    }

}
void Interrupttimer0() interrupt 1
{
    static unsigned char i = 0;   

    TH0 = 0xFC;
    TL0 = 0x67;

    cnt++;

    P0 = 0x00;
    switch(i)
    {
        case 0: ADDR0 = 1; ADDR1 = 1; ADDR2 = 1; i++; P0 = LedBuff[0]; break;
        case 1: ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; i++; P0 = LedBuff[1]; break;
        case 2: ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; i++; P0 = LedBuff[2]; break;
        case 3: ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; i++; P0 = LedBuff[3]; break;
        case 4: ADDR0 = 0; ADDR1 = 1; ADDR2 = 1; i++; P0 = LedBuff[4]; break;
        case 5: ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; i=0; P0 = LedBuff[5]; break;
        default : break;
    }   
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。