What will be the output of this embedded C code snippet when the microcontroller enters sleep mode?
#include <stdio.h> #include <avr/sleep.h> int main() { printf("Before sleep\n"); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); printf("After sleep\n"); return 0; }
Consider what happens after sleep_cpu() in this code.
The microcontroller prints "Before sleep", then enters sleep mode. After waking up, it continues and prints "After sleep".
Which sleep mode is best suited for the lowest power consumption while keeping the RAM contents intact?
Think about which mode stops most clocks but preserves RAM.
Power-down mode stops almost all clocks, giving the lowest power consumption while preserving RAM contents.
What is the error in this code that prevents the microcontroller from entering sleep mode?
#include <avr/sleep.h>
void enter_sleep() {
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_cpu();
sleep_disable();
}Check syntax carefully line by line.
The line set_sleep_mode(SLEEP_MODE_IDLE) is missing a semicolon, causing a syntax error.
What is the output of this code snippet?
#include <stdio.h> #include <avr/sleep.h> int main() { printf("Start\n"); set_sleep_mode(SLEEP_MODE_STANDBY); sleep_enable(); sleep_cpu(); sleep_disable(); printf("End\n"); return 0; }
Consider the order of sleep_disable() and sleep_cpu() calls.
The MCU prints "Start", enters standby sleep, wakes up, disables sleep, then prints "End".
Which wake-up source can bring the microcontroller out of Power-down mode?
Think about which interrupts are active in Power-down mode.
In Power-down mode, external interrupts, watchdog timer, and some timer interrupts can wake the MCU.