Switching prescaler on ATMEGA168 timer2 without losing count

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi - I'm using timer2 on an ATMEGA168PV for an RTC. In normal, high power mode, I have the prescaler set to 8 so that I can interrupt at 1024x/second. (clock = 32768Hz, prescaler = 8, OCR2A = 32768/(8*1024) - 1)

However, when in low power mode, I'd like to interrupt a lot slower. With the prescaler set to 8 I believe the slowest I can interrupt at is 16Hz (OCR2A = 32768/(8*16) - 1 = 255). That is OK - but I'd be happier if I could interrupt at 1Hz.

Is there any way to switch my prescaler values without losing any clock cycles?

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do not understand your maths.

If you are using a watch crystal on Timer2 ASSR, then you can overflow at 32768/1024/256 = 0.125Hz (8 secs).
If you use CTC mode, and OCR2A=31, you will get a TIM2_COMPA interrupt every second.

If you want to interrupt at 16Hz, OCR2A = 1.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
I do not understand your maths.

If you are using a watch crystal on Timer2 ASSR, then you can overflow at 32768/1024/256 = 0.125Hz (8 secs).
If you use CTC mode, and OCR2A=31, you will get a TIM2_COMPA interrupt every second.

If you want to interrupt at 16Hz, OCR2A = 1.

David.


My device operates in two modes:

High power mode:
interrupt rate = 1024hz
clock = 32768Hz
prescaler = 8
OCR2A = 32768/(8*1024) - 1) = 3

Low power mode:
Interrupt rate = 16Hz (but I'd like it to be slower)
Clock = 32768Hz
Prescaler = 8
OCR2A= 32768/(8*16) - 1 = 255

The /8 prescaler is the largest prescaler that I can use that will allow me to interrupt at 1024Hz. The next larger prescaler is /32, and to interrupt at 1024Hz I'd have to have OCR2A = 0. I'm pretty sure it won't work properly that way but I could be mistaken.

So what I'm wondering is if there is some way to switch prescalers without losing counts. I would ideally like to switch to the /64 prescaler when I go into low power mode, and switch back to the /8 prescaler when in high power mode.

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If you switch prescaler right at the beginning of its interrupt you should be fine.
George.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I still don't understand the concern, but how about this: Leave your timer at 1 second overflow for timekeeping purposes. Rain or shine.

When you are awake, enable a CTC interrupt at the desired interval. In the CTC ISR add the interval counts to the OCR register. When you go to sleep, simply disable the interrupt. When you awaken, set OCR = TCNT+interval; , clear the IF flag, and re-enable the interrupt.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This helped me to change rtc precision.

The prescaler is cleared when the mux is changed, but right after an interrupt, it would have all the low bits as 0 and you have 32 microseconds to change it without losing any cycles.

Excellent.