Source: http://brownsofa.org/blog/2011/01/10/the-compleat-attiny13-led-flasher-part-3-low-power-mode/
Timestamp: 2019-04-21 18:57:08+00:00

Document:
So here are my questions: What are the sleep modes and what do they do? What interrupt vector(s) are available and how do I enable them?
In the datasheet, §7 covers the sleep modes. Right at the top, Table 7-1 summarizes which clocks and oscillators are active, and how the device can be awaken from each of the three supported sleep modes. Looking at §7.1.1-3, the lowest power consumption sleep mode looks like “Power Down Mode” (no surprise), and once in that state it can be awoken by an external interrupt (i.e. a level change on a pin) or through the Watchdog Timer.
Looking at §8.4, the Watchdog timer (WDT) is a separately clocked counter/timer system, capable of generating interrupts which we can use to bring the device out of the Power Down sleep mode. §8.5.2 details the WDT’s register, and in particular Table 8-2 lists how to alter the length of the WDT’s time period.
We’ve sen that there’s a way to put the device to sleep, and a way to periodically wake it up. Last time our LED flasher toggled its LED state each time a timer-based interrupt was generated. Let’s do the same thing, except put the device to sleep once the LED state has been toggled. Hopefully this will reduce the power consumption overall, but mainly during the “off” part of the cycle.
We need to adjust the timer setup and interrupt we’re using, and add the power mode change. Working on the timer part first, we remove all references to Timer0, and replace them with set up for the WDT. Having read through §8.5.2 it’s clear that operating the WDT is very similar to Timer/Counter0. Setting the WDTIE bit enables WDT interrupts, and combinations of WDP3, WDP2, WDP1, and WDP0 adjust the prescaling and thus the WDT’s period. Looking at Table 8-2, setting WDP2 and WDP0 gives a 0.5s timer period.
To wrap up replacing the older timer code, we also need to change the interrupt vector we’re using. The documentation for interrupt.h shows that this is called WDT_vect.
Finally, because we can prescale the timer this low, we can also dispense with our old overflow counter.
If you set up the circuit and program this into the ATtiny13, the LED should flash on and off with a period of ~1 second, as you would expect. It might be interesting top find out what kind of power savings the sleep modes might give us, so before we add the sleep code let’s measure current drain right now. To make measurements a little easier slow down the flash rate by setting the prescaler to 4 seconds – WDTCR |= (1<<WDP3); – then set your multimeter to measure current and put in the main power loop somewhere. For me the most convenient way was to break the connection between VCC on the ISP breakout and pin 8 on the ATtiny, and insert the ammeter there. I measured 50mA on the mark (whilst the LED was on), and 2.3mA on the space.
is all there is to it. But what parameter do we pass to set_sleep_mode to enable Power Down Mode? Since it’s not in this page of the documentation, if you take a look at the code for the header file itself, at line 253 there’s a #define for SLEEP_MODE_PWR_DOWN that’s applicable to ATtiny13s.
Plugging in the ammeter again shows 47mA on the mark and 7µA on the space. These readings (and the previous set) are definitely within range of the typical DC characteristics shown in Table 18-1 in the datasheet. If you were using a CR1632 lithium coin cell rated at 125mAh, using the power down mode might increase battery life from 2.4h to 2.6h. Ok, that’s not fantastic but the huge majority of electron juice is used by the LED. If your circuit was a little more complex and had longer periods of inactivity, using the power down saves you a lot of power. If our LED were on for 1/10th of the time it were off, the battery duration change goes from an increase of 8% with the sleep code to an increase of 55%, which seems well worth it to me.
For more ways to reduce power consumption, read §7.4 in the datasheet.
Jumptuck’s LED Menorah is a real-world example of using the sleep modes to reduce power consumption – in that case it’s to reduce power whilst the device is “off”.
Go back to part 1 or part 2.

References: §7
 §7
 §8
 §8
 §8
 §7