Tool/software:
Hi everyone,
I want to toggle my LED on the MSP-EXP430G2 Launchpad. I implemented a interrupt which gets activated, when I press the Button S2. In this interrupt I activate the Timer A0, in order to debounce the button.
Unfortunately, the debouncing does not work properly, because I can only jump into the Timer interrupt function, when the delay variable (count variable for the timer) is so small, that the debouncing doesn't work.
I do not get into the Timer Interrupt, when the Delay variable is too high. Do you have any suggestions what I am doing wrong here?
That is my code so far:
#include <msp430.h>
#define SW BIT3
#define GREEN BIT6
#define DEBOUNCE 20000
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= GREEN; // Set LED pin -> Output
P1OUT &= ~GREEN;
P1DIR &= ~SW; // Set SW pin -> Input
P1REN |= SW; // Enable Resistor for SW pin
P1OUT |= SW; // Select Pull Up for SW pin
P1IES |= SW; // Select Interrupt on Falling Edge
P1IE |= SW; // Enable Interrupt on SW pin
TA0CCR0 = DEBOUNCE; // Set delay time
TA0CCTL0 = CCIE; // Enable Timer A0 interrupt in compare mode
TA0CTL = TASSEL_2 | MC_0 | TACLR ; // Clock & timer stopped
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4 and Enable CPU Interrupt
}
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
TA0CTL |= MC_1; // set Timer A0
P1IFG &= ~SW; // Clear SW interrupt flag
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0_ISR(void)
{
// Stop Timer A0
TA0CTL &= ~MC_1; // Stop mode
// Check if the button is still pressed
if (!(P1IN & SW))
{
// Toggle output pin (P1.2)
P1OUT ^= GREEN;
}
// Clear Timer A0 interrupt flag
TA0CTL &= ~TAIFG;
}