【51单片机C语言】1-2使用4个独立按键控制LED灯闪烁方向及步长

 单片机型号:STC89C52RC

开发版:普中51 A2

系统频率:11.0592MHz

使用方法:按下K1为左移模式,按下K2为右移模式,按下K3更改步长为1,按下K4更改步长为2。

#include <REGX52.H>

unsigned char temp=0;	//temp为0x01<<temp中要使用的

void Delay(unsigned int xms)		//Delay函数
{
	unsigned char i, j;

	for(;xms>0;xms--)
	{
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);
	}
}

void Lmove(unsigned char Lstep)	//左移函数
{
	if(Lstep==1)		//左移步数为1
	{
		if(temp==0)
			temp=7;
		else
			temp--;
	}
	if(Lstep==2)		//左移步数为2
	{
		if(temp==0)
			temp=6;
		else if(temp==1)
			temp=7;
		else
			temp-=2;
	}
}

void Rmove(unsigned char Rstep)	//右移函数
{
	if(Rstep==1)		//右移步数为1
	{
		temp++;
		temp%=8;
	}
	if(Rstep==2)		//右移步数为2
	{
		if(temp==6)
			temp=0;
		else if(temp==7)
			temp=1;
		else
			temp+=2;
	}
}
//==========================
void main()
{
	unsigned char step=1;
	while(1)
	{
		P2=~(0x01<<temp);
		Delay(100);
		P2=0xff;
		Delay(100);
			
		if(P3_1==0)		//按下K1 左移模式
		{
			Delay(20);
			while(P3_1==0);
			Delay(20);
			Lmove(step);
		}
			
		if(P3_0==0)		//按下K2 右移模式
		{
			Delay(20);
			while(P3_1==0);
			Delay(20);
			Rmove(step);
		}
			
			
		if(P3_2==0)		//按下K3 更改步长为1
		{
			Delay(20);
			while(P3_2==0);
			Delay(20);
				
			step=1;
		}
		if(P3_3==0)		//按下K4 更改步长为2
		{
			Delay(20);
			while(P3_3==0);
			Delay(20);

			step=2;
		}
	}
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值