# On an AVR (ATtiny2313), which is serviced first when TIMER0_OVF and TIMER0_COMPA are due at the same time?

If an AVR's 8-bit timer compare register is 255 and the timer overflows (according to the datasheet, the compare interrupt happens on the next timer clock cycle) then both the overflow interrupt and the compare interrupt should be due at the same time. Which will be serviced first?

See the datasheet for the ATtiny2313: http://www.atmel.com/dyn/resources/prod_documents/doc2543.PDF

The priority of interrupts is determined by the order of the interrupt vectors. See page 46 for the list. The lower the number, the higher the priority. Since Timer/Counter0 Overflow has a lower number (7) than Timer/Counter0 Compare Match A (14), the overflow ISR will run first.

• Awesome. I thought it would be something like that. – joeforker Jun 24 '10 at 14:01

I really hope somebody knows this, and that you get a straightforward answer with good reasoning from the docs, examples of how to plan for simultaneous interrupts, etc. but all I can tell you right now is that it's the one that's still running when you do

ISR(TIMER0_OVF_vect)
{
printf("OVF ISR Ran First\n"); //or whatever your console output uses
fflush(stdout);

while(1);
}

ISR(TIMER0_COMPA_vect)
{
printf("COMPA  ISR Ran First\n"); //or whatever your console output uses
fflush(stdout);

while(1);
}

• I don't know about these AVRs but on every embedded system that I have ever worked on, and there have been plenty, you cannot/must not use printf or its equivalent from inside an interrupt function. As a start, if the output device uses interrupts then this function, as it never returns, will probably block its operation. – uɐɪ Jun 24 '10 at 14:58
• I understand that it's bad style, but all I wanted to do was output where the code was. A toggled LED, global string in a Big Loop, or any number of outputs could have been used, but printf is easy to read. The point was to write a blocking interrupt (which is definitely bad practice in 99% of the cases I can imagine!) – Kevin Vermeer Jun 24 '10 at 18:42