A brief discussion on how to eliminate key jitter

Publisher:VelvetDreamerLatest update time:2012-08-06 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Click to browse the next page

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.

Click to browse the next page

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:

Click to browse the next page

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

Reference address:A brief discussion on how to eliminate key jitter

Previous article:Interrupt serial communication
Next article:Black Book on MCU Program Debugging

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号