【问题标题】:How to properly implement button combinations with STM32?STM32如何正确实现按键组合?
【发布时间】:2019-09-05 14:30:58
【问题描述】:

我想将一些触觉按钮与 STM32 连接。然后基于特定时间段的按钮组合,我需要执行不同的功能。

我知道使用 HAL_Delays 会冻结程序,我不想这样做。我想计时器是要走的路。在那种情况下,我应该使用什么作为时间段。我应该轮询计时器吗?执行此操作的标准且无故障的方法是什么?

【问题讨论】:

  • 你不应该轮询任何东西
  • @P__J__ 是的,当然。

标签: c button embedded stm32


【解决方案1】:

有很多可能性,但一个简单的方法是捕获按钮按下事件的时间,并将其与当前时间进行比较。

硬件密集型方法是将每个按钮连接到输入捕获计时器。然后将捕获按钮按下时间,并且按住的时间是当前计时器值减去捕获时间。然后,您的应用程序可以确定每个按下的按钮,因此它已经持续了多长时间。

但是,该方法需要为每个按钮设置一个计时器捕获单元。一个较便宜的解决方案是将每个按钮连接到一个 GPIO EXTI 输入,并为每个按钮捕获按钮按下中断的 systick 时间。

在任何一种情况下,对捕获时间的处理都是相同的。

伪代码:

int downTime( int button_id ) 
{
    int down_time = 0 ;

    // If the button is down, report how long it has been down
    if( buttonDown( button_id ) )
    {
        down_time = buttonTimerNow( button_id ) - buttonTimerCapture( button_id ) ;
    }

    return down_time ;
}

bool pressed( int button_id )
{
    // The button is pressed, if it has been down for 
    // longer than the switch bounce time.
    return downTime( button_id ) > DEBOUNCE_TIME ;
}

bool combinationPressed()
{
    // Test for the required combination of currently 
    // simultaneously pressed buttons.
    return pressed( BUTTON_A ) && pressed( BUTTON_B ) ;
}

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-06-24
    • 2012-08-16
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多