The PIC microcontroller's sleep instruction is used to put the PIC microcontroller to sleep. In sleep mode the PIC microcontroller's clock is stopped and the 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 PIC microcontroller is in a device powered by batteries. The PIC microcontroller 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 PIC microcontroller 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 PIC microcontroller 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 PIC microcontroller executes programs, it must always perform the instruction immediately following the sleep when it wakes up. Even if you have set the PIC microcontroller 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 compiler 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.
/* enter the sleep mode */ sleep () ; /* nill instruction */ nop ();