avr_sleep(3avr)
π€ NAME π€
avr_sleep β <avr/sleep.h>: Power Management and Sleep Modes β¨
okay bestie, listen up, this is the header that lets your AVR take a lil nappy π΄ and stop gobbling battery like it's a free sample table. Sleep modes, the SE bit, BOD shenanigans β it's all here. iykyk.
β¨ SYNOPSIS β¨
πΈ Functions πΈ
void sleep_enable (void)
void sleep_disable (void)
void sleep_cpu (void)
void sleep_mode (void)
void sleep_bod_disable (void)
π¦ Detailed Description π¦
(yeah Doxygen just kinda put a heading here and dipped, no extra text, we move π β the real tea is down in Function Documentation)
π΄ FUNCTION DOCUMENTATION π΄
(these are all marked [extern], no cap)
π void sleep_bod_disable (void ) [extern]
Turn off the BOD before you head to sleep. Not available on all devices, so check your datasheet before you manifest this one. π
π void sleep_cpu (void ) [extern]
This is the one that actually puts the device into sleep mode. β But likeβ¦ the SE bit has to be set beforehand, and it's recommended to clear it afterwards. Don't skip steps, queen.
π void sleep_disable (void ) [extern]
Clears the SE (sleep enable) bit. Simple. Iconic. π
π void sleep_enable (void ) [extern]
#include <avr/sleep.h>
Using the SLEEP instruction can let an application reduce its power consumption considerably. πβ¨ AVR devices can be put into different sleep modes, and you should go read the datasheet for the deets relating to your specific device. No guessing, bestie.
There are several macros in this header file to actually put the device into sleep mode. The simplest way is to optionally set the desired sleep mode with 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(). That macro automatically sets the sleep enable bit, goes to sleep, and clears the sleep enable bit. main character energy, it does everything for you π
Example:
#include <avr/sleep.h>
...
set_sleep_mode(<mode>);
sleep_mode();
β οΈ real talk: unless your whole goal is to completely lock the CPU until a hardware reset, interrupts need to be enabled before you go to sleep. Otherwise you're justβ¦ stuck there. bestie no.
Because the sleep_mode() macro might cause race conditions in some situations, the individual steps β messing with the sleep enable (SE) bit and actually issuing the SLEEP instruction β are provided as separate macros: sleep_enable(), sleep_disable(), and sleep_cpu(). This also unlocks test-and-sleep scenarios that make sure you don't miss the interrupt that's supposed to wake your device back up. π
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 guarantees an atomic test of some_condition with interrupts disabled. If the condition is met, sleep mode gets prepped, and the SLEEP instruction gets scheduled right after an SEI instruction. And because the instruction immediately after the SEI is guaranteed to execute before an interrupt can trigger, you know the device will genuinely be put to sleep. π―
Some devices can disable the Brown Out Detector (BOD) before going to sleep, which also reduces power while sleeping. π If your specific AVR device has that ability, then an extra macro is defined: sleep_bod_disable(). This macro generates inlined assembly code that correctly implements the timed sequence for disabling the BOD before sleeping. BUT β and this is the important bit β there's a limited number of cycles after the BOD has been disabled in which the device can be put into sleep mode, otherwise the BOD will not truly be disabled. The recommended practice is: disable the BOD (sleep_bod_disable()), set the interrupts (sei()), and then put the device to sleep (sleep_cpu()), like so:
#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.
π void sleep_mode (void ) [extern]
Put the device into sleep mode, taking care of setting the SE bit before, and clearing it afterwards. π It's the low-effort, high-reward option. slay.
π«Ά AUTHOR π«Ά
Generated automatically by Doxygen for AVR-LibC from the source code. (not me writing a lil love letter, it's the robots π€π)
and that's the whole page fr fr β brought to you by AVR-LibC, Version 2.2.1, the avr_sleep(3avr) girlies π
β¨. go put your microcontroller to bed, she's tired. ππ
π¦ where this page comes from
- ubuntu resolute: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- ubuntu stonking: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms