# Timer interrupt (ISR)

I am using a timer interrupt for multiplexing however my question relates more closely to interrupt service routines.

If I set the prescaler and turn on a timer, using the below code I can run code asynchronous to the main loop at a certain interval depending on the prescaler. Or at least this is my understanding of it.

ISR(TIMER1_OVF_vect) { //code to run asynchronously at a certain interval }

Now if for example I set my timer to interrupt every millisecond (16000 cycles on at 16Mhz AVR) and the code I run takes for example 2 milliseconds to execute; is the code completely executed by the second timer interrupt. That is, does the next timer interrupt depend on the previous one completing and could having slow code slow down an interrupt?

I am using an Atmega328P-PU with an external 16Mhz crystal as clock.

• Your question needs more context. For instance, what AVR chip are you using? However, regardless of what chip you're using, it should be indicated in its instructions manual on how to utilize the ISR and its recommendations of setting it up. – KingDuken Jun 13 '18 at 3:54
• Despite the good general advice so far in the comments, there is a glaring unaddressed problem in your question: You can't cram $2\:\text{ms}$ of work into every $1\:\text{ms}$ of time. Period. Can't be done. (Of course, if you only need to do that much on rare occasions and can afford the price of delaying and/or missing the next event that's supposed to happen $1\:\text{ms}$ and $2\:\text{ms}$ later, then perhaps. But that's not how you wrote your question.) – jonk Jun 13 '18 at 4:40
• @JackCreasey The OP wrote "..set my timer to interrupt every millisecond ... and the code I run takes for example 2 milliseconds to execute." Doesn't fit. Of course, there are nuances. But I parenthetically waved a hand at those already. – jonk Jun 13 '18 at 7:11
• @Maple Where was a pre-emptive O/S mentioned? I may have missed that context. – jonk Jun 13 '18 at 8:41
• @jonk Nothing as complex as that. I supply software for a company making custom AT devices. When you have a dozen 200W servos and as many other sensors working simultaneously you are bound to face task management problem eventually. – Maple Jun 13 '18 at 9:53