Take the 2-minute tour ×

This should be a simple formula but I can not figure it out.

I have a 16mHZ AVR Chip.

I need to run a PWM signal to be 24kHZ..

what is the formula to decide what is the best Prescaler.

I am using a 16bit timer.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Read the datasheet (here for example from ATmega328P datasheet, 16-bit Timer/Counter1 ) to get the formula:


fPWM = fclk_IO / (N * (1 + TOP))

The N variable represents the prescaler divider (1, 8, 64, 256, or 1024).

share|improve this answer
Im just trying to work out how to get 24KHZ from this formula... My algebra is a little rusty being 10years out of school –  michael Feb 16 '12 at 23:09
@michael simple math: use a fixed N and compute (fclk_IO / (N * fPWM)) - 1 = TOP. The TOP value rounded to the nearest integer is the setting you are looking for. Using the rounded TOP integer you can know your accurate PWM frequency by using the formula in my answer. If you try with different N values you'll observe the higher prescaler value you use the more accurate is your frequency. –  ouah Feb 16 '12 at 23:34
so it would be 16000000 / (8 / 2400000) - 1 = TOP for a 16MHZ chip trying to get a 24KHZ frequency? –  michael Feb 17 '12 at 0:14
I meant the lower prescaler value you use, the higher the accuracy is –  ouah Feb 17 '12 at 0:16
@michael You probably meant 16 000 000 / (8 * 24 000) - 1 = TOP. –  Lundin Feb 17 '12 at 14:36
add comment

16MHz / 24KHz gives you...

(16 * 1024 * 1024) / (24 * 1024) gives you...

16777216 / 24576 gives you...


Because your prescaler can only be a whole number, and depending on how precise you need it to be, you could optionally alter the prescaler value in software on every third tick. For example, every first two ticks would have a prescaler of 683 with every third tick being 682.

share|improve this answer
Chances are that since we're talking about CPU clock rates, the whole "powers of 2" business doesn't apply here (if this were a 32k crystal, that might be a different story, however). The clock is probably 16 * 1000 * 1000 Hz, nominally. That also changes the 682.xyz to, yes, 666.666... –  Dan Feb 17 '12 at 6:43
Unless otherwise specified, crystal frequencies are always a power of two. –  Jim Fell Feb 20 '12 at 22:13
add comment

Your Answer


