Go Down

### Topic: Timer interrupts (Read 1 time)previous topic - next topic

#### blackm0k

##### Mar 20, 2012, 04:10 am
Hi,

I'm just trying to understand the way that the Arduino (and ATmega) handles timer interrupts, with a view to using one in a project in the near future. I have been looking at the Timer1 library, cross-referencing to the ATMega168/328P data-sheet, in order to get an understanding of a working example.

However, I've come across something where I can't get the same results as are given on the Timer1 page on the playground http://arduino.cc/playground/Code/Timer1. When I calculate the maximum period for any value of the prescaler, I always get half the value given on this wiki page.

The formula the author has stated for obtaining the maximum period is
Quote
Max Period = (Prescale)*(1/Frequency)*(2^17)
- why is this 2^17? From my understanding Timer1 is a 16-bit timer and therefore there would be a maximum value for the register of 2^16 - 1, thus causing overflow (and so, the timer interrupt to trigger) at 2^16.

Am I missing something here?

#### MarkT

#1
##### Mar 20, 2012, 01:08 pm
Yes, you are missing an understanding of the phase-correct PWM modes, where the counter counts down to BOTTOM, then counts up to TOP, then counts down to BOTTOM etc etc.  Thus the period is 2 x (2^16 - 1), which is approx 2^17.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### blackm0k

#2
##### Mar 20, 2012, 06:49 pm
So, if I understand what you are saying, for the 'normal', 'CTC' and 'Fast PWM' modes, my calculation is correct - there is a maximum period of 2^16 clock ticks, since in these modes the timer only counts up. It is only for the 'phase correct' and 'phase and frequency correct' PWM modes that the period is doubled.

Is this right?

#### MarkT

#3
##### Mar 20, 2012, 10:55 pm
Yes, precisely.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy