Is there a way to programmatically determine at run time whether the code is executing from within an interrupt handler? I was hoping for a something like a bit in the status register but I haven't found any such thing.
Thanks
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.
Is there a way to programmatically determine at run time whether the code is executing from within an interrupt handler? I was hoping for a something like a bit in the status register but I haven't found any such thing.
Thanks
No, there is no automatic 'I'm in an ISR' flag.
When entering an ISR, the GIE bit in the status register is clear, but th eISR may set it to allow interrupt nesting, and during main, it may have been intentionally be cleared to avoid interrupts during critical code sections.
If you need such an info, it's best to define a volatile global valriable which is initialized 0 and set to 1 at the very start of each ISR and to 0 on ISR exit (or counted up and down, in case you have nested interrupts)
If there is no status bit, make your own :)
I agree with JMG. To add to it a bit further, if you're expecting nested interrupts create a volatile global variable for each and increment it in every ISR run. This way you can tell who is running after who.
Possibly a better approach is to use a global array and a counter variable. The first and last line of each ISR introduces a unique number to the array at the counter position and then increments the counting variable. For example you can have
Port1 ISR: uses the number 4
Port 2 ISR uses the number 5
Then after you run, the array might have the values
4,4,5,5
Which means one ISR ran after the other (no nesting)
or
4,5,5,4
Which means there was some nesting.
It's very quick and easy , but it can tell you a lot about what's going on as far as who is getting called and when. This is only for debugging and I'm only suggesting it because sending to the UART might take more time than you have.
Gustavo
**Attention** This is a public forum