標題: 當中斷切換時,從當前燈切換循環(huán)點亮熄滅。我沒懂什么意思,這個意思是指關機后啟... [打印本頁]

作者: nooobaoe    時間: 2025-4-5 16:05
標題: 當中斷切換時,從當前燈切換循環(huán)點亮熄滅。我沒懂什么意思,這個意思是指關機后啟...
老實了,我把程序改了一下讓計數(shù)器做最簡單的計數(shù)工作。
#include <REGX51.H>
void Delay(int ms);           
void Timer0_Init(void);           
void UpdateLED(void);

char LED = 0;
bit direction = 0;           
bit auto_mode = 0;  
unsigned int counter = 0; // 將計數(shù)器移出中斷

void main()
{
    P2 = 0xFF;
    Timer0_Init();  

    while(1)
    {
        if(P3_0 == 0)
        {
            Delay(5);
            if(P3_0 == 0)
            {
                while(P3_0 == 0);
                Delay(5);  
                if(direction != 0)  // 如果當前方向不同,只改變方向
                {
                    direction = 0;
                }  
                auto_mode = 1;     
            }
        }

        if(P3_1 == 0)
        {
            Delay(5);
            if(P3_1 == 0)
            {
                while(P3_1 == 0);
                Delay(5);
                if(direction != 1)  // 如果當前方向不同,只改變方向
                {
                    direction = 1;
                }  
                auto_mode = 1;   
            }
        }

        // 主循環(huán)中處理LED更新
        if(counter >= 10)
        {
            counter = 0;
            if(auto_mode)
            {
                UpdateLED();
            }
        }
    }
}

void Timer0_ISR(void) interrupt 1
{
    TH0 = 0xB0;  
    TL0 = 0x18;

    // 只做最簡單的計數(shù)工作
    counter++;
}

void UpdateLED(void)
{
    if(direction == 0)  // 向右循環(huán)
    {
        LED++;
        if(LED >= 8)
        {
            LED = 0;
        }
    }
    else  // 向左循環(huán)
    {
        if(LED == 0)
        {
            LED = 7;  // 從0減到7,實現(xiàn)循環(huán)
        }
        else
        {
            LED--;
        }
    }
    P2 = ~(0x01 << LED);  // 更新LED顯示
}

void Timer0_Init(void)
{
    TMOD &= 0xF0;  
    TMOD |= 0x01;
    TH0 = 0xB0;   
    TL0 = 0x18;
    ET0 = 1;  //允許定時器0中斷     
    EA = 1;   //開啟總中斷     
    TR0 = 1;      
}

void Delay(int ms)
{
    int i,j;
    while(ms--)
    {
        for(i=1;i<=ms;i++)
            for(j=1;j<=300;j++);
    }
}

現(xiàn)在他加了一條要求。當中斷切換時,從當前燈切換循環(huán)點亮熄滅。我沒懂什么意思,這個意思是指關機后啟動燈還是在那個位置還是什么意思?


作者: hantu    時間: 2025-4-5 17:58
意思可能是,比如從左到右為D1到D8總共8個燈,當前若是在亮D5燈的時候,如果按鍵變成從左到右時是從 D5 D6 這樣亮下去而不是從頭D1開始亮;如果按鍵變成從右到左時從 D5 D4 這樣亮下去而不是從D8開始亮起。具體是不是這樣,你得問出題人,臉皮厚一點,問清楚為止。
作者: GlenXu    時間: 2025-4-17 09:36
其實小學語文基礎很重要,我就不好,導致應用題總是要比別人多費點時間,
雖然我的智商不低,但效率和能力明顯是打折扣的。
作者: WL0123    時間: 2025-4-17 14:27
就是老師舉右手,你從1數(shù)到10循環(huán),老師舉左手,你從10數(shù)到1循環(huán)。如果老師換手,你就得在當前的數(shù)向上或向下數(shù)數(shù),不許從頭數(shù)。




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