當按下和釋放微動按鍵時,會由短時間的抖動現象才會到達想要的狀態。如下圖所示:
從上圖可知。按鍵抖動時間大概為150us。
在一些對按鍵抖動敏感的情況下需要進行消抖設計,目前常見的消抖設計如下:
濾波電容
關于去抖硬件最簡單的方式并聯一顆100nF陶瓷電容,進行濾波處理。
RC濾波+施密特觸發器
要想更嚴謹設計消抖電路,會增加施密特觸發器,更大程度的保證后端不受按鍵抖動影響,電路如下:
分別來看按鍵閉合斷開時電路狀態:
開關打開時:
電容C1通過R1 D1回路充電,Vb電壓=Vcc-0.7為高電平,后通過反向施密特觸發器使Vout輸出為低。
開關閉合時:
電容C1通過R2進行放電,最后Vb電壓變為0,通過反向施密特觸發器使Vout輸出為高。
當按下按鍵出現快速抖動現象時,通過電容會使Vb點電壓快速變成Vcc或GND。在抖動過程時對電容會有輕微的充電或放電,但后端的施密特觸發器有遲滯效果不會導致Vout發現抖動現象。
此電路中D1的使用使為了限制R1 R2一起給C1供電,增加充電時間影響效果。如果減小R1的值會使電流增加,功耗較高。
專用消抖芯片
一些廠家會提供專用芯片,避免自搭電路的不穩定性, 如美信-Max6816:
軟件濾波
軟件消除抖動也是很常見的方式,一般形式是延時查詢按鍵狀態或者中斷形式來消除抖動。
下面是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();
}
}
審核編輯:湯梓紅
-
開關
+關注
關注
19文章
3127瀏覽量
93497 -
濾波電容
+關注
關注
8文章
457瀏覽量
39988 -
抖動
+關注
關注
1文章
69瀏覽量
18847 -
按鍵
+關注
關注
4文章
223瀏覽量
57572
原文標題:開關抖動及消除
文章出處:【微信號:mcu168,微信公眾號:硬件攻城獅】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論