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
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 } }
Previous article:51 MCU-static variables
Next article:51 MCU - Optimize the code that does not support continuous pressing again
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Help, has anyone encountered this problem using the Pickit 3 Programmer programming tool?
- Qorvo Simplifies Smart Home IoT Design with Matter Development Kit
- The time for UWB to explode has arrived!
- The distance between boards cannot be changed during AD puzzle
- The weekly review information is here~~
- Looking for a domestic driver with Hall BLDC
- STB75NF75LT4 5000 pieces in total
- D Question Development
- LabVIEW Programming and Applications (2nd Edition)
- CyCubeTouch