51單片機 LED 燈

得到8盞LED交替亮滅的實驗效果

#include <reg51.h>

void Delay10ms(unsigned int c); // 延時10ms

void main() 
{
    while(1) 
    {
        P0 = 0x00; // 設置 P0 爲低電平
        Delay10ms(50); // 延時
        P0 = 0xff; // 設置 P0 爲高電平
        Delay10ms(50); // 延時
    }
}

void Delay10ms(unsigned int c)
{
    unsigned char a, b;
    for (; c > 0; c--)
    {
        for (b = 38; b > 0; b--)
        {
            for (a = 130; a > 0; a--);
        }
    }
}

LED燈做二進制加1顯示

#include <reg51.h>

void Delay10ms(unsigned int c); // 延時10ms

void main() 
{
    unsigned char count = 0x00;
    while(1) 
    {
        P0 = count;
        Delay10ms(50); // 延時
        count++;
    }
}

void Delay10ms(unsigned int c)
{
    unsigned char a, b;
    for (; c > 0; c--)
    {
        for (b = 38; b > 0; b--)
        {
            for (a = 130; a > 0; a--);
        }
    }
}

延時實現LED流水燈效果P2口八個燈作跑馬燈

#include <reg51.h>
// #include <intrins.h>
/** intrins.h 方法
_crol_ 字符循環左移
_cror_ 字符循環右移
_irol_ 整數循環左移
_iror_ 整數循環右移
_lrol_ 長整數循環左移
_lror_ 長整數循環右移
_nop_ 空操作8051 NOP 指令
_testbit_ 測試並清零位8051 JBC 指令函數名: _crol_,_irol_,_lrol_
*/

void Delay10ms(unsigned int c); // 延時10ms

void main() 
{
    unsigned char LED;
    LED = ~0xfe;   // 0xfe = 1111 1110
    while(1) 
    {
        P0 = LED;
        Delay10ms(50); // 延時
        LED <<= 1;
        if (P0 == 0x00)
        {
            LED = ~0xfe;
        }
        // LED = _crol_(LED, 1);
    }
}

void Delay10ms(unsigned int c)
{
    unsigned char a, b;
    for (; c > 0; c--)
    {
        for (b = 38; b > 0; b--)
        {
            for (a = 130; a > 0; a--);
        }
    }
}

LED燈做跑馬燈左右移動

#include <reg51.h>

void Delay10ms(unsigned int c); // 延時10ms
unsigned char LED;
void main() 
{
    unsigned char i;
    LED = 0xfe; // 0xfe = 1111 1110
    while(1) 
    {
        for (i = 0; i < 7; i++) 
        {
            P0 = LED;
            Delay10ms(50);
            LED <<= 1;
            LED |= 0x01;
        }
        for (i = 0; i < 7; i++) 
        {
            P0 = LED;
            Delay10ms(50);
            LED >>= 1;
            LED |= 0x80;
        }

    }
}

void Delay10ms(unsigned int c)
{
    unsigned char a, b;
    for (; c > 0; c--)
    {
        for (b = 38; b > 0; b--)
        {
            for (a = 130; a > 0; a--);
        }
    }
}
發佈了39 篇原創文章 · 獲贊 21 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章