Electronic – SLEEP or busy-waiting. Which would service an interrupt faster

interruptspicsleep

I'm programming a PIC microcontroller to service two events via interrupts on a very time constrained environment.

PIC microcontrollers allow a SLEEP mode which wakes the PCU on any external interrupt (INTn) but has a wake delay depending on the selected oscillator mode.

I want to know which option would start servicing the interrupts faster. A main loop like this:

//CPU never goes to sleep, keeps executing a jump
void main(){
    while(1){}
}

or a main loop like this:

//CPU goes to sleep (idle), waits for INTn interrupts.
void main(){
    while(1){
     asm SLEEP;
    }
}

Waking from idle sleep seems to take 2 cycles according to the PIC18F4XK22 whereas a jump instruction (of an infinite while loop may take longer and may or may not be interruptible halfway into the execution of the jump.

Best Answer

You said it yourself, waking up from sleep incurs some delay, depending on the oscillator. The delay will be relatively small for R-C oscillators, and much longer for crystal oscillators.

The second method uses less power, assuming you can sleep for long enough on average for the reduced current to be meaningful.