# 51 MCU-Independent Button Control LED

1. The position of the independent button of the 51 single-chip microcomputer:

Definition of independent button: The independent button is equivalent to an electronic switch. When pressed, the switch is turned on, and when released, the switch is turned off. The realization principle is to realize the connection and disconnection by lightly touching the metal shrapnel inside the button. ;

In the lower left corner of the entire 51 development board, there are 4 independent buttons. This article mainly understands these 4 buttons;

2. Use the bit register to control the LED switch

There are bit registers inside the register Bit Registers can define each bit individually-----P2_0 P2_1 and so on;

```#include <REGX52.H>
#include <INTRINS.H>

void main()
{
P2_0=0;//Indicates the first light
P2_1=1;//Indicates the second light
P2_2=0;//Indicates the third light
P2_3=1;//Indicates the fourth light
P2_4=0;//Indicates the fifth light
P2_5=1;//Indicates the sixth light
P2_6=0;//Indicates the seventh light
P2_7=0;//Indicates the eighth light
while(1)
{

}
}```

in:

P2_0 = 0xA0;     P2_1 = 0xA1;     P2_2 = 0xA2;    P2_3 = 0xA3;    P2_4 = 0xA4;
P2_5 = 0xA5; P2_6 = 0xA6; P2_7 = 0xA7; Once again verified that the 51 single-chip microcomputer receives hexadecimal digits;

2. Use the first independent button to control the LED light on and off:

Principle: First of all, we need to understand that the representation of any code is based on our schematic diagram; that is to say, the pin that shows the LED light on the schematic diagram is connected to the chip P3_1, so our loop statement uses P3_1 , p3_1==0 It means that in the working state of the pin, that is, when we press the first independent button, when we do not press the first independent button by default, it is the otherwise state of the if statement, that is, else, all lights are off. ;

```#include <REGX52.H>
#include <INTRINS.H>

void main()
{

while(1)
{
if(P3_1==0)
{
P2_0=0;//Indicates the first light
P2_1=0;//Indicates the second light
P2_2=0;//Indicates the third light
P2_3=0;//Indicates the fourth light
P2_4=0;//Indicates the fifth light
P2_5=0;//Indicates the sixth light
P2_6=0;//Indicates the seventh light
P2_7=0;//Indicates the eighth light
}
else
{
P2_0=1;//Indicates the first light
P2_1=1;//Indicates the second light
P2_2=1;//Indicates the third light
P2_3=1;//Indicates the fourth light
P2_4=1;//Indicates the fifth light
P2_5=1;//Indicates the sixth light
P2_6=1;//Indicates the seventh light
P2_7=1;//Indicates the eighth light
}
}
}```

The following represents a simple introduction of a second independent button:

P3_1 represents the first independent button; P3_0 represents the second independent button; the function of bitwise and && is that only when the independent button 1 and the independent button 2 are pressed at the same time, the 8 LED lights will be on;

```#include <REGX52.H>
#include <INTRINS.H>

void main()
{

while(1)
{
if(P3_1==0&&P3_0==0)
{
P2_0=0;//Indicates the first light
P2_1=0;//Indicates the second light
P2_2=0;//Indicates the third light
P2_3=0;//Indicates the fourth light
P2_4=0;//Indicates the fifth light
P2_5=0;//Indicates the sixth light
P2_6=0;//Indicates the seventh light
P2_7=0;//Indicates the eighth light
}
else
{
P2_0=1;//Indicates the first light
P2_1=1;//Indicates the second light
P2_2=1;//Indicates the third light
P2_3=1;//Indicates the fourth light
P2_4=1;//Indicates the fifth light
P2_5=1;//Indicates the sixth light
P2_6=1;//Indicates the seventh light
P2_7=1;//Indicates the eighth light
}
}
}```

3. The meaning of bitwise left shift << and bitwise right shift>>:

0011 1100<<1 -> 0111 1000 Bitwise shift left by 1 character means that the high bit is shifted out by one character, and the low bit is filled with 0;

0011 1100>>1 -> 0001 1110 Bitwise shift right by 1 character means that the low bit is shifted out by one character, and the high bit is filled with 0;

4. Button shaking:

For mechanical switches, when the mechanical contacts are opened and closed, due to the elastic effect of the mechanical contacts, a switch will not be turned on immediately when it is closed, and it will not be disconnected at once when it is disconnected. The moment the switch is closed and opened is accompanied by a series of jitters.

5. The independent button realizes press to light, press to turn off:

P3_1==0 means that the button is pressed to perform the light-on operation; the two Delay Delays represent the jitter when pressing and releasing;

Special attention: Negative ~ ------- Indicates that the two states of 1 and 0 alternate with each other, that is, press to light, then press to turn off;

```#include <REGX52.H>
#include <INTRINS.H>
void Delay(unsigned int xms)
{
unsigned char i,j;
while(xms)
{
i=2;
j=239;
do
{
while(--j);
}
while(--i);
xms--;
}
}
void main()
{
while(1)
{
if(P3_1==0)
{
Delay(20);//Jitter delay after key press
while(P3_1==0);
Delay(20);//Jitter delay after button release
P2_0=~P2_0;//Negative means that the No. 1 LED is on and off;
}//1 0 to loop;
}
}```

while (P3_1==0) means judging the state of the independent button, as long as the button is pressed, my while statement is empty, so as long as the button is judged on the status of the button-----my light does not reflect (the light is always on, until the loose Turn on the button; the light will always be off until the button is released), as long as the state is judged to be releasing the button, I will perform the inversion operation, and the light will turn on and off;

while (P3_1==0) If there is no operation after I press the button, it is judged that the button has been pressed, and I let him perform an empty loop;

The delay time of the delay function Delay is the response time of the light after I press the button, that is, I press the button. Assuming Delay=1000, I need to wait for 1 second before the light will respond;

5. Independent key control LED display binary:

P2_ means 8 bits; P3_ means 1 bit;

Ideas: The above is the delay function; the following defines a new function unsigned char LED_L=0;

8 bits is: 0000 0000 each time LED_L++; binary addition; every two into one; operation;

0000 0001；0000 0010；0000 0011；0000  0100；

Reverse: 1111 1110; 1111 1101; 1111 1100; 1111 1011; low-level light; pay attention to the idea of ​​defining a new function to initialize 0;

```#include <REGX52.H>
#include <INTRINS.H>
void Delay(unsigned int xms)
{
unsigned char i,j;
while(xms)
{
i=2;
j=239;
do
{
while(--j);
}
while(--i);
xms--;
}
}
void main()
{
unsigned char LED_L=0;
while(1)
{
if(P3_1==0)
{
Delay(20);//Jitter delay after key press
while(P3_1==0);
Delay(20);//Jitter delay after button release
LED_L++;
P2=~LED_L;
}
}
}```

6. Independent button to control LED shift: (simply speaking, press the button once to make the water flow, that is, the light will turn on once)

Idea: The above is the delay function; the same below, as long as I press the button, I will execute the statement and define the initial value of the function to 0; + + 1 each time, as long as it exceeds 8, I will initialize, 8 lights will cycle, in the hexadecimal The shift operation is performed on the basis of 0X01, which is 1, and the LED_L bits are shifted to the left each time, and inverted to ensure low-level output;

```#include <REGX52.H>
#include <INTRINS.H>
void Delay(unsigned int xms)
{
unsigned char i,j;
while(xms)
{
i=2;
j=239;
do
{
while(--j);
}
while(--i);
xms--;
}
}
void main()
{
unsigned char LED_L=0;
while(1)
{
if(P3_1==0)
{
Delay(20);//Jitter delay after key press
while(P3_1==0);
Delay(20);//Jitter delay after button release
LED_L++;
if(LED_L>=8)
LED_L=0;//Guaranteed to start the cycle with 8 lights
P2=~(0X01<<LED_L);
}
}
}```

Press the independent button 1, and the light will be turned on from left to right; press the independent button 2, and the light will be turned on from right to left;

```#include <REGX52.H>
#include <INTRINS.H>

void Delay(unsigned int xms)
{
unsigned char i,j;
while(xms)
{
i=2;
j=239;
do
{
while(--j);
}
while(--i);
}

}
void main()
{
while(1)
{//Realize bit-wise lighting from left to right
if(P3_1==0)
{
unsigned char LED=0;
Delay(20);
while(P3_1==0);
Delay(20);
LED++;
if(LED>=8)
LED=0;
P2=~(0X01<<LED);
}
if(P3_0==0)
{//Realize bit-wise lighting from right to left
unsigned char LED=0;
Delay(20);
while(P3_0==0);
Delay(20);
LED--;
if(LED==0)
LED=7;
else
LED--;
P2=~(0X01<<LED);
}
}
}```

Tags: 8051 microcontroller

Posted by Gast on Sun, 23 Oct 2022 10:02:52 +1030