Shenzhen ZTRON Microelectronics Co., Ltd
Telephone
0755-8299 4126

MCU

MCU button debounce technology


Single-chip microcomputer button debounce technology and its extended application


When using a single-chip microcomputer to build a human-computer interaction system, it is necessary to use a keyboard. The switch used for the usual buttons is a mechanical elastic switch. When the mechanical contacts are opened and closed, the voltage signal is switched. It is very necessary for the system to accurately sample the input logic 0 or l of the keyboard to avoid wrong input. The ideal keyboard input characteristics are: when the key is not pressed, the input is a logic l, and when it is pressed, the input immediately becomes a logic O, and when it is released, the input immediately becomes a logic 1.


However, the input characteristics of the actual keyboard may not be perfect due to the influence of the manufacturing process and the like. Due to the elastic effect of the mechanical contact, a key switch will not be turned on stably immediately when it is closed, nor will it be disconnected suddenly when it is turned off. Therefore, there is a series of jittering at the moment of closing and disconnecting. The length of the jittering time is determined by the mechanical characteristics of the key, generally 5ms-10ms. The working time of the single-chip microcomputer is at the level of nanoseconds and milliseconds. When the button is pressed and the contact is about to be in full contact, the on-off state of the keyboard may have changed many times. That is, during this period of time, the keyboard has entered logic 0 and 1 multiple times. That is, the input is out of control. If these inputs are responded to by the system, the system will temporarily be out of control, which we want to avoid as much as possible. The same is true for the period from when the contacts are about to separate to when they are completely separated.


The keyboard actually produces instantaneous high-frequency interference pulses when inputting logic transitions. In order to ensure that the CPU processes only one key closure once, key jitter must be removed. That is, the state of the key is read when the key is closed and stable, and it must be judged that the key is released and stable before processing. The purpose is to eliminate interference to achieve ideal input characteristics.


There are two stages to try to eliminate this interference: 1. Before the keyboard signal is input into the system (outside the system): 2. After the keyboard signal is input into the system (inside the system).


1. Hardware debounce


Eliminating the jitter interference before the signal is input into the system can save system resources and improve the system's responsiveness to other signals. This is hardware debounce.


(1) Basic RS hardware debounce: Use two "NAND gates" to form an RS flip-flop and use the memory function of the basic RS latch to eliminate the impact of switch contact vibration. There is only one flip of the output end every time the switch S switches, and there is no jitter waveform.


(2) Capacitive filter debounce: The use of RS latch debounce is only suitable for single-pole double-throw switches. The keyboards commonly used in practical applications are mostly keys with two terminals. The common hardware debounce method for this type of key is to connect a capacitor in parallel on the key, use the capacitor discharge to delay the smoothing wave, and then adjust the Schmidt inverter to obtain a glitch-free pulse wave.


(3) Interrupt method debounce: each button is externally connected to the external interrupt port. When a button is pressed, it will cause the interrupt of the single chip microcomputer. The advantage of this method is that there is no need to continuously loop the query in the main program. As long as there is an interrupt, then do the corresponding processing. The disadvantage is that the interrupt source of a single-chip microcomputer is very precious, so few people use this method.


2. Software delay debounce


As mentioned earlier, if the hardware anti-shake circuit is used, then N keys must be equipped with N anti-shake circuits. Therefore, when the number of keys is relatively large, the hardware anti-shake will not be able to do the job. In this case, I can use software for anti-shake. The essence of software debounce is to reduce the sampling frequency of the keyboard input port to omit the high-frequency jitter, that is, to detect the key closure and execute a delay program to generate a delay of 5ms to 10ms, so that the state of the key is detected again after the front jitter disappears. , if the closed state level is still maintained, it is confirmed that there is a real key pressed. When it is detected that the button is released, a delay of 5ms to 10ms is also given, and the processing program of the button can only be transferred after the jitter of the trailing edge disappears.


3. Extended application of software debounce


In practical applications, the software anti-shake technology is not only applied to the buttons, but also can be applied to other places to make the program run more in line with our requirements, as in the following two situations:


1) In some cases, for example, the system is subject to external vibration, and there will be level jitter in the button circuit, but this jitter is not the operation the controller wants. If there is no anti-shake program, the system will be affected by this unwanted interference. And wrong action.


2) In order to avoid system misjudgment, an anti-shake program can be written. The following is the first case (in the stroke control system) using this technology shield to see if it can improve the system performance and better realize the function I make up for the lack of common methods! Take a simple industrial control project as an example to illustrate Gu Mu’s request to power on Finally, a common motor controls a slider that is pushed from the left to the right and there is a limit switch on the far right. After the slider touches the limit switch, the machine stops and the movement ends.


Convert it into a single-chip programming idea and use one IO port to output the motor to move when the level is low, and the motor to stop when the level is low. The other 1 IO port is used as an input method to fully measure the level status of the travel switch. If it is found to be high, it means that it has not touched the travel switch sensor. If it is found to be low, it means that it has touched the sensor. At this time, it can send out Command to stop the motor.


Idea 1: Directly judge that the level state of the travel sensor is wide and low level is found, it is considered that the motor has touched the travel switch and immediately stops the motor.


The advantage of this idea is that the response is timely, but the disadvantage is that it is too sensitive and the anti-interference ability is very poor. In the industrial control environment, when the motor is running, if the input signal of the travel switch is disturbed by power fluctuations or external glitch signals, it may be read. The momentary low-level situation caused the single-chip microcomputer to misjudge and stop the motor in advance, and the machine stopped hastily before touching the travel switch.


Idea 2: Add software anti-interference processing (software debounce technology) when judging the level status of the travel sensor. Once a low level is found, a timer starts counting. Immediately clear the timer. If it is always low and there is no high level during the period, it is considered to be a stable low level. At this time, it is determined that the travel switch has been touched.


The advantage of this idea is that the addition of anti-interference processing can almost 100% ensure that the motor stops when it touches the limit switch, and there will be no misjudgment by the first idea. The disadvantage is that a small delay is added in the anti-interference link of the software, and this small delay will cause the motor to not stop immediately after touching the limit switch. After a long time, it is easy to crush and squeeze the limiting mechanical structure on the right.


Idea 3: This idea is to combine the advantages of the previous two. When judging the level state of the travel sensor, when it is found to be a low level (even if it is a momentary low level when there is interference), the motor will immediately pause and stop. The concept is different (although the motor is not rotating), when it is found that the machine continues to run when it is high Only when it is considered that the travel switch has been touched. During the small delay period for judging the low level, the motor is in a paused state and does not rotate), so it will not overshoot and squeeze the travel limit mechanism on the right.


The advantage of this idea is that it can respond in time and increase the anti-interference processing of the limit switch detection, and will not let the motor overshoot and squeeze the limit switch on the right. The third idea can better realize the function and effectively prevent interference!


Summarize


Traditional single-chip microcomputer systems are mostly serial processing and can only process some interrupt programs in parallel. For such a system, only pure software or hardware debounce can be used, but neither is perfect. In actual application, in order to improve the stability and reliability of the system, the software debounce technology is often applied on the basis of the hardware debounce technology, which can better meet the needs of the system.


The above is the MCU button debounce technology and its extended application introduced by Shenzhen Zuchuang Microelectronics Co., Ltd. We have rich experience in customized development of smart electronic products, can evaluate the development cycle and IC price as soon as possible, and can also calculate the PCBA quotation. We are the agent of Sonix MCU and Yingguang MCU agent, selling and developing MCU and voice IC solutions of Sonix and Yingguang. We act as an agent and develop ICs and solutions of Jieli, Ankai, Quanzhi, realtek and other series, and also develop BLE Bluetooth IC, dual-mode Bluetooth module, wifi module, and Internet of Things module. We have hardware design and software development capabilities. Covering circuit design, PCB design, single-chip microcomputer development, software custom development, APP custom development, WeChat official account development, voice recognition technology, Bluetooth development, wifi technology, etc. It can also undertake the research and development of smart electronic products, the design of household appliances, the development of beauty equipment, the development of Internet of Things applications, the design of smart home solutions, the development of TWS earphones, the development of Bluetooth earphone speakers, the development of children's toys, and the development of electronic education products.

  • TOP