MSP430 发射台 |分配双击按钮
MSP430 LaunchPad | Assign double click on button
我正在使用 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 并根据单击或双击做一些工作人员。
您通过使用计时器硬件使 LED 闪烁的代码已经是检测双击按钮的良好起点。我建议使用计时器来实现去抖动功能的基础知识,它只是 up/down 处理某些事件(例如按钮输入)的计数器。
关于按钮的问题是它们很吵。如果您足够快地从按钮采样 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;
}
我正在使用 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 并根据单击或双击做一些工作人员。
您通过使用计时器硬件使 LED 闪烁的代码已经是检测双击按钮的良好起点。我建议使用计时器来实现去抖动功能的基础知识,它只是 up/down 处理某些事件(例如按钮输入)的计数器。
关于按钮的问题是它们很吵。如果您足够快地从按钮采样 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;
}