當(dāng)按下和釋放微動(dòng)按鍵時(shí),會(huì)由短時(shí)間的抖動(dòng)現(xiàn)象才會(huì)到達(dá)想要的狀態(tài)。如下圖所示:
從上圖可知。按鍵抖動(dòng)時(shí)間大概為150us。
在一些對(duì)按鍵抖動(dòng)敏感的情況下需要進(jìn)行消抖設(shè)計(jì),目前常見的消抖設(shè)計(jì)如下:
濾波電容
關(guān)于去抖硬件最簡單的方式并聯(lián)一顆100nF陶瓷電容,進(jìn)行濾波處理。
RC濾波+施密特觸發(fā)器
要想更嚴(yán)謹(jǐn)設(shè)計(jì)消抖電路,會(huì)增加施密特觸發(fā)器,更大程度的保證后端不受按鍵抖動(dòng)影響,電路如下:
分別來看按鍵閉合斷開時(shí)電路狀態(tài):
開關(guān)打開時(shí):
電容C1通過R1 D1回路充電,Vb電壓=Vcc-0.7為高電平,后通過反向施密特觸發(fā)器使Vout輸出為低。
開關(guān)閉合時(shí):
電容C1通過R2進(jìn)行放電,最后Vb電壓變?yōu)?,通過反向施密特觸發(fā)器使Vout輸出為高。
當(dāng)按下按鍵出現(xiàn)快速抖動(dòng)現(xiàn)象時(shí),通過電容會(huì)使Vb點(diǎn)電壓快速變成Vcc或GND。在抖動(dòng)過程時(shí)對(duì)電容會(huì)有輕微的充電或放電,但后端的施密特觸發(fā)器有遲滯效果不會(huì)導(dǎo)致Vout發(fā)現(xiàn)抖動(dòng)現(xiàn)象。
此電路中D1的使用使為了限制R1 R2一起給C1供電,增加充電時(shí)間影響效果。如果減小R1的值會(huì)使電流增加,功耗較高。
專用消抖芯片
一些廠家會(huì)提供專用芯片,避免自搭電路的不穩(wěn)定性, 如美信-Max6816:
軟件濾波
軟件消除抖動(dòng)也是很常見的方式,一般形式是延時(shí)查詢按鍵狀態(tài)或者中斷形式來消除抖動(dòng)。
下面是Arduino的軟件消抖代碼:
/* SoftwareDebounce
*
* At each transition from LOW to HIGH or from HIGH to LOW
* the input signal is debounced by sampling across
* multiple reads over several milli seconds. The input
* is not considered HIGH or LOW until the input signal
* has been sampled for at least "debounce_count" (10)
* milliseconds in the new state.
*
* Notes:
* Adjust debounce_count to reflect the timescale
* over which the input signal may bounce before
* becoming steady state
*
* Based on:
* http://www.arduino.cc/en/Tutorial/Debounce
*
* Jon Schlueter
* 30 December 2008
*
* http://playground.arduino.cc/Learning/SoftwareDebounce
*/
int inPin = 7; // the number of the input pin
int outPin = 13; // the number of the output pin
int counter = 0; // how many times we have seen new value
int reading; // the current value read from the input pin
int current_state = LOW; // the debounced input value
// the following variable is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was sampled
int debounce_count = 10; // number of millis/samples to consider before declaring a debounced input
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
digitalWrite(outPin, current_state); // setup the Output LED for initial state
}
void loop()
{
// If we have gone on to the next millisecond
if(millis() != time)
{
reading = digitalRead(inPin);
if(reading == current_state && counter > 0)
{
counter--;
}
if(reading != current_state)
{
counter++;
}
// If the Input has shown the same value for long enough let's switch it
if(counter >= debounce_count)
{
counter = 0;
current_state = reading;
digitalWrite(outPin, current_state);
}
time = millis();
}
}
審核編輯:湯梓紅
-
開關(guān)
+關(guān)注
關(guān)注
19文章
3138瀏覽量
93727 -
濾波電容
+關(guān)注
關(guān)注
8文章
458瀏覽量
40079 -
抖動(dòng)
+關(guān)注
關(guān)注
1文章
69瀏覽量
18880 -
按鍵
+關(guān)注
關(guān)注
4文章
223瀏覽量
57616
原文標(biāo)題:開關(guān)抖動(dòng)及消除
文章出處:【微信號(hào):mcu168,微信公眾號(hào):硬件攻城獅】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論