开关抖动及消除

当按下和释放微动按键时,会由短时间的抖动现象才会到达想要的状态。如下图所示:

在这里插入图片描述
从上图可知。按键抖动时间大概为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();
  }
}

参考

  1. Switch Bounce and How to Deal with It
  2. Switch Debouncing
  3. Debounce a Switch
  • 18
    点赞
  • 120
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
嵌入式实验是一种通过单片机来控制外部设备和收集数据的实践活动。在该实验中,我们需要掌握单片机io口操作的基本方法,并且了解按键扫描及软件延时消除抖动的技巧。 首先,掌握单片机io口操作的基本方法非常重要。单片机的io口可以用来控制外部设备的开关状态,我们需要了解如何配置io口的输入和输出方向。配置为输出时,可以向外部设备发送高电平或低电平信号,控制其开关状态。配置为输入时,可以读取外部设备发送过来的信号,以获取相关的数据。 其次,按键扫描及软件延时消除抖动也是嵌入式实验中需要掌握的重点。通常,按键的状态变化不会立即发生,而是会有一定的延迟和抖动。为了准确获取按键的状态,我们需要编写代码来实现按键的扫描和消除抖动。 按键扫描是指定时地检测按键的状态变化,通常使用循环扫描的方法,通过读取io口的状态来判断按键是否按下。当检测到按键按下时,我们可以执行相应的操作,比如控制外部设备的状态或者记录相关数据。 然而,由于按键的状态变化会产生抖动,需要通过软件延时的方法来消除抖动。软件延时是指在检测到按键状态变化后,让程序暂停一段时间,以确保按键状态已经稳定。一般情况下,延时几十毫秒即可有效消除抖动。 综上所述,嵌入式实验中,要想顺利地控制外部设备和获取数据,我们需要掌握单片机io口操作的基本方法,并且熟练掌握按键扫描及软件延时消除抖动的技巧。这些基本的知识和技能将为我们进行更复杂的嵌入式系统开发打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值