51 microcontroller: independent button control of LED lights

1. Independent button to control LED light status 

        After the on-off experiment is completed, the LED state can be controlled. First of all, we know that due to the elasticity of the mechanical structure, the key switch will not turn on stably for an instant when it is closed, nor will it turn off all at once when it is turned off. Therefore, we need to program to eliminate jitter, that is, to delay appropriately. The program is as follows

#include"reg52.h"
//定义LED灯
sbit LED1 = P2^0;
sbit LED2 = P2^1;
sbit LED3 = P2^2;
sbit LED4 = P2^3;
sbit LED5 = P2^4;
sbit LED6 = P2^5;
sbit LED7 = P2^6;
sbit LED8 = P2^7;

//Define independent buttons
sbit KEY1 = P3^1; //It should be noted that button 1 is the P3^1 pin and button 2 is the P3^0 pin
sbit KEY2 = P3^0;
sbit KEY3 = P3^2;
sbit KEY4 = P3^3; 

void Delay(int time)
{     while(time--) //The while loop takes 10us     {     } }



 
 int main()
 {
    while(1)
    {
        if(KEY1==0)
        {
            Delay(500);
               while(KEY1==0);
            Delay(500);
            LED1 =~ LED1;
        }
            

    }
 }

The experimental phenomena are as follows

2. Independent button control LED light display binary

        The experiments of turning on and off the LED and controlling the LED status have been completed. To control the binary display of the LED light is to directly operate the LED light P2. In the initial state, all pins of P2 output high level, then we directly decrement P2 The experimental effect can be achieved by operation. The procedure is as follows:

#include"reg52.h"

//Define independent button
sbit KEY1 = P3^1; //It should be noted that button 1 is the P3^1 pin and button 2 is the P3^0 pin

void Delay(int time)
{
    while(time--) //while循环一次需要10us
    {
    }
}
 
 int main()
 {
    while(1)
    {
        if(KEY1==0)
        {
            Delay(500);
               while(KEY1==0);
            Delay(500);
            P2--;
        }
            

    }
 }

The experimental phenomena are as follows

3. Independent button to control LED light shift

        By controlling the independent keys KEY1 and KEY2 to achieve left and right shifting, the procedure is as follows

 #include"reg52.h"

//Define independent buttons
sbit KEY1 = P3^1; //It should be noted that button 1 is the P3^1 pin and button 2 is the P3^0 pin
sbit KEY2 = P3^0;

void Delay(int time)
{     while(time--) //The while loop takes 10us     {     } }  int main()  {      int num = -1;      while(1)     {         //LED light moves to the right         if(KEY1== 0)         {              Delay(500);                while(KEY1==0);             Delay(500);             num++;             if(num>=8)                 num=0;              P2 =~ (0x01<<num);         }




 








            







        

        //LED灯左移
        if(KEY2==0)
        {
            if(num<=0)
                num=8;
            else
            {
                  Delay(500);
                   while(KEY2==0);
                Delay(500);
                num--;
                P2 =~ (0x01<<num);
            }    
                
        }    

    }
 }

The experimental phenomena are as follows

Guess you like

Origin blog.csdn.net/weixin_52300845/article/details/124317717