avr/sleep.h Library

The assembly instruction that tells the microcontroller to enter sleep is the SLEEP instruction. For easier integration with C code, the avr/sleep.h library was created to offer functions to facilitate entering sleep.

The C library avr/sleep.h provides the following functions: sleep_enable(), sleep_disable(), set_sleep_mode(<mode>), sleep_cpu() and sleep_mode().

The set_sleep_mode(<mode>) function takes as argument one of the following macros: SLEEP_MODE_IDLE, SLEEP_MODE_STANDBY or SLEEP_MODE_PWR_DOWN. Using this function with one of the macros will set the corresponding sleep mode.

The sleep_enable() and sleep_disable() functions set and respectively clear the SEN bit.

The sleep_cpu() function invokes the SLEEP instruction.

The sleep_mode() function combines the previous functions. It calls sleep_enable(), then sleep_cpu(). When the device wakes up, the sleep_disable() function is called and then the program continues as usual.