本文介绍了如何通过微处理器/微控制器同时检测两个或更多按钮按下(GPIO)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如问题中所述,我想知道控制器是否有可能同时检测到两个按钮的按下.

As mentioned in the question , I was wondering whether it is possible for the controller to detect two button press simultaneously.

我是控制器编程的新手,从基础知识开始-LED闪烁,然后移至按钮,现在尝试按一下按钮.我想同时按下两个按钮时设置一些标志.

I am new to controller programming and started with the basics - blinking LED, then moved to buttons and now trying to play around button presses. I wanted to set some flag when both buttons are pressed together .

但是据我所知,在这种情况下将只调用一个ISR,从而检测到单次按下.我们如何实现这一目标...

But as of I know, Only one ISR will be called in this case, thus detecting a single press. How can we achieve this...

(在某些电子设备中,当我们同时按下某些按钮时,它具有特定的功能,例如,当人们同时按下3个适当的按钮时,重置电话)

(In some electronic devices, it has a specific functionality when we press certain buttons together e.g. resetting a phone when one presses 3 appropriate buttons simultaneously)

致谢,梅西

推荐答案

单个ISR触发不足以检测单个按钮按下.由于您可以通过所有按钮弹起机电信号,因此您需要某种去抖算法.

One single ISR triggered is not sufficient to detect a single button press. Because of the electro-mechanical signal bounce you get from all buttons, you need some kind of de-bouncing algorithm.

此外,您需要该程序具有抗EMI的能力,以便每当有许多脉冲从按钮发出时,多个中断都不会在堆栈上造成破坏.

Furthermore, you need the program to be immune to EMI so that multiple interrupts won't create havoc on the stack whenever there are lots of pulses coming from the button(s).

例如:

  1. 如果按钮连接到提供不同中断的不同端口,请为每个按钮创建一个中断.如果它们连接到相同的端口,它们通常可以触发相同的中断(取决于MCU).

  1. If the buttons are connected to different ports that give different interrupts, create one interrupt per button. If they are connected to the same port, they can usually trigger the same interrupt (depending on MCU).

每当您从按钮上收到任何由于信号沿(上升或下降)而引起的中断时,则在ISR中禁用该中断并启动一个通常为5-10ms的硬件计时器取决于按钮.计时器最好应触发计时器中断.

Whenever you get an interrupt as result of any signal edge (raising or falling) from the button, then in the ISR disable the interrupt and start a hardware timer of typically 5-10ms depending on the button. The timer should preferably trigger a timer interrupt.

必须禁用该中断,以滤除由反弹和潜在的EMI毛刺引起的虚假中断.

Disabling the interrupt is necessary to filter out spurious interrupts caused by the bouncing and by potential EMI glitches.

计时器对于反跳是必需的.如果您无法在按钮数据表中找到确切的信号反弹时间(通常是找不到),则只需使用示波器对其进行测量即可.

The timer is necessary for the de-bouncing. If you cannot find the exact signal bounce time in the button data sheet (you most often don't), then simply measure it using an oscilloscope.

当计时器过去时,读取端口并将结果存储在变量中.再次启用按钮中断.

When the timer elapses, read the port and store the result in a variable. Enable the button interrupt once again.

该变量需要在文件范围内声明为static volatile.专用封装的静态,这是良好程序设计所必需的.可以防止常见的编译器优化器错误,因为编译器未意识到ISR修改了变量.

The variable needs to be declared at file scope as static volatile. Static for private encapsulation, which is needed for good program design. Volatile to prevent against common compiler optimizer bugs, where the compiler doesn't realize that a variable has been modified by an ISR.

对第一个按钮执行相同的操作.您将有两个不同的变量来告诉按钮的当前状态.只需将这两个变量相互比较即可判断是否同时按下了两个按钮.

Implement the same for the first button. You'll have two different variables telling the current state of the buttons. Simply compare these two variables with each other to tell whether or not two buttons are pressed at the same time.

这篇关于如何通过微处理器/微控制器同时检测两个或更多按钮按下(GPIO)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:19