Slowing down the clock

Sometimes the clock – be it external or internal – is too fast, especially for simple applications (think blinking light) in power saving mode.

I have an example where I use Timer2 as a counter to raise interrupts to do stuff. Even at 8MHz the counter overflows too many times causing the chip to wake up.

Wouldn’t it be nice to slow the clock down so that the chip only wakes up when it needs to?

Well, we can’t slow the clock down but divide it down using a pre-scaler.

The pre-scaler can be set via the CLKPR register. It’s slightly complicated as it’s a 2 step process:

  1. First set CLKPCE while clearing out all other bits
  2. Then set CLKPS[0..3] bits with CLKPCE bit = 0

Example (for a 1/8 pre-scaler):

CLKPR = 0b10000000;
CLKPR = 0b00000011; // or 0x03

Alternatively:

#include <avr/power.h>

clock_prescale_set(x)
Value (CLKPR) Clock Division Factor
  0x01     2
  0x02     4
  0x03     8
  0x04    16
  0x05    32
  0x06    64
  0x07   128
  0x08   256

Leave a Reply