5
\$\begingroup\$

When you enter the interrupt service routine of an Atmega328 that is triggered from a compare event of the TCNT and the value that is set, will the counter TCNT start counting again or rather, keep counting inside the interrupt handler or is it frozen until the interrupt handler is exited?

I will have an interrupt triggered every X seconds so I'm wondering about the timing of this interrupt. Because if the interrupt stops the TCNT from counting, then I will have an offset every second.

\$\endgroup\$
  • 1
    \$\begingroup\$ Worth noting that the timer/counter is a separate piece of hardware - hence why its behaviour is independent of what you're doing in your code. \$\endgroup\$ – sapi Dec 23 '19 at 5:10
10
\$\begingroup\$

The counter continues counting unless you set the necessary control bits to stop it. The interrupt handler won't stop it.

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

The counter will keep counting in the interrupt handler routine just as @DavidMolony has indicated.

There is another thing to keep in mind when the interrupt handler is dynamically updating something like the next next counting period. Lets say that your operational mode is to just let the timer continue to count from 0x0000 to 0xFFFF back to 0x0000 and continue like that. If you adjust the next output compare value on the fly by the amount needed to the next interrupt then there is no need to be concerned about the offset of the interrupt service routine. That is as long as the time to the next output compare interrupt is much longer than the time to process the current interrupt handler.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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