×
单片机 > 单片机程序设计 > 详情

51单片机最简LED驱动

发布时间:2020-05-28 发布时间:
|

得到8盏LED交替亮灭的实验效果


#include


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


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
// #include
/** 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


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--);
        }
    }
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机中断源的扩展方法