4
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ Idle sleep seems to wake in ~2 clock cycles. If jump instructions cannot be interrupted halfway (I don't know) idle sleep might be a better option. Power consumption, not an issue. \$\endgroup\$
    – NeonMan
    Sep 3, 2014 at 15:02
  • \$\begingroup\$ @NeonMan - add that information about 2-clock-wake to your question. Maybe include sub-questin about un-interuptable jump. \$\endgroup\$
    – gbulmer
    Sep 3, 2014 at 16:14
  • \$\begingroup\$ Also add that part about "Power consumption, not an issue". I predict that you will get nearly universal agreement to keep the thing awake. Put in 10 NOP instructions (or a hundred) if you want a 90 (or 99) percent chance of catching the interrupt without a JMP instruction annoying you. \$\endgroup\$
    – User.1
    Sep 3, 2014 at 22:43
  • \$\begingroup\$ @OlinLathrop interesting. Can you add details on why a crystal would take longer? I imagine the RC only takes the time it takes to charge the cap. Ignoring the chip cycles to get out of sleep. \$\endgroup\$
    – kenny
    Sep 3, 2014 at 23:10
  • \$\begingroup\$ Interrupts can be serviced in between instructions. If an interrupt request happens while an instruction is executing, the instruction must run to completion before the interrupt can be serviced. I think the question you're actually worrying about is, what is the min/max interrupt latency for PIC18F. \$\endgroup\$
    – MarkU
    Sep 4, 2014 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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