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:
- First set CLKPCE while clearing out all other bits
- Then set CLKPS[0..3] bits with CLKPCE bit = 0
Example (for a 1/8 pre-scaler):
CLKPR = 0b10000000; CLKPR = 0b00000011; // or 0x03
#include <avr/power.h> clock_prescale_set(x)
|Value (CLKPR)||Clock Division Factor|