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.

How to use Timer in Interrupt service routine ?



Hi,

I want when I press button LED gets on for some time and then switch off. for keeping led on for sometime can I use TIMER in ISR instead __delay_cycles() function ?

#pragma vector=PORT1_VECTOR
__interrupt void port1_ISR(void){


P1OUT |=0x01;
_delay_cycles(10000);      //instead of this delay Can I use timer or any other efficient way ?
P1OUT &=~ 0x01;
P1IFG = 0;

}

  • Yes, you can use the timer to do it. Configure the timer to get the desire ms or sec you want. When the timer finish the count, the msp430 will jump to the timer ISR. Inside the ISR you have to turn off the led. Check some examples code about the timer to understand how it works.
    Regards
    Gastón
  • you are right but I know how it works. I am going in ISR when button is pressed. I want to remain in ISR for few ms so that I can see LED is toggle. During ISR I want to use timer instead of delay cycles. 

  • Is not recomended to stay longer inside the ISR. For example you can set a flag inside the interrupt port_X ISR. If the flag is set, in your main program you can enable the timer count. You can wait to the timer finish the count by doing something like this do()while(!flag_timer). When the timer flag is set inside the TIMER ISR, the condition above will be false and then you can turn off the led and clear the timer flag and the port flag.
    Gastón
  • I forgot to mention, you can search problems like this in the forum and find different solutions.
    Gastón

**Attention** This is a public forum