AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page FAQ Library Reference Additional Documentation Example Projects

<avr/sleep.h>: Power Management and Sleep Modes


Detailed Description

 #include <avr/sleep.h>

Use of the SLEEP instruction can allow an application to reduce its power comsumption considerably. AVR devices can be put into different sleep modes. Refer to the datasheet for the details relating to the device you are using.

There are several macros provided in this header file to actually put the device into sleep mode. The simplest way is to optionally set the desired sleep mode using set_sleep_mode() (it usually defaults to idle mode where the CPU is put on sleep but all peripheral clocks are still running), and then call sleep_mode(). Unless it is the purpose to lock the CPU hard (until a hardware reset), interrupts need to be enabled at this point. This macro automatically takes care to enable the sleep mode in the CPU before going to sleep, and disable it again afterwards.

As this combined macro might cause race conditions in some situations, the individual steps of manipulating the sleep enable (SE) bit, and actually issuing the SLEEP instruction are provided in the macros sleep_enable(), sleep_disable(), and sleep_cpu(). This also allows for test-and-sleep scenarios that take care of not missing the interrupt that will awake the device from sleep.

Example:

    #include <avr/interrupt.h>
    #include <avr/sleep.h>

    ...
      cli();
      if (some_condition) {
        sleep_enable();
        sei();
        sleep_cpu();
        sleep_disable();
      }
      sei();

This sequence ensures an atomic test of some_condition with interrupts being disabled. If the condition is met, sleep mode will be prepared, and the SLEEP instruction will be scheduled immediately after an SEI instruction. As the intruction right after the SEI is guaranteed to be executed before an interrupt could trigger, it is sure the device will really be put on sleep.


Sleep Modes

Note:
Some of these modes are not available on all devices. See the datasheet for target device for the available sleep modes.


#define SLEEP_MODE_IDLE   0
#define SLEEP_MODE_ADC   _BV(SM0)
#define SLEEP_MODE_PWR_DOWN   _BV(SM1)
#define SLEEP_MODE_PWR_SAVE   (_BV(SM0) | _BV(SM1))
#define SLEEP_MODE_STANDBY   (_BV(SM1) | _BV(SM2))
#define SLEEP_MODE_EXT_STANDBY   (_BV(SM0) | _BV(SM1) | _BV(SM2))

Sleep Functions

void set_sleep_mode (uint8_t mode)
void sleep_mode (void)
void sleep_enable (void)
void sleep_disable (void)
void sleep_cpu (void)


Define Documentation

#define SLEEP_MODE_ADC   _BV(SM0)
 

ADC Noise Reduction Mode.

#define SLEEP_MODE_EXT_STANDBY   (_BV(SM0) | _BV(SM1) | _BV(SM2))
 

Extended Standby Mode.

#define SLEEP_MODE_IDLE   0
 

Idle mode.

#define SLEEP_MODE_PWR_DOWN   _BV(SM1)
 

Power Down Mode.

#define SLEEP_MODE_PWR_SAVE   (_BV(SM0) | _BV(SM1))
 

Power Save Mode.

#define SLEEP_MODE_STANDBY   (_BV(SM1) | _BV(SM2))
 

Standby Mode.


Function Documentation

void set_sleep_mode uint8_t  mode  ) 
 

Select a sleep mode.

void sleep_cpu void   ) 
 

Put the device into sleep mode. The SE bit must be set beforehand, and it is recommended to clear it afterwards.

void sleep_disable void   ) 
 

Clear the SE (sleep enable) bit.

void sleep_enable void   ) 
 

Set the SE (sleep enable) bit.

void sleep_mode void   ) 
 

Put the device in sleep mode. How the device is brought out of sleep mode depends on the specific mode selected with the set_sleep_mode() function. See the data sheet for your device for more details.


Automatically generated by Doxygen 1.4.1 on 23 Jan 2006.