Interrupt routine during delay

Discussion in 'Programmer's Corner' started by olvine, Apr 11, 2014.

  1. olvine

    Thread Starter Member

    Mar 10, 2014
    99
    0
    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 ?
     
  2. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,131
    267
    If interrupts are enabled, then they are serviced.

    Depends on how you write the code.
     
  3. olvine

    Thread Starter Member

    Mar 10, 2014
    99
    0
    yes they are enabled.
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Does the delay routine it self make use of interrupt?
     
  5. olvine

    Thread Starter Member

    Mar 10, 2014
    99
    0
    no it does not contain delay function
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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).
     
    olvine likes this.
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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).
     
    olvine likes this.
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    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.
     
    olvine likes this.
  9. olvine

    Thread Starter Member

    Mar 10, 2014
    99
    0
    Thanks alot guys !! I got it :)
     
Loading...