No matter what kind of work you design, buttons are always indispensable. What do you know about buttons?
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.
Figure 1
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.
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, you can use an oscilloscope to test it. Key jitter can cause a key to be misread multiple times. To ensure that the CPU only processes a key closure once, key jitter must be removed. Read the key status when the key is closed stably, and must determine that the key is released stably before processing.
method
To debounce a key, you can use either hardware or software.
Hardware debounce
When the number of keys is small, the key jitter can be eliminated by hardware methods. The RS trigger shown in the figure below is a commonly used hardware debounce.
Figure 2
The two "NAND" gates in the figure form an RS trigger. When the key is not pressed, the output is 1; when the key is pressed, the output is 0. 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.
Another method of hardware debouncing is to use the discharge delay of the capacitor and adopt the parallel capacitor method to achieve hardware debouncing, as shown in Figure 3:
Figure 3[page]
Software debounce
If there are many keys, software de-jittering is often used, that is, after detecting the key closure, 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 a 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. Timer interrupts can also be used to eliminate jitter.
Here is another method to debounce the buttons: using the switch() structure, the program design is as follows:
Software debounce without delay
/****************************************************
Name: Keyboard scan subfunction
Function: Determine the key value during the key stability period and return the key value
**********************************************/
uchar keyscan(void)
{
static char key_state = 0;
static char key_value = 0;
uchar key_press, key_return = 0;
key_press=turn_left&turn_right; //Read key I/O level
switch (key_state)
{
case 0 : //Key initial stateif
(key_press==0) key_state = 1; //Key is pressed, but need to confirm whether it is interferencebreak
;
case 1 : //Key confirmation stateif
(key_press==0)
//If a key is pressed, it is not interference, determine the key value
{ if(turn_left==0) //Determine which key is
pressedkey_value=1; //When there are many keys, the switch selection structure can be usedelse
if(turn_right==0)
key_value=2;
else key_value=0;
key_state = 2; // State transition to key release state
}
else key_state = 0; // The key has been lifted, which is interference, transition to the key initial
statebreak;
case 2 :
if (key_press==1)
{
key_return=key_value;//After the key is released, output the key value
key_value=0;
key_state = 0; //If the key is released, transition to the key initial state
} break;
}return key_return; //Return key value
}
/*************************************************
Name: Key processing subfunction
Function:
**********************************************/
void key_operation(void)
{
switch (keyscan()) //Execute different contents according to different key values
{ case 1:
hight_votage-=1;
if(hight_votage<5)
hight_votage=5; break;
case 2:
hight_votage+=1;
if(hight_votage>25)
hight_votage=25; break;
default :break;
}
}
As long as there is a button, you must think about debouncing. In short, no matter whether it is hardware debouncing or software debouncing, you must always think of the scene shown in Figure 1 when the button is pressed, and then make corresponding designs.
Summary of several good button designs
The common idea for designing multiple buttons is: Separate data from procedures in a process-oriented programming way. Put everything related to the button status, such as button functions, into the structure, and put the debounce code in a function. Here are several methods for button design:
1. Matrix keyboard, http://www.51hei.com/f/jzjpcx.rar
(This document contains several matrix keyboard programs. I think these programs are relatively concise. You can refer to them)
2. ADC button, for specific design, please click this website: http://www.ceet.hbnu.edu.cn/bbs/viewthread.php?tid=7641&extra=&highlight=%CA%FD%D7%D6%CA%BE%B2%A8%C6%F7&page=2
The advantage of the ADC button is that it saves IO ports, but the resistance value needs to be adjusted, which I personally find a bit troublesome.
3. Integrate the serial output button. For the specific design, please click this website: http://www.ceet.hbnu.edu.cn/bbs/redirect.php?tid=8059&goto=lastpost#lastpost
4. 4*3 keyboards and multiplexed ports are awesome http://www.51hei.com/mcu/1316.html
Previous article:Interrupt serial communication
Next article:Black Book on MCU Program Debugging
- 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
- Added performance test for STM32L432
- New predictions for cloud computing in 2019
- Typical applications of Zigbee technology in street light control systems
- Bluetooth BLE - BlueNRG2 VTimer
- Celebrate National Day and wish our motherland a happy 70th birthday!
- Why are pull-up resistors necessary in microcontroller systems?
- Update the user program of DSP28335 through the serial port SCI
- How to design a triode amplifier circuit
- C5517 FFT hardware accelerator performs calculations sometimes well and sometimes poorly
- Antenna Aperture Tuning eBook