This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

msp430g2553 stuck coding in switch& interrupt

Other Parts Discussed in Thread: MSP430G2553

hello.My LaunchPad board is msp430g2553. 

i am strucked programming about interrupt.

i founded this debouce switch code from internet and a little adapted .


#include "msp430g2553.h"
#define FLIP_HOLD (0x3300 | WDTHOLD) // flip HOLD while preserving other bits

#define S1 0x0001
#define S2 0x0002
#define SWITCH_MODE BIT3

unsigned char PressCountS1 = 0;
unsigned char PressCountS2 = 0;
unsigned char Pressed = 0;
unsigned char PressRelease = 0;

unsigned char NormalMode = 0; //default normal mode

void InitialiseSwitch2(void);

void main (void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT

P1DIR |= (BIT0|BIT6|BIT5|BIT4); // Set the LEDs on P1.0, P1.1, P1.2 and P1.6 as outputs
P1OUT = 0;
P1OUT |= (BIT0|BIT6); // Turn on P1.0 and P1.6 LEDs to indicate initial state

InitialiseSwitch2(); // Initialise Switch 2 which is attached to P1.3

// The Watchdog Timer (WDT) will be used to debounce s1 and s2
WDTCTL = WDTPW + WDTHOLD;
IFG1 &= ~(WDTIFG); // Clear the WDT and NMI interrupt flags
IE1 |= WDTIE ; // Enable the WDT and NMI interrupts
// The CPU is free to do other tasks, or go to sleep
__bis_SR_register(LPM0_bits | GIE);
}



/* This function configures the button so it will trigger interrupts
* when pressed. Those interrupts will be handled by PORT1_ISR() */
void InitialiseSwitch2(void)
{
P1DIR &= ~SWITCH_MODE; // Set button pin as an input pin
P1OUT |= SWITCH_MODE; // Set pull up resistor on for button
P1REN |= SWITCH_MODE; // Enable pull up resistor for button to keep pin high until pressed
P1IES |= SWITCH_MODE; // Enable Interrupt to trigger on the falling edge (high (unpressed) to low (pressed) transition)
P1IFG &= ~SWITCH_MODE; // Clear the interrupt flag for the button
P1IE |= SWITCH_MODE; // Enable interrupts on port 1 for the button
}

#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
if (P1IFG & SWITCH_MODE)
{
P1IE &= ~SWITCH_MODE; // Disable Button interrupt to avoid bounces
P1IFG &= ~SWITCH_MODE; // Clear the interrupt flag for the button

if (P1IES & SWITCH_MODE) // P1IES = P1.3 = 1 high to low
{ // Falling edge detected
P1OUT |= BIT0; // Turn on P1.0 red LED to indicate switch 2 is pressed
Pressed |= S2; // Set Switch 2 Pressed flag
PressCountS2 = 0; // Reset Switch 2 long press count
}
else
{ // Rising edge detected

///////////////////////
if (NormalMode == 4)
NormalMode = 0;
else
NormalMode++;

///////////////////////
P1OUT &= ~(BIT0+BIT6); // Turn off P1.0 and P1.6 LEDs
Pressed &= ~S2; // Reset Switch 2 Pressed flag
PressRelease |= S2; // Set Press and Released flag
}

if(NormalMode == 0){
P1OUT = 0;
P1OUT |= BIT4;
}
else if(NormalMode == 1){
P1OUT = 0;
P1OUT |= BIT5;
}

P1IES ^= SWITCH_MODE; // Toggle edge detect ----- when push this instruction charge edge detect low -> high when for detect pull and charge to high to low later
IFG1 &= ~WDTIFG; // Clear the interrupt flag for the WDT
WDTCTL = WDT_MDLY_32 | (WDTCTL & 0x007F); // Restart the WDT with the same NMI status as set by the NMI interrupt

}
else {/* add code here to handle other PORT1 interrupts, if any */}

}

// WDT is used to debounce s1 and s2 by delaying the re-enable of the NMIIE and P1IE interrupts
// and to time the length of the press
#pragma vector = WDT_VECTOR
__interrupt void wdt_isr(void)
{
if (Pressed & S2) // Check if switch 2 is pressed
{
if (++PressCountS2 == 47 ) // Long press duration 47*32ms = 1.5s
{
P1OUT |= BIT6; // Turn on the P1.2 LED to indicate long press
}
}

P1IFG &= ~SWITCH_MODE; // Clear the button interrupt flag (in case it has been set by bouncing)
P1IE |= SWITCH_MODE; // Re-enable interrupt for the button on P1.3
}

-------------------------------------------------------------------------------------------

The statement with color is the statement that I add later.

The Code without red statement is work perfectly. I can press the switch S2 and get input without bounce.
When I press the switch red LED  will on and off when I release. When i hold the switch  green LED will on.
Now, I add a global variable type unsigned char name "NormalMode" and value = 0 and  I add some statement  in ISR of port1 for add NormalMode'value by 1 everytime  the switch is released and use NormalMode'value for select output.
but It doesn't work ! when I press the switch Led will off and output set at PIN4 and green LED will on like hold the switch then pressing the switch doesn't cause anything.
I try to debug this coding and i found the red statement is cause the error but I don't know why and i can't solve it.
Please tell me what should i to do 
Thank you very much.

**Attention** This is a public forum