voidsleep_bod_disable(void)[extern]
Disable BOD before going to sleep. Not available on all devices.
voidsleep_cpu(void)[extern]
Put the device into sleep mode. The SE bit must be set beforehand, and it is recommended to clear it
afterwards.
voidsleep_disable(void)[extern]
Clear the SE (sleep enable) bit.
voidsleep_enable(void)[extern]
#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().Thismacroautomaticallysetsthesleepenablebit,goestosleep,andclearsthesleepenablebit.
Example:
#include <avr/sleep.h>
...
set_sleep_mode(<mode>);
sleep_mode();
Note that unless your purpose is to completely lock the CPU (until a hardware reset), interrupts need to
be enabled before going to sleep.
As the sleep_mode()macromightcauseraceconditionsinsomesituations,theindividualstepsofmanipulatingthesleepenable(SE)bit,andactuallyissuingtheSLEEPinstruction,areprovidedinthemacrossleep_enable(),sleep_disable(),andsleep_cpu().Thisalsoallowsfortest-and-sleepscenariosthattakecareofnotmissingtheinterruptthatwillawakethedevicefromsleep.
Example:
#include <avr/interrupt.h>
#include <avr/sleep.h>
...
set_sleep_mode(<mode>);
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 to sleep.
Some devices have the ability to disable the Brown Out Detector (BOD) before going to sleep. This will
also reduce power while sleeping. If the specific AVR device has this ability then an additional macro is
defined: sleep_bod_disable().ThismacrogeneratesinlinedassemblycodethatwillcorrectlyimplementthetimedsequencefordisablingtheBODbeforesleeping.However,thereisalimitednumberofcyclesaftertheBODhasbeendisabledthatthedevicecanbeputintosleepmode,otherwisetheBODwillnottrulybedisabled.RecommendedpracticeistodisabletheBOD(sleep_bod_disable()),settheinterrupts(sei()),andthenputthedevicetosleep(sleep_cpu()),likeso:
#include <avr/interrupt.h>
#include <avr/sleep.h>
...
set_sleep_mode(<mode>);
cli();
if (some_condition)
{
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
}
sei();
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.
Set the SE (sleep enable) bit.
voidsleep_mode(void)[extern]
Put the device into sleep mode, taking care of setting the SE bit before, and clearing it afterwards.