Consider a microcontroller with different power states. What will this code print?
#include <stdio.h> #define POWER_STATE_ACTIVE 1 #define POWER_STATE_SLEEP 2 int main() { int power_state = POWER_STATE_SLEEP; if (power_state == POWER_STATE_ACTIVE) { printf("Active Mode\n"); } else if (power_state == POWER_STATE_SLEEP) { printf("Sleep Mode\n"); } else { printf("Unknown Mode\n"); } return 0; }
Check the value assigned to power_state and compare it with the defined constants.
The variable power_state is set to POWER_STATE_SLEEP. The code checks this and prints "Sleep Mode" accordingly.
Which of the following is the main reason to manage power carefully in embedded systems?
Think about what happens when a device uses less power.
Reducing power consumption helps embedded devices run longer on batteries and prevents overheating, which is critical for reliability.
What error will this code cause when compiled?
#include <stdio.h> void set_power_mode(int mode) { switch(mode) { case 1: printf("Active mode set\n"); break; case 2 printf("Sleep mode set\n"); break; default: printf("Invalid mode\n"); } } int main() { set_power_mode(2); return 0; }
Look carefully at the switch cases and their syntax.
The case 2 line is missing a colon (:), causing a syntax error during compilation.
Choose the correct way to define a constant for low power mode in embedded C.
Remember how #define works in C for constants.
Option A correctly uses #define without an equals sign. Option A is invalid syntax. Option A is valid C but not a macro. Option A defines a variable, not a constant macro.
An embedded device runs 3 hours in active mode consuming 50mA, 5 hours in sleep mode consuming 5mA, and 16 hours in deep sleep consuming 1mA. What is the total average current consumption in mA?
Calculate weighted average: (current * hours) / total hours.
Total current = (50*3 + 5*5 + 1*16) / 24 = (150 + 25 + 16) / 24 = 191 / 24 ≈ 7.96 mA. The closest option is 8.5 mA (B).