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.

blinking leds using ports interrupt

Other Parts Discussed in Thread: MSP430G2553

Need help to blink the led1(red)  5times only when switch1 button is pressed. This is far what ive got. Thanks!

#include <msp430g2553.h>
unsigned int i;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction
P1OUT &=~(0x01); Off
P1REN=0x08; //Enable interal pull up resistors
P1IE |= 0x08; // P1.3 interrupt enabled
P1IES |= 0x08; // P1.3 Hi/lo edge
P1IFG &= ~0x08; // P1.3 IFG cleared
_BIS_SR(GIE); // General Interrupt Enable

while(1){ //Loop - Do nothing
if (i>0)
{
P1OUT ^=0x01;
__delay_cycles(500000);
}
}

}

// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
i^ =0x01;
P1IFG &= ~0x08; // P1.3 IFG cleared
P1OUT &=~(0x01); //Clear the LEDs so they start in OFF state

}

  • use </> button for your code.

    move _BIS_SR to after the while

     while(1){                           // Loop forever
     _BIS_SR(LPM0_bits + GIE);			// Enter LPM0 Sleep w/ interrupt
     int i = 10; while(--i){            // toggle led 10 times = 5 blinks
     ...
    in the irq put: __bic_SR_register_on_exit(LPM0_bits);

    Should move P1OUT &=~(0x01) to main just above int i =10;
    as button switch bounce will  reset  the led toggle state a few times and routine in main could end up leaving LED ON after all toggles.
    A reason to not use xor but: loop 5 ( ledon,delay,ledoff,delay) as you will know the outcome.

    next learn how to use timer instead of delay cycles

    and after that: OUTMOD_4    /* PWM output mode: 4 - toggle */
    or set bit-pattern in a 16bit word that BIT0 is the LED state for 1/8sec and then you right-shift it one bit, done by a timer IRQ
    You can then have all types of blinking patterns

**Attention** This is a public forum