51 single-chip microcomputer--use independent buttons to control LED

The principle of independent keys

insert image description here

An independent button is a basic electronic component, which usually consists of a button and two pins. In the single-chip microcomputer, we can connect a pin of the button to an IO port, and monitor the status of the button through the corresponding program.
insert image description here
We can see that,One foot of an independent button is grounded, and the other foot is connected to the microcontroller register;
K1 key is controlled by P31, and K2 is controlled by P30 . This is due to the negligence of the designer, so when we want to write a program, we must remember to write these two in reverse.
insert image description here
The above figure is the pin interface corresponding to the microcontroller, RXD means receiving data, TXD means sending data;The independent button defaults to a high level, that is, if it is not pressed, it is a high level, and if it is pressed, it is a low level.
Let's look at an example:

#include <REGX52.H>
int main()
{
    
    

	while(1)
	{
    
    
		if(P3_1==0)
		{
    
    
			P2_3=0;
		}
		else
		{
    
    
			P2_3=1;
		}
	}
}
  P3_1表示第一个按键,在循环里面,当它处于低电平时,也就是我们按下去
  时,那么第三个LED灯将会亮起,松手就会熄灭;倘若没有在循环里面,我
  们只有在复位之前一直按住按键,那么LED灯将会一直亮起,如果复位前没
  有按下去,那么LED灯将会一直不亮。

Independent button to control the state of the LED light

key shake

Here, the individual keys areMechanical elastic switch, when the contact of the button is closed and disconnected, due to the elastic effect, the independent button cannot be kept stable immediately, and it needs to wait for a certain period of time to keep stable, generally speaking, this jitter is around 10-20ms.
insert image description here
In order to maintain this stability, we need to eliminate this jitter. Generally speaking, there are two ways, one is through hardware:Connect a capacitor to the circuit; The other is software debounce,Add 10ms delay based on experience. For us, we can debounce through the program.

Control the status of LED lights

Here, what we have to do is to use independent buttons,Press it once to turn on the light, press it again to turn it off, and so on. Just like on a computer webpage, press the left mouse button in the upper right corner, and the webpage will be closed after letting go. So, we still have a problem here, we don’t know how long everyone let go, but we know that if they don’t let go, then the button will remain in a low level state, then we can use a loop to determine whether to let go.
code show as below:

#include <REGX52.H>
void Delay(unsigned int x)		//@11.0592MHz
{
    
    
	unsigned char i, j;

	while(x--)
	{
    
    
	
		i = 2;
		j = 199;
		do
		{
    
    
			while (--j);
		} while (--i);
	}
}

void main()
{
    
    
	while(1)
	{
    
    
		if(P3_1==0)
		{
    
    
			Delay(20);//抖动消除
			while(P3_1==0);//检测松手
			Delay(20);//抖动消除
			P2_0=~P2_0;//按位取反
		
		}
	}
}
			

 Delay是一个以ms为单位的延迟函数,可以输入参数来确定时间长短;当用户
 按下去时也就是P3_1==0时,我们要先做的是抖动消除,接着是检测松手,
 没有松手就一直保持低电平状态,当松手之后,又会产生抖动,故又需要抖
 动消除。~是二进制中按位取反的意思,如果是0,按位取反之后就是1,如
 果是1,按位取反后就是0,这刚好就是能改变独立按键高低电平的状态。

Independent key control binary

code show as below:

#include <REGX52.H>
void Delay(unsigned int x)		//@11.0592MHz
{
    
    
	unsigned char i, j;

	while(x--)
	{
    
    
	
		i = 2;
		j = 199;
		do
		{
    
    
			while (--j);
		} while (--i);
	}
}
void main()
{
    
    
	unsigned char num=0;
	while(1)
	{
    
    
		if(P3_1==0)
		{
    
    
			Delay(20);//消除抖动
			while(P3_1==0);//检测松手
			Delay(20);//消除抖动
			num++;
			P2=~num;
		}
	}	
}		

  num初始化为0,每当独立按键按一次,num依次增加1,由于LED灯是低电
  平才显示(与二进制中的数字反过来),所以要用按位取反;

Independent button control shift

code show as below:

#include <REGX52.H>
void Delay(unsigned int x)		//@11.0592MHz
{
    
    
	unsigned char i, j;

	while(x--)
	{
    
    
	
		i = 2;
		j = 199;
		do
		{
    
    
			while (--j);
		} while (--i);
	}
}
void main()
{
    
    
	unsigned char num=0;
	P2=~0x01;//默认在第一个LED灯上
	
	while(1)
	{
    
    
		//第一按键控制左移
		if(P3_1==0)
		{
    
    
			Delay(20);//抖动消除
			while(P3_1==0);//检测松手
			Delay(20);//抖动消除
			num++;
			num%=8;
			P2=~(0x01<<num);
		}
		//第二按键控制右移
		if(P3_0==0)
		{
    
    
			Delay(20);//抖动消除
			while(P3_0==0);//检测松手
			Delay(20);//抖动消除
			num--;
			num+=8;
			num%=8;
			P2=~(0x01<<num);
		}
			
	}	
}
 先看左移的,由于只有8个LED灯,所以当num++超过8之后,就要回到初始
 位置,那么可以利用对8取模限制只在0~7中走动;然后对0x01(0000 0001)
 进行左移,num是多少,二进制中的1就向左移动多少,最后再按位取反即可。
 接下里看右移的,以num的大小为基准,每向右移动一位,num就减少1,与
 左移正好相反。num+=8是为了当处于初始位置向右移动时,使它正好处于
 末尾位置;然后取模限制在0~7内,最后对0x01(0000 0001)左移,按位取
 反即可。
 这里不使用右移是因为为了以num为基准,且当LED灯处于初始位置亮时,
 向右移时将会把二进制中的1给移不见了,所以用左移。

Guess you like

Origin blog.csdn.net/m0_74068921/article/details/131537766