『7x24小时有问必答』
当按下和释放微动按键时,会由短时间的抖动现象才会到达想要的状态。如下图所示:
从上图可知。按键抖动时间大概为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();
   }
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

上一主题上一主题         下一主题下一主题
QQ手机版小黑屋粤ICP备17165530号

关于我们·投诉举报· 用户帮助· 联系我们 · 本站服务 · 版权声明· 隐私政策 · 投搞指南

法律保护:PLC技术网,plcjs.com,plcjs.net等字样
Copyright 2010-2030. All rights reserved. 


微信公众号二维码 抖音二维码 百家号二维码 今日头条二维码哔哩哔哩二维码