首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MSP430 LaunchPad |分配双击按钮

MSP430 LaunchPad |分配双击按钮
EN

Stack Overflow用户
提问于 2021-01-09 04:32:05
回答 1查看 97关注 0票数 0

我正在使用MSP-EXP430F5529LP。我已经可以用以下命令闪烁红色的led了。

代码语言:javascript
复制
#pragma vector=TIMER2_A0_VECTOR
__interrupt void Timer_A2(void)
{
    P1OUT ^= 0x01;
}

代码语言:javascript
复制
    TA0CCR0 = 25000;
    TA0CCTL0 |= CCIE;
    TA0CTL |= TASSEL_2 | ID_3 | MC_1 | TACLR;

我真的需要一些帮助,使双击检查

代码语言:javascript
复制
#pragma vector=PORT1_VECTOR

因此,我可以将它与单击区分开来。我只想使用简单的if在那里,并做一些基于单击或双击的工作人员。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-20 14:23:52

通过使用计时器硬件闪烁LED的代码已经是检测双击按钮的一个很好的起点。我建议使用计时器来实现去抖动功能的基本功能,这是简单的向上/向下计数器,处理一些事件,如按钮输入。

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

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

代码语言:javascript
复制
// 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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65636008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档