Key debounce principle
1. First, let’s review the key delay debounce
Since the button is a mechanical structure, it is inevitable to have jitter when pressing it. Generally, the jitter occurs when pressing and releasing it. The jitter time is about 10ms.
Therefore, there is a simple solution to delay the debounce of key jitter:
2. Key debounce code
Method 1: The time of key press loss depends on the time from pressing the key to releasing it, which is at least 10ms. After pressing the key, the CPU will no longer execute other instructions until the key is released, and the application capability is weak.
Code function: Press the button to invert the LED state, press the button and wait until it is released
#include sbit key=P1^0; //define key as P1.0 sbit led=P2^0; //define LED as P2.0 void delay_ms(unsigned int t) //ms delay { unsigned int i,j; for(i=0; i } void main(void) { while(1) { if(key==0) //Read P1.0 pin, if the pin is low, enter if { delay_ms(10); //10ms delay to eliminate jitter if(key==0) //Judge again whether the key is pressed to prevent interference and enhance stability { led = !led; //led state changes while(key==0); //Wait for the key to be released to prevent further execution } } } } Method 2: (No while) The time it takes to press a key is about 10ms, no waiting is required, other operations can be performed while the key is pressed, and the application capability is strong Code function: Press the button to invert the LED state, press the button without waiting for release #include sbit key=P1^0; //define key as P1.0 sbit led=P2^0; //define LED as P2.0 void delay_ms(unsigned int t) //ms delay { unsigned int i,j; for(i=0; i } void main(void) { int key_up=1; //Key release flag while(1) { if(key==0 && key_up==1) // Check if the key is pressed { delay_ms(10); //delay to eliminate jitter key_up=0; //Prevent loop execution of key control program if(key==0) //Judge again to exclude the possibility of release or external interference { led1=!led1; } } else if(key==1) key_up=1;//No key is pressed and the state is released } } As you can see, the biggest disadvantage of delay debouncing is the delay, which takes at least 10ms to 20ms. For us, 10ms may be very short, but for some high-performance MCUs, it is the time to execute tens of thousands of instructions. Key timer debounce 1. Key debounce Key debounce is generally divided into 4 steps: 1. Determine whether the button is pressed 2. Eliminate shaking 3. Determine again whether the button is pressed 4. Wait for the button to be released 2. Timer debounce principle 1. Determine whether the button is pressed. 2. If a key is detected to be pressed, the timer is turned on and the timer interrupt is turned on. The timing time is about 10ms, so that the timer interrupt is entered 10ms after the key is pressed. When the interrupt is entered, the key jitter time has passed. 3. Determine again whether the button is pressed in the timer interrupt 4. Turn off the timer and wait for the button to be released Code: #include sbit key=P1^0; //define key as P1.0 sbit led=P2^0; //define LED as P2.0 void main(void) { TMOD|=0X01; //Select timer 0 mode, working mode 1, and only use TR0 to start. TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms TL0=(65536-10000)%256; ET0=1; //Enable timer 0 interrupt EA=1; //Open the general interrupt TR0=0; //turn off the timer while(1) { if(key==0) //Read P1.0 pin, if the pin is low, enter if { TR0=1; //Turn on the timer } } } /*Timer interrupt*/ void Timer0() interrupt 1 { TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms TL0=(65536-10000)%256; TR0=0; //turn off the timer if(key==0) //Check if the key is pressed again { led=!led; while(key==0); //Wait for the key to be released } } You can see that the code above still has "while(key==0); //Wait for the key to be released" which will make the program wait Program upgrade! Final code Modify the circuit and add external interrupt: Code: #include sbit key=P3^2; //define key to P1.0 sbit led=P2^0; //define LED as P2.0 void main(void) { TMOD|=0X01; //Select timer 0 mode, working mode 1, and only use TR0 to start. TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms TL0=(65536-10000)%256; ET0=1; //Enable timer 0 interrupt TR0=0; //turn off the timer IT0=1; // Jump edge start mode (falling edge) EX0=1; //Turn on the interrupt enable of INT0. EA=1; //Open the general interrupt while(1) { } } /*Timer interrupt*/ void Timer0() interrupt 1 { TH0=(65536-10000)/256; //Assign an initial value to the timer, set the timer to 10ms TL0=(65536-10000)%256; TR0=0; //turn off the timer if(key==0) //Check if the key is pressed again { led=!led; //Control the LED status to reverse } } /*External interrupt 0*/ void Int0() interrupt 0 //interrupt function of external interrupt 0 { TR0=1; //Turn on the timer } It can be seen that the entire program has no delay and no waiting, and the key function is realized. END
Previous article:51 MCU tutorial: Digital tube dynamic display (0~99999999) 74hc138 driver
Next article:51 MCU tutorial: key input, matrix key (key usage) proteus simulation + keil
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Help
- ODX-based diagnostic application software INTEWORK-OBT
- Share the MSP430F5529 clock UCS programming considerations
- RK3288 Information
- FFT based on C2000 series DSP
- Occupy the posting position, ESP32---WS2812 16*16 dot matrix drawing points, lines and surfaces
- ASM1117 SOT-323 heat sink problem?
- About the pitfalls of ADXL362 accelerometer
- SimpleLink Wi-Fi Devices
- Efficiency Programming of Single Chip Microcomputer Active Buzzer Driver