51 MCU-supports continuous pressing and global variables

Publisher:qinghongLatest update time:2021-08-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Support the idea of ​​continuous pressing

The code that supports continuous pressing is to change "if(KEY4==1)" to "if(KEY4==0)" based on the idea of ​​"not supporting continuous pressing" code. In this way, the program can enter the curly brackets of "if(KEY4==0)" without releasing the key. Then we define a variable times. If you keep pressing the key, times will keep accumulating. When it accumulates to 1000, it means that the low level has lasted for a certain period of time, and we can execute the function code, as shown in the figure


3.png


2. Global variables

Global variables are defined before all functions. Such variables can be used in all functions. For example, when the value of this variable changes to another value, assuming it is 12, the value of this variable used by other functions at this time is 12. For the advantages and disadvantages of global variables, please refer to Section 7.1.2 of the document "Teaching You to Learn 51 Microcontrollers Step by Step".


So this time we encapsulate the content code displayed by the digital tube into a function, define a global variable cnt, and change the value of cnt through key actions in the main function, and then the digital tube is responsible for displaying this number.


The experimental phenomenon is that if you keep pressing K4, the value of cnt displayed on the digital tube will continue to accumulate.

 

3. Code that supports double-clicking


#include  

sbit ADDR2 = P1^2;

sbit ADDR1 = P1^1;

sbit ADDR0 = P1^0;

sbit ENLED = P1^4;

sbit ADDR3 = P1^3;

  

sbit LED2 = P0^0;

sbit KEY4 = P2^7;

unsigned char code LedChar[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; // Initialize the status value of the digital tube

unsigned char LedBuff[6]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //digital tube display buffer

unsigned char cnt=0; //Global variables that can be used in SEG_task() and main()

  

void SEG_task()//digital tube display function

{

    static unsigned char i=0;

    LedBuff[0]=LedChar[cnt%10];   

    if(cnt>=10)LedBuff[1]= LedChar[(cnt/10)%10]; //Do not update the initial value of LedBuff[1] until cnt reaches 10

    if(cnt>=100)LedBuff[2]= LedChar[(cnt/100)%10]; //The initial value of LedBuff[2] will not be updated until cnt reaches 100

    if(cnt==0){ LedBuff[1]=0xFF; LedBuff[2]=0XFF; }//After cnt reaches 255, adding 1 will overflow to 0. At this time, the two digital tubes should be turned off again.

   

    P0=0xFF; // All port status are turned off, and the LED in the digital tube is refreshed

    switch(i)

    {

        case 0:

        ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;P0=LedBuff[0];i++;break;

        case 1:

        ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;P0=LedBuff[1];i++;break;     

        case 2:

        ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;P0=LedBuff[2];i=0;break;  

    }  

}

 

void main()

{  

    unsigned char key_up=1; //Define a variable to record the key status value, with an initial value of 1 to avoid the program entering "if (key_up==0)" at the beginning

    unsigned int times=0; //Used to record the number of times the key judgment statement has been entered

    ADDR3 = 1; // Enable 38 decoder

    ENLED = 0; // 

   

    ADDR2 = 1; //****************************

    ADDR1 = 1; //Let IO6 of the 38 decoder output low level

    ADDR0 = 0; //****************************

    P2 = 0xF7; //Let K4 have the conditions to be pulled low first

   

    while(1)

    { 

        SEG_task(); //Digital tube display task

    

        //Button function part

        if(key_up==0)

        {      

            if(KEY4==0) //Previously "KEY4==1" did not support continuous pressing, now it is changed to "KEY4==0" to support continuous pressing

            {

                times++;

                if(times>=1000) //The button IO port has been at a low level, and times has been accumulated. When it accumulates to 1000, it means that the low level has lasted for a period of time, and it is time to execute the function code. If you modify the number 1000, the speed of cnt self-increment will change

                {

                    times=0;

                    cnt++; //Execute function code

                }

            }

        }

        key_up=KEY4; //If you don't let go, key_up will be equal to 0  

    }

}


Reference address:51 MCU-supports continuous pressing and global variables

Previous article:51 MCU-static variables
Next article:51 MCU - Optimize the code that does not support continuous pressing again

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号