PDA

View Full Version : Timer Interrupts



gadelhas
- 19th August 2011, 00:07
Hi Again Everyone;

Help me out on this doubt;

Lets forget from a little moment, abour rule number one of interrupts. ( Get in, and get out ASAP )

If i have im my code 2 interrupts, one from timer1 that interrupts every 10mS, and other from timer2 that interrupts every 3mS. Inside the ISR of the timer1, i have a piece fo code that takes 8mS to complete, and on timer2 i have a routine that takes 2ms.

My doubt/question, is, what will happen when timer1 is trigger, and during the execution of the code inside the ISR, timer2 triggers also?

Darrel Taylor
- 19th August 2011, 01:17
While inside the ISR, interrupts are disabled (the GIE bit is cleared by hardware).
So it will wait until the current interrupt source has been serviced before handling the next one.
Unless you are using low priority interrupts, which can be interrupted by high priority interrupts.

If you are using ON INTERRUPT, the question would be irrelevant.

gadelhas
- 19th August 2011, 01:28
While inside the ISR, interrupts are disabled (the GIE bit is cleared by hardware).
If you are using ON INTERRUPT, the question would be irrelevant.

Thank you Darrel. Actually i'm using DT ints made by you!!!

So when the timer1 interrupt triggers, goes to the ISR and disable all the interrupts, when the ISR is completed it turns on all the interrupts again, thats it?

mister_e
- 19th August 2011, 01:33
..........

Unless you are using low priority interrupts, which can be interrupted by high priority interrupts.

amgen
- 19th August 2011, 01:34
I think your interrupts may interfere with each other. Tell me if I'm wrong but you should only stay in int for 10-20-50 micro secs, raise some flag if necessary and let basic code do the longer work.

don
amgen

gadelhas
- 19th August 2011, 01:36
I think your interrupts may interfere with each other. Tell me if I'm wrong but you should only stay in int for 10=20=50 micro secs, raise some flag if necessary and let basic code do the longer work.

don
amgen


I kown that! See my first post! I´m trying to understand the theory better, and what will happen in the case that i posted!!

amgen
- 19th August 2011, 01:47
yes, your way ahead. delete my post ......

gadelhas
- 19th August 2011, 01:54
..........

Ok, steve, lets see, if my timer1 is low priority, and timer2 is high priority. The timer interrupts and starts to execute the code inside the ISR, meanwhile the timer2 triggers, since is high priority jumps to its own execution. What will hapen?
After the ISR of the timer2 finishs it jumps again to the ISR of the timer1 to the place where it was interrupt by timer2?

SteveB
- 19th August 2011, 04:53
Ok, steve, lets see, if my timer1 is low priority, and timer2 is high priority. The timer interrupts and starts to execute the code inside the ISR [low priority], meanwhile the timer2 triggers, since is high priority jumps to its own execution. What will hapen?

The next address that would be executed in the Low Priority ISR is pushed onto the stack and then the program branches to the location of the High Priority ISR (0008h), where it begins to execute that code.


After the ISR of the timer2 finishs it jumps again to the ISR of the timer1 to the place where it was interrupt by timer2?

Yes. It pulls the address off the stack and branches back to the low priority ISR.

It will also set the global enable bit for the High Priority interrupts, so that if another High Priority interrupt occurs, it will branch again to that ISR. (Which is why it is critical to reset the interrupt flags in the ISR)

Eventually, it should finish with the Low Priority ISR and branch back to the main program flow. (This will also set the global enable bit for Low Priority interrupts)

Clear as mud?;)

SteveB
- 19th August 2011, 05:11
One other thing.

You use the example of 2 timers, 1 HP and 1 LP. Keep in mind these could be any of the components capable of raising an interrupt, such as...
EUSART Receive Interrupt, CCP1 Interrupt, A/D Converter Interrupt, High/Low-Voltage Detect Interrupt, Etc.
Any of these can be enabled and set either to HP or LP. Also, more than one can be set to HP or LP.

Imagine you had two of them set to LP, and 1 to HP. A/D and HLVD set to LP. TMR1 to HP.
So the A/D triggers. Program jumps to LP ISR. While executing the LP ISR, the HLVD interrupt occurs. What happens?
The LP ISR just continues as if no other interrupt occured because the LP enable bit was cleared when it first entered the ISR. When the RETFIE command (return from interrupt command) is executed the LP enable bit is set again. Assuming the A/D flag was reset, but not the HLVD, the program would immediately branch back to the LP ISR again. In these cases, where you have more than one item that can raise an interrupt, the ISR needs to check which flag was set so it knows what to do.

Now, at ANY time (in this example), if the TMR1 interrupt occurs, it jumps to the HP ISR, executes the ISR, then jumps back.

Hope I didn't make things even more muddy.

gadelhas
- 19th August 2011, 10:48
Many thanks SteveB, i understood perfectly.

Thank you everyone!

mister_e
- 19th August 2011, 16:00
Hope I didn't make things even more muddy.
It's my job to do so :D

Nice explanation!