【问题标题】:MSP430 LaunchPad | Assign double click on buttonMSP430 启动板 |分配双击按钮
【发布时间】:2021-01-08 20:32:04
【问题描述】:

我正在使用 MSP-EXP430F5529LP。我已经可以使用

闪烁红色 LED
#pragma vector=TIMER2_A0_VECTOR
__interrupt void Timer_A2(void)
{
    P1OUT ^= 0x01;
}

    TA0CCR0 = 25000;
    TA0CCTL0 |= CCIE;
    TA0CTL |= TASSEL_2 | ID_3 | MC_1 | TACLR;

我真的很想在双击检查时得到一些帮助

#pragma vector=PORT1_VECTOR

所以我可以将它与单击区分开来。我只想在里面使用简单的 if's 并根据单击或双击来做一些工作。

【问题讨论】:

  • 你知道如何处理单击吗?以及如何测量时间?

标签: c msp430


【解决方案1】:

通过使用计时器硬件使 LED 闪烁的代码已经是检测双击按钮的良好起点。我建议使用计时器来实现去抖动功能的基本功能,它只是处理某些事件(例如按钮输入)的递增/递减计数器。

按钮的问题是它们很吵。如果您足够快地从按钮中采样 MSP430 的输入引脚,您会发现它会在一个简单的按住事件中多次切换。松开按钮也会产生噪音。因此,您可以预期双击事件会产生很多噪音。可以创建外部硬件电路来滤除印刷机的噪音部分,但软件成本较低,因此大多数实现都采用这种方式。

去抖动功能背后的想法是计时器用于测量来自按钮输入的事件以处理噪声。这是检测单个按钮按下的起点。这可以很容易地修改为双(或三)按!

// Define button status and the button variable
typedef enum {BUTTON_STATUS_NOTPRESSED, BUTTON_STATUS_HOLDING, BUTTON_STATUS_RELEASED} button_status_t;
button_status_t button = BUTTON_STATUS_NOTPRESSED;

// Define button state values (if inverted then swap the values, assume input at bit 1)
typedef enum {BUTTON_STATE_NOTPRESSED = 0, BUTTON_STATE_PRESSED = BIT1} button_state_t;

// Setup timer hardware for 1 ms ticks
TA0CCR0 = 125;
TA0CCTL0 = CCIE;
TA0CTL = TASSEL_2 | ID_3 | MC_1 | TACLR;

// Process the button events for every timer tick
#pragma vector=TIMER2_A0_VECTOR
__interrupt void Timer_A2(void) // Better name would be Timer_A0_2_ISR
{
  // Local variable for counting ticks when button is pressed
  // Assume nobody will ever hold the button for 2^32 ticks long!
  static uint32_t counter = 0;
  
  // Constant for measuring how long (ms) the button must be held
  // Adjust this value to feel right for a single button press
  const uint32_t BUTTON_PRESSED_THRES = 50;
  
  // Check status of button, assume P2.1 input for the button
  // Note once the button enters release status, other code must change back to not pressed
  switch (button)
  {
    case BUTTON_STATUS_NOTPRESSED:
      // Increment if pressed, else reset the count
      // This will filter out the noise when starting to press
      if ((P2IN & BIT1) == BUTTON_STATE_PRESSED) counter += 1;
      else counter = 0;
      if (counter > BUTTON_PRESSED_THRES) button = BUTTON_STATUS_HOLDING;
      break;
    
    case BUTTON_STATUS_HOLDING:
      // Decrement if not pressed, else set the count to threshold
      // This will filter out the noise when starting to release
      if ((P2IN & BIT1) == BUTTON_STATE_NOTPRESSED) counter -= 1;
      else counter = BUTTON_PRESSED_THRES;
      if (counter == 0) button = BUTTON_STATUS_RELEASED;
      break;
  }
}

// Your other code to detect when the button has been pressed and release
if (button == BUTTON_STATUS_RELEASED)
{
    // do something
    // Must reset after detecting button has been release
    button = BUTTON_STATUS_NOTPRESSED;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 2013-01-31
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多