Watchdog timer and idle mode

Since the RGB Tumbler will be running off a single coin cell I will need to make sure the ATmega microprocessor doesn't use a single microamp more than necessary. And there are several ways of doing that.

Firstly, let's switch off all peripherals that we won't be using. The "Power Reduction Register", or PRR, is the place for that (refer to section 9.11.3 of the datasheet). And also ACSR for disabling the analog comperator:

// Power-down unused stuff
PRR |= (1<< PRTWI) | (1<< PRTIM1) | (1<< PRSPI) | (1<< PRUSART0); // TWI, Timer1, SPI & USART off
ACSR |= (1<< ACD); // Power-down analog comperator

I have left Timer0 and Timer2 running and turned everything else off.

The next thing is to let the MCU sleep when it's not doing anything. The accelerometer has a sampling frequency of 50 Hz so there are lots and lots of CPU cycles where we are just waiting for the accelerometer. The AVR has several different degrees of sleeping (section 9.1 of the datasheet), but since we will be needing the PWM output to remain active at all times I can only use the "idle" mode that saves the least amount of power. If I had used the NCP5623 LED driver instead I could turn everything off and use "power-down" mode instead and save lots more power.

I am using the Watchdog Timer (section 10.8 in the datasheet) to wake up the CPU periodically.  With a prescaler of 0, the Watchdog times out every 2048 cycles of the 128 kHz Watchdog oscillator for a 16 ms delay or a 62.5 Hz frequency. In other words, the Watchdog kicks the CPU awake every 16 ms to let it do its stuff (everything takes place in the Watchdog Timer interrupt handler) after which it will go to sleep again.

To see how much time was spent awake and how much time was spent sleeping, I useed an output pin which I set high on wake-up and low just before sleeping. This is what it looks like on my scope:

Channel two (blue) shows the "CPU active" pin. Channel one (yellow) is the PWM output for one of the LEDs.

As can be seen, the CPU is only active about 4% of the time and sleeping the remaining 96%.
Here's a close-up of the active time:

The CPU is only active about 600 µs every 16 ms. And in that time, three analog readings are taken from the accelerometer and the PWM duty cycles are adjusted correspondingly.

One comment on "Watchdog timer and idle mode"
Sam says:

Hi! I found this article while googling for info about the sleep modes on the ATtiny84. I'm working on a Voltmeter Clock project, and I want to send a PWM signal to my analog meters, keep the external crystal oscillator ticking, and sleep everything else. I was reading through the documentation and I'm pretty sure I can do it in "Stand by" mode.

Check out the product page here

and the github here:

if you feel like critiquing a mechanical engineer who is trying to learn some electrical stuff, I'm currently trying to decide how to power my board.

Leave a Reply