Interrupt routine during delay

Thread Starter

olvine

Joined Mar 10, 2014
99
Hey,

I want to know that if delay function is called and during that delay function's execution some interrupt came along then what would compiler do ? would it go to isr or execute the delay function first ?
 

THE_RB

Joined Feb 11, 2008
5,438
Normally the delay functions count operating cycles, they don't use a timer or an interrupt.

So if an int occurs during the delay, the delay will be lengthened and no longer times an accurate period.

You should get into the habit of making delays using one of the hardware timers. That way the interrupt won't normally affect the delay time (unless it happens right at the end of the delay).
 

tshuck

Joined Oct 18, 2012
3,534
Hey,

I want to know that if delay function is called and during that delay function's execution some interrupt came along then what would compiler do ? would it go to isr or execute the delay function first ?
The compiler has already run and isn't doing anything at this point.

The delay routines are typically "dumb" delays as they block the processor and simply make the processor busy for a certain amount of cycles. As this is operating in a normal context, it can be interrupted - that is, the program counter is forced to the interrupt vector location in memory (interrupting the delay function). Once the interrupt has been serviced, the program will continue at which point it left of in the delay routine, which, as THE_RB noted, will result in a longer program delay.

If, however, the delay was implemented in interrupt context (often not a good idea), and an interrupt occurred of the same hardware priority, the delay function would finish before the interrupt was serviced. If the same happened and the interrupt was a higher hardware priority interrupt, the interrupt would be serviced, the delay would finish, then the program would return to normal context (assuming you serviced all interrupts first).
 

ErnieM

Joined Apr 24, 2011
8,377
I often find it useful to have a 1 millisecond time tick active in my programs. By using the Timer 2 match facility one can get interrupts in exact intervals, and these ISR routines can update global time variables.

So the ISR just runs off on the side without any main code attention, and you can use the global variable to implement delays and such.

One caveat: if the tick variable is larget then the bus (ie, >8 bits on an 8 bit machine) you have to be careful using it, as it can be updated as you are reading it. That leads to weird seemingly random errors.
 
Top