## Problems with PWM

4 posts / 0 new
Author
Message

Hi

I want to control a servo using the mega8 and pwm.
The problem is the pwm frequency is the double it should be.
The mega 8 has a 16Mhz quartz.
The pwmfreq I calculated like in the datasheet:
Pwmfreq = 16000000 / (prescalcer* (top+1))

prescaler is 256
top is 10bit equals 1023
This should leed to 61 hz
What I get is 122hz

Here is the relevant part of the code:

void init_servo ( void )
{
TCCR1A = (1<<WGM11)|(1<<COM1A1) |(1<<COM1B1); //9BIT FAST PWM for 1A and 1B
TCCR1B = (1<<CS12) ; //prescaler CK/256

DDRB |= ( 1<<PB1 ); //Servo left
DDRB |= ( 1<<PB2 ); //Servo right
}

What have I done wrong?

Cheers
Rubi (cluless)

TOP is not 10 bit but 9 bit means 511.
The result is 122 Hz.

______________
Armin

Sorry

Shame on me, I pasted the wrong code,...
This was the code with the 122Hz,
interestingly then I tried the code above, phase correct 9Bit pwm, but the frequency was the same.

void init_servo ( void )
{
TCCR1A = (1<<WGM12)|(1<<WGM11)|(1<<WGM10)|(1<<COM1A1) |(1<<COM1B1); //10BIT FAST PWM for 1A and 1B
TCCR1B = (1<<CS12) ; //prescaler CK/256

DDRB |= ( 1<<PB1 ); //Servo left
DDRB |= ( 1<<PB2 ); //Servo right
}

Cheers
Rubi

Your first code makes a phase correct pwm with 9 bit:

frequency = fsys/(2 * N * TOP) = 16 Mhz/(2*256*511) = 61.03 Hz

Your second code makes a fast pwm with 10 bit

frequency = fsys/(N * (1 + TOP)) = 16 Mhz/(256*1023) = 61.09 Hz

The results should almost be the same in both cases, but half
to the value you've measured. I'm a little bit confused...
What are your values of OCR1A and OCR1B while measuring ?

________________
Armin