The PICmicro sleep instruction is used to put the PICmicro to sleep. In sleep mode the PICmicro clock is stopped and the PICmicro program no longer run. However, the contents of all the input and output ports are maintained as are the values of variables.

Sleep is used to minimise power consumption if your PICmicro is in a device powered by batteries. The PICmicro will reawaken if an interrupt occurs or a watchdog timeout takes place. The sleep can be ended by an input event or a PORTB input change. When the PICmicro is asleep the system clock is stopped (but not the watchdog timer) and so timer overflows will not take place.

If interrupts are enabled when the sleep ends the sleep acts as an interrupt and the PICmicro will execute the interrupt handler before resuming the foreground program. If interrupts are turned off when sleep ends the program will continue where it left off. One point to remember is that, because of the way that the PICmicro executes programs, it must always perform the instruction immediately following the sleep when it wakes up. Even if you have set the PICmicro up to cause an interrupt on waking, the instruction which immediately follows the sleep will still be performed. It is traditional to place a null instruction (nop or no-operation) after the sleep to explicitly give the processor something to do at this point.

The C2C provides a function called sleep which will invoke a sleep instruction. It also provides one called nop which invokes the instruction which performs no processing.