Calling LED functions from an interrupt

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

I need to implement a rapid blink of the LED. I have a watchdog timer that goes off every 100 ms . In the timer ISR I  set a toggle blink flag to tell the main loop to write a value to the LED This works for the most part but occasionally  the LED stays on or off longer than 100 ms/ I think it's because the main loop executes slower and less regularly than the timer. and the flag value gets out of sync with the current value of the LED.

   

WDT_ISR()

   

{

   

toggle_blink = !toggle_blink;

   

}

   

main loop

   

if(toggle_blink)

   

WriteLED(!ReadLED());

   

So the question is, is it OK to call WriteLED from the ISR?

   

If the answer is yes, I need to check  a global variable to decide if the blink is needed. Wonder if that's OK too in the ISR

   

Thanks

   

Andy

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The M0 core at 24 MHz is quite fast enough to handle the if() and the LED write. To optimize use a local static uint8 for the LED state and

   

if(NeedToBlink) LEDPin_Write(LEDState = !LEDState);

   

Do not forget to switch off the LED when you set NeedToblink to false.

   

 

   

Bob

View solution in original post

3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The M0 core at 24 MHz is quite fast enough to handle the if() and the LED write. To optimize use a local static uint8 for the LED state and

   

if(NeedToBlink) LEDPin_Write(LEDState = !LEDState);

   

Do not forget to switch off the LED when you set NeedToblink to false.

   

 

   

Bob

Anonymous
Not applicable

Thanks Bob, that worked!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are always welcome, Andy.

   

 

   

Bob

0 Likes