Sleep / Power — pymcu.hal.power#
from pymcu.hal.power import sleep_power_down
Sleep mode helpers that halt the CPU and reduce current consumption. Returning from any
sleep_* function means the MCU has woken from sleep.
Warning
Interrupts must be enabled
Global interrupts (sei) must be enabled before calling any sleep function, or the MCU will
sleep indefinitely. The compiler emits sei automatically when at least one @interrupt
handler is registered; otherwise call asm("sei") explicitly.
Sleep modes#
Function |
Typical current (3 V, 25 °C) |
Wake sources |
|---|---|---|
|
~6 mA |
All interrupts |
|
~1 mA |
ADC complete, ext-int, TWI address match, Timer2 |
|
~0.1 µA |
External INT0/INT1, TWI address match, WDT |
|
~0.1 µA |
Same as power-down + Timer2 async |
|
~0.1 µA |
Same as power-down (faster oscillator startup) |
Examples#
ADC noise reduction sleep#
from pymcu.hal.adc import AnalogPin
from pymcu.hal.power import sleep_adc_noise
from pymcu.types import uint16
sensor = AnalogPin("A0")
def read_quiet() -> uint16:
sensor.start()
sleep_adc_noise() # CPU sleeps; ADC completes, ADC ISR wakes CPU
return sensor.value()
Watchdog-based periodic wake (~0.1 µA between wakes)#
from pymcu.hal.power import sleep_power_down
from pymcu.hal.watchdog import Watchdog
wdt = Watchdog(timeout_ms=1000)
@interrupt(0x0018) # WDT vector
def on_wdt():
pass
def main():
wdt.enable()
asm("sei")
while True:
sleep_power_down()
do_periodic_task()
ATmega328P register map#
Register |
Address |
Description |
|---|---|---|
|
|
Sleep Mode Control Register (SM2:0, SE) |
|
|
MCU Control Register (BODS, BODSE) |