What should be expected if the code in the timer interrupt ISR does not finish before the interrupt is called again? For example Timer0 ISR(TIMER0_COMPA_vect){} on the Uno. This is for debugging purposes in a complex program. Thanks.


2 Answers 2

  1. the timer will set the Timer Interrupt Flag (TIFR0 register)
  2. The ISR will be called, and the flag will be cleared
  3. you do some long calculations or something
  4. the timer will match OCR0A, and set the interrupt flag again.
  5. you calculations are done, and the ISR function will end.
  6. the uC will jump back to the main loop code, and execute 2 1 instructions.
  7. The uC will see the flag, and call the ISR again. As per step 2.

PS please note that by default the ISR macro will turn off global interrupt (cli) at the beginning of the function, and set it back at the end. You can re-enable interrupts inside the ISR, but you have to be very careful here, as interrupts can be nested, and cause stack-overflows.

You can re-enable interrupts inside the ISR, but that only means other interrupt may interrupt this ISR, but never the same vector. (i.e. only one ISR per vector can run at the same time). So in your case the timer ISR can't be interrupted by the same timer interrupt.

  • 1
    Congrats, that's a pretty good summary of what happens in this situation.
    – jfpoilpret
    Aug 14, 2016 at 19:38
  • 1.- My understanding is that the uC will execute a single instruction (not 2) of the main code before entering the ISR again. 2.- Where did you read that only one ISR per vector can run at the same time? It's the first time I read anything like this. Aug 15, 2016 at 8:24
  • Note that if you re-enable interrupts inside the ISR, then the same interrupt definitely can re-enter the same vector. This is one reason why re-enabling interrupts inside an ISR needs careful thought... You said it yourself: (step 2) - "...the flag will be cleared." Aug 15, 2016 at 9:18
  • Apparently I was wrong in the last paragraph. I'm not sure where I got that from, but it doesn't appear anywhere in the datasheet.
    – Gerben
    Aug 15, 2016 at 9:35

It won't be called until the current interrupt is finished. As soon as the current interrupt function returns the pending interrupt would trigger and call the ISR again.

  • You need to explicitly allow interrupts to be interrupted. For more information I suggest you see section nested interrupts on avr-libc.
    – Kwasmich
    Aug 15, 2016 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.