51单片机流水灯方向控制方法详解

用独立按键控制流水灯转向,按下独立按键流水灯向右移动,再按下独立按键流水灯向左移动

此代码用到

逻辑左移:_crol_(移动对象,移动的位数)

逻辑右移:_cror_(移动对象,移动的位数)

#include <REGX52.H>
#include <INTRINS.H>  //导入头文件
unsigned char LEDNum;
unsigned int count;
void Delay1ms(unsigned int xms)		//@11.0592MHz   //延时函数
{
	unsigned char i, j;
	while (xms--)
	{
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);

	}
}

void main()
{
	while(1)
	{
		if(P3_1==0)
		{
			Delay1ms(20);
			while(P3_1==0);
			Delay1ms(20); //软件消抖
			P2=0xFE;
			Delay1ms(500);
			LEDNum = 0xFE;
			while(1)
			{
				while(count == 0) //当count为0时进入此循环
				{
					LEDNum =_crol_(LEDNum,1);
					P2 = LEDNum;
					Delay1ms(500);//1
					if(P3_1==0)
					{
						Delay1ms(20);
						while(P3_1==0);
						Delay1ms(20);
						count = 1; //再次按下K1改变count值使进入逻辑右移
						LEDNum = LEDNum;
					}
				}
				while(count == 1) //当count为1时进入此循环
				{
					LEDNum =_cror_(LEDNum,1);
					P2 = LEDNum;
					Delay1ms(500);
					if(P3_1==0)
					{
						Delay1ms(20);
						while(P3_1==0);
						Delay1ms(20);
						count = 0; //再次按下K1改变count值使进入逻辑左移
						LEDNum = LEDNum;
					}
				}
			}			
		}
	}
}

物联沃分享整理
物联沃-IOTWORD物联网 » 51单片机流水灯方向控制方法详解

发表评论