51单片机入门学习--LED流水灯呼吸灯

LED

led:发光二极管,Light Emitting Diode,可用万用表测量正负极
二极管电路图

直插式led,长的一端是正极,短的一端是负极

贴片式led,三角形的尖指向的是负极,有颜色的是负极,底边是正极

开发板的led模块是共阳极的,因此只需要单片机对应I/O口输出低电平就可以点亮

流水灯逐个点亮并且循环

#include <reg52.h>
#include <intrins.h>


void Delayms(int ms)		//1毫秒
{
	unsigned char i, j;
	while(ms)
	{
		_nop_();
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
		ms--;
	}
     

}


void main()
{
	int i = 0;
	P2=0xfe;
	while(1)
	{
		for(i=0;i<8;i++)
		{
			P2=0xfe<<i;//1111  1110
			Delayms(1000);//1秒
		}
		P2=0xff;
		Delayms(1000);
		P2=0xfe;

	}

		
}

单个点亮并且循环,注意_crol_ 函数的使用

#include <reg52.h>
#include <intrins.h>


void Delayms(int ms)		//1毫秒
{
	unsigned char i, j;
	while(ms)
	{
		_nop_();
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
		ms--;
	}
     

}


void main()
{
	int i = 0;
	P2=0xfe;
	Delayms(1000);//1秒
	while(1)
	{
		for(i=0;i<8;i++)
		{
			P2=_crol_(P2,1);//1111  1110
			Delayms(1000);//1秒
		}

	}

		
}

呼吸灯的亮度并不是通过电压控制的,而是控制单位时间内亮灯时间占的比例,所以当占比逐渐增大再逐渐减少,就会有一种亮度的视觉差。
用延时控制的呼吸灯

#include <reg52.h>
#include <intrins.h>

sbit LED0=P2^0;

}
void Delayus(int us)		//10us
{
	unsigned char i;
	while(us)
	{
		i = 2;
		while (--i);
		us--;
	}


}

void main()
{	
	int i = 0;
	while(1)
	{
		for(i=1;i<300;i++)//由暗变亮
		{
			LED0=0;
			Delayus(i);
			LED0=1;
			Delayus(300-i);
		}
		for(i=1;i<300;i++)//由亮变暗
		{
			LED0=1;
			Delayus(i);
			LED0=0;
			Delayus(300-i);
		}
	}
  • 13
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值