单片机基础强化第一课 简单设计一个LED流水灯

代码如下:

#include <reg51.h>



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


void main() 
{
    while(1) 
    {
        P0 = 0x00; // 设置 P0 为低电平
        Delay10ms(250); // 延时
        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--);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值