ENع
→Sleep modes
الدرس 44 · Low power and reliability

Sleep modes and wake-up sources

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةAdvanced
عن هذا الدرس

A battery node lasts months only if its microcontroller spends almost all its time asleep. Sleeping means switching clocks off: each deeper sleep mode stops more of them and draws less current, but leaves fewer things that can wake the chip. Follow an ATmega328P from 1.7 mA awake down to 4.2 µA in Power-down, see sleep_mode() stop the CPU on one line, and wake it again with two wake-up sources: the watchdog timer every 8 seconds and a button on pin 2. Then pay the catch (millis() stops) and collect the payoff: about 4.6 µA on average. The lesson “Power budgets and battery life” turns such averages into battery life; the Hardware lessons on power supplies and batteries cover the cells themselves.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 44 · تفاعلي
01 / 09

Awake: every clock ticking

At 4 MHz and 3 V an awake ATmega328P draws about 1.7 mA. Three clocks tick: the CPU clock, the I/O clock for timers and serial, and the watchdog's own oscillator. Every tick costs a little charge, even inside delay().

chip     ATmega328P, 4 MHz, 3 V  (datasheet Table 29-8, typical)
active   1.7 mA
clocks   CPU on · I/O on · WDT on
delay()  waits awake: the clocks keep ticking
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Awake: every clock ticking

    At 4 MHz and 3 V an awake ATmega328P draws about 1.7 mA. Three clocks tick: the CPU clock, the I/O clock for timers and serial, and the watchdog's own oscillator. Every tick costs a little charge, even inside delay().

    chip     ATmega328P, 4 MHz, 3 V  (datasheet Table 29-8, typical)
    active   1.7 mA
    clocks   CPU on · I/O on · WDT on
    delay()  waits awake: the clocks keep ticking
  2. Idle: stop the CPU clock

    A sleep mode switches clocks off. Idle stops only the CPU clock. Timers, serial and the ADC (the voltage reader) keep running, so almost any interrupt wakes it: a signal that pulls the CPU into a short routine. About 0.3 mA.

    sleep mode  which clocks keep ticking
    Idle        CPU off · I/O on · WDT on   0.3 mA
    wakes on    almost any interrupt: timer, serial, pins, ADC
  3. Power-down: only the watchdog

    Power-down stops every clock but the watchdog's: 4.2 µA, about 400 times less than awake. With the watchdog off too: 0.1 µA. Power-save keeps only Timer2 on its 32 kHz crystal, watchdog off: 0.9 µA. Fewer clocks, fewer ways to wake.

    Active      CPU on  · I/O on  · WDT on              1.7 mA
    Idle        CPU off · I/O on  · WDT on              0.3 mA
    Power-down  CPU off · I/O off · WDT on              4.2 µA
    Power-save  CPU off · I/O off · WDT off · Timer2    0.9 µA  (32 kHz crystal)
    Power-down  CPU off · I/O off · WDT off             0.1 µA
    compare like with like: every figure lists what still ticks
  4. sleep_mode(): stop on this line

    setup() chose SLEEP_MODE_PWR_DOWN. sleep_mode() sets the sleep-enable bit and runs the SLEEP instruction. The clocks stop and the CPU halts right on this line. SRAM and registers keep their values; no code runs at all.

    setup()       set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    line 4        sleep_mode();
      = sleep_enable();   SE bit = 1
        sleep_cpu();      SLEEP instruction: clocks stop here
        sleep_disable();  runs only after a wake-up
    now           4.2 µA, SRAM and registers kept
  5. Wake-up source 1: the watchdog

    armWakeups() set the watchdog to interrupt mode. It counts its own 128 kHz oscillator: after 1,048,576 ticks, about 8 seconds, it raises an interrupt, and the interrupt wakes the chip.

    line 3     armWakeups();
      WDTCSR = WDCE | WDE           unlock (timed sequence)
      WDTCSR = WDIE | WDP3 | WDP0   interrupt mode, 8 s
    WDT        1,048,576 cycles of 128 kHz  ->  about 8 s
    time-out   WDT_vect interrupt  ->  clocks restart
  6. Carry on after SLEEP

    The chip waits for its clock to settle, plus four cycles, then runs the interrupt routine, ISR(WDT_vect). Then it carries on after the SLEEP instruction: sleep_mode() returns, loop() starts again and measure() reads the sensor.

    wake-up    clock start-up time + 4 cycles
    ISR        ISR(WDT_vect) {}   (nothing to do but wake)
    resume     the instruction after SLEEP: sleep_disable()
    line 2     measure();   analogRead(A0), then sleep again
  7. Wake-up source 2: a button

    A button on pin 2 also wakes it. In Power-down a pin can wake the chip only with a LOW level on pin 2 or 3, or a pin change: not a rising or falling edge. The token is that LOW level.

    attachInterrupt(digitalPinToInterrupt(2), onButton, LOW);
    Power-down wakes on: INT0/INT1 LOW level, pin change,
                         watchdog, TWI address match, reset
    not on:              RISING/FALLING edges on INT0/INT1,
                         serial data, Timer0/1/2 (Timer2 only in Pwr-save)
  8. The catch: millis() stops

    In Power-down the I/O clock is off, and so is Timer0, which counts millis(). Each 8 s sleep is missing from millis(). Serial data cannot wake the chip either: bytes that arrive while it sleeps are lost.

    millis()   counted by the Timer0 overflow interrupt
    Timer0     runs on the I/O clock  ->  stopped in Power-down
    so         after 10 sleeps of 8 s, millis() has gained
               only the awake time, not 80 s
    serial     no wake-up from Power-down; keep time with the WDT count
  9. The payoff: 4.6 µA on average

    Say measure() takes 2 ms, then it sleeps 8 s. The average is about 4.6 µA, 370 times less than staying awake: about 41 mAh a year instead of 14,892. The watchdog's 4.2 µA is now most of the bill.

    awake    1.7 mA x 2 ms   = 3.4 µA·s
    asleep   4.2 µA x 8 s    = 33.6 µA·s
    average  37.0 µA·s / 8.002 s = 4.62 µA
    a year   41 mAh   (awake all year: 14,892 mAh)
    watchdog 91 % of the charge

اختبار يتاح بعد الخطوة الأخيرة.