標題: 單片機小白求助 有關數(shù)碼管的動態(tài)顯示 [打印本頁]

作者: Zzhanp    時間: 2019-2-22 14:57
標題: 單片機小白求助 有關數(shù)碼管的動態(tài)顯示
剛開始學習51單片機..一步一步照著書上來..
按照書上的程序?qū)懙?按書上的應該是秒表計時的效果...可我的為什么是流水燈效果...
希望各位能幫我看看~ 萬分感激~

#include<reg52.h>

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned char code LedChar[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                                        0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char LedBuff[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
        unsigned char i = 0;
        unsigned int cnt = 0; //記錄T0中斷次數(shù)
        unsigned char flag1s = 0; // 1s定時標志
void main()
{
        unsigned long sec = 0;
        EA = 1;
        TMOD = 0x01;
        TH0 = 0xFC;
        TL0 = 0x67;
        ET0 = 1;
        TR0 = 1;

        while(1)
        {
                if(flag1s == 1) // 判斷1s定時標志
                {
                        flag1s = 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
{
        if(TF0 == 1)
        {
                TF0 = 0;
                TH0 = 0xFC;
                TL0 = 0x67;
                cnt++;
        }

        if(cnt>=1000)
        {
                cnt = 0;
                flag1s = 1;
        }
        P0 = 0xFF;
        switch(i)
                        {
                                case 0:LSA = 0;LSB = 0;LSC = 0;i++;P0 = LedBuff[0];break;/*Y0*/
                                case 1:LSA = 1;LSB = 0;LSC = 0;i++;P0 = LedBuff[1];break;/*Y1*/
                                case 2:LSA = 0;LSB = 1;LSC = 0;i++;P0 = LedBuff[2];break;/*Y2*/
                                case 3:LSA = 1;LSB = 1;LSC = 0;i++;P0 = LedBuff[3];break;/*Y3*/
                                case 4:LSA = 0;LSB = 0;LSC = 1;i++;P0 = LedBuff[4];break;/*Y4*/
                                case 5:LSA = 1;LSB = 0;LSC = 1;i=0;P0 = LedBuff[5];break;/*Y5*/
                                default:break;
                        }
}


作者: Zzhanp    時間: 2019-2-22 15:13
已經(jīng)解決了 中斷函數(shù)那里弄錯了 謝謝大家




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1