1 //write by:cyt
2 //Time:2017-2-10
3 //Porject Name:key shake_destory
4 #include
5 #define GPIO_KEY P0 ///
6 void Delay1ms(unsigned char n);
7 unsigned char Key_Scan() // GPIO_KEY is a global variable, input here
8 {
9 unsigned char i=0,Key_Value=0;
10 if(GPIO_KEY!=0xff)
11 {
12 Delay1ms(10);
13 if(GPIO_KEY!=0xff)
14 Key_Value = GPIO_KEY;
15 while(GPIO_KEY!=0xff&&i<50)
16 {
17 Delay1ms(10);
18 i++;
19 }
20 }
21 return Key_Value;
twenty two }
Why does the key jitter?
The switch used for common buttons is a mechanical elastic switch. When the mechanical contacts are opened and closed, due to the elastic effect of the mechanical contacts, a button switch will not be immediately and stably connected when closed, nor will it be disconnected all at once when disconnected. Therefore, there will be a series of jitters at the moment of closing and disconnecting. The measure to avoid this phenomenon is to eliminate the jitter of the button.
Jitter Time
The length of the jitter time is determined by the mechanical characteristics of the key, generally 5ms to 10ms. This is a very important time parameter and is used in many occasions. Click to view the picture The length of the key stable closing time is determined by the operator's key action, generally a few tenths of a second to several seconds. Key jitter can cause a key to be misread multiple times. To ensure that the CPU processes a key closure only once, key jitter must be removed. Read the key status when the key is closed stably, and it must be determined that the key is released stably before processing.
Key debounce
To debounce a key, you can use either hardware or software.
Hardware debounce
When the number of keys is small, key jitter can be eliminated using hardware methods.
Two NAND gates form an RS flip-flop.
When the key is not pressed, the output is 0; when the key is pressed, the output is 1. At this time, the mechanical properties of the key are used to make the key disconnected instantly due to elastic jitter (jitter jump B). The key does not return to the original state A, the state of the bistable circuit does not change, the output remains 0, and no jittering waveform is generated. In other words, even if the voltage waveform at point B is jittering, after passing through the bistable circuit, its output is a regular rectangular wave. This can be easily verified by analyzing the working process of the RS trigger.
Software debounce
There are many buttons, and software methods are commonly used to debounce
That is, after detecting that the key is closed, a delay program is executed, with a delay of 5ms to 10ms, so that the leading edge jitter disappears and the key state is detected again. If the closed state level is still maintained, it is confirmed that the key is really pressed. When the key is released, a delay of 5ms to 10ms is also given, and the key processing program can only be transferred after the trailing edge jitter disappears.
Previous article:51 single chip microcomputer dynamic digital tube
Next article:51 MCU delay calculation method
Recommended ReadingLatest update time:2024-11-15 02:31
- Popular Resources
- Popular amplifiers
- 2024 DigiKey Ideas Contest Code
- A review of learning-based camera and lidar simulation methods for autonomous driving systems
- Mission-oriented wireless communications for cooperative sensing in intelligent unmanned systems
- Evaluating Roadside Perception for Autonomous Vehicles: Insights from Field Testing
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- [ESK32-360 Review] + Sensor Measurement and Output Display
- ARM editing and compilation tools
- EEWORLD University - What is an antenna vibrator?
- [RISC-V MCU CH32V103 Review] First Look at CH32V103
- May was quite busy, and my hand broke.
- Pre-registration for the prize-winning live broadcast | Infineon system solutions make electric motorcycle design more reliable and efficient!
- What methods do you generally use to recruit or find jobs? Which platform is used more often?
- Summary of the points to note when using CCS8.0 to program MSP430G2553
- A fire broke out at Foxconn in Shenzhen, with thick smoke billowing from the scene
- PCB copper plating