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.

CCS/MSP430G2553: Toggle led with interrupt

Part Number: MSP430G2553

Tool/software: Code Composer Studio

Here is my code for toggling led with interrupt but it is not working properly.

Please help me out

#include <msp430.h>


/**
* main.c
*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P1DIR |= ( BIT0 | BIT6 ); //set p1.0 & p1.6 as output
P1DIR &= ~BIT3; //set p1.3 as input
P1OUT |= BIT0; // Turn p1.0 led ON
P1OUT &= ~BIT6; // Turn p1.6 led OFF
P1REN |= BIT3; // Enable pull up/down p1.3
P1OUT |= BIT3; // Set p1.3 as pull up
P1SEL &= 0x00; // setting p1 pins as input/output
P1IE |= BIT3; //Enable interrupt for p1.3
P1IFG &= ~BIT3; // Clears Interrupt flag for p1.3
P1IES = BIT3; // Set High to low to p1.3


_bis_SR_register(LPM4_bits+ GIE);


return 0;
}

#pragma vector = PORT1_VECTOR
__interrupt void PORT1(void)
{
if( (P1IFG & BIT3) != 0) // check for p1IFG.3 is set or not
{
P1OUT ^= (BIT0 ); //toggle p1.0


P1IFG &= ~BIT3; // clears P1.3 interrupt flag
}
}

  • Hi,

    Could you please describe what you are seeing? Have you set breakpoints to see if you are even getting into the ISR?

    Regards,
    Nathan
  • Hii Nathan,

    I have tried adding a break point in ISR and i found that sometimes an interrupt is automatically generated to toggle the led upon low to high transition.

    But i have set to enable interrupt only on High to low transition by setting P1IES |= BIT3, and i also tried adding this line explicitly in ISR but then also it doesn't working.

    Regards 

    Avinash 

  • I put your code on a (Rev 1.5) G2 Launchpad and it functions as expected -- each button push (high->low) toggles the LED.

    There's no debouncing in this code. I've found the LP buttons to be mostly bounce-free, but it's always a possibility and it could masquerade as your symptom.

    Unsolicited: In general, you should clear the P1IFG bit after setting P1IES.

    [Edit: Fixed wording slightly.]

**Attention** This is a public forum