02节-51单片机-LED模块

1.点亮一个LED灯

在这里插入图片描述

#include <REGX52.H>
void main()
{
	P2=0xFE; //1111 1110
	while(1){}
}

2.LED闪烁

增加延时,控制LED的亮灭间隙
延时函数的添加依靠STC-ISP软件的延时函数功能代码自动生成,如图
在这里插入图片描述

#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms(void)	//@12.000MHz
{
	unsigned char data i, j, k;

	_nop_();
	i = 4;
	j = 205;
	k = 187;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void main()
{
	while(1)
	{
		P2=0xFE; //1111 1110
		Delay500ms();
		P2=0xFF;  //1111 1111
		Delay500ms();
	}
}

3.LED流水灯

进阶使用位运算来实现流水灯的移动,并设置标志位来判断流动方向。

#include <REGX52.H>
void Delay(unsigned int xms)	//@12.000MHz
{
	unsigned char data i, j;
	while(xms--)
	{
			i = 12;
			j = 169;
			do
			{
				while (--j);
			} while (--i);
	}
}
void main()
{
	int flag=0;
	P2=0xFE;
	Delay(10);
	while(1)
	{
		//方法一:暴力解法
//		P2=0xFE;  //1111 1110
//		Delay(100);
//		P2=0xFD;  //1111 1101
//		Delay(100);
//		P2=0xFB;  //1111 1011
//		Delay(10);
//		P2=0xF7;  //1111 0111	
//		Delay(10);
//		P2=0xEF;  //1110 1111
//		Delay(10);
//		P2=0xDF;  //1101 1111
//		Delay(10);
//		P2=0xBF;  //1011 1111
//		Delay(10);
//		P2=0x7F;  //0111 1111	
//		Delay(10);
		//方法二:该方法,进行改进为乒乓球流水灯
		if(P2==0x7F)
		{
			flag=1;
		}
		if(P2==0xFE)
		{
			flag=0;
		}
		if(flag==0)
		{
			P2<<=1;
			P2+=1;
			Delay(10);
		}
		else
		{
			P2>>=1;
			P2+=128;
			Delay(10);
		}
	}
}
  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值