简体   繁体   中英

MSP430 LaunchPad | Assign double click on button

I am using MSP-EXP430F5529LP. I can already blink the red led using

#pragma vector=TIMER2_A0_VECTOR
__interrupt void Timer_A2(void)
{
    P1OUT ^= 0x01;
}

and

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

I would really like some assistance in making a double click check on

#pragma vector=PORT1_VECTOR

so i can distinguish it from single click. I just want to use simple if's in there and do some staff based on single or double click.

Your code to blink the LED through the use of a timer hardware is already a good starting point towards detecting the double-press to a button. I suggest using a timer to implement the basics of a debounce function, which are simply up/down counters that process some event such as button input.

The thing about buttons is they are noisy. If you sample the input pin of the MSP430 from the button fast enough you will find it will toggle more than once for a simple press-and-hold event. Releasing the button produces noise too. So you can expect a lot of noise for the double-press event. It is possible to create external hardware circuits to filter out the noise part of the press, but software is cheaper so most implementations go this route.

The idea behind a debounce function is the timer is used to measure events from the button input to deal with the noise. Here is a starting point for detecting single button press. This can be modified easily for the double (or tripple) press!

// 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM