Atmega 32 PWM

Jony130

Joined Feb 17, 2009
5,600
We both did a mistake.
First --- > our counter can count only up to 65536.
And I use wrong Prescaler value, but in final i use correct one.
And this is why I get 31250

OK let me try once again.
You want to generate 0.5Hz signal at OC1A output. To do so we will use 16 bit timer/counter build in into atmega32 work in CTC mode.
Atmega is running at FCPU = 8MHz clock speed and ouer counter can divide this frequency by 2^16 = 65536. So the lowers frequency we can get at OC1A outputs is (8MHz/65536)/2 = 122.070312Hz/2 = 61.035156Hz. As you can see this is not enough. And this is why we are forced to use "additional" CPU frequency divider. We call this additional divider a Prescaler. I decided to set Prescaler at 64. So now our clock speed is no longer 8Hhz. Our new clock speed is Fclk = 8Mhz/64 = 125Khz. So our timer1 again will divide this frequency by 2^16 = 65536. Fout = (125Khz/65536)/2 = 1.90734863Hz/2 = 0.953674315Hz
but we want Fout = 0.5hz.
What we can do now? Well again we need increase our Prescaler.
Let as assume we set Prescaler = 256.
Fclk = 8Mhz/256 = 31.25Khz and our timer need to count

\(\Large Cout = \frac{1s}{\frac{1}{31.25Khz}} = \frac{1s}{32\mu} = 31250 \)

Or

\(\Large Cout = \frac{Fclk}{2 * Fout} = \frac{31.25KHz}{2 * 0.5Hz} = \frac{31.25KHz}{1Hz} = 31250 \)
 
Top