Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: nick on June 22, 2008, 02:28:54 AM

Title: A math problem in the USB-SERVO2 project
Post by: nick on June 22, 2008, 02:28:54 AM
Hi I am planning to implement the USB-SERVO2 (http://www.societyofrobots.com/member_tutorials/node/25 (http://www.societyofrobots.com/member_tutorials/node/25)),
but I found a little problem... do the math here:

1. the ATmega8 uses a 12MHz external oscillator;
2. and the Timer1 for PWM generation sets prescaler at 256 (from source code);
3. the PWM should be at 50Hz;
4. Then the ICR1 (aka topcount) is 486.75 = 12MHz/(2*256*50) -> Decimal?!
what goes wrong??

Title: Re: A math problem in the USB-SERVO2 project
Post by: bens on June 22, 2008, 03:12:45 AM
I don't understand what problem you're having.  Are you surprised that you can't acheive exactly 50 Hz when using a timer that's clocked at 46875 Hz?

If you want an exact 50 Hz signal, use Timer1 in phase-correct PWM mode with a prescaler of 8 and a top count of 15000 (or maybe 14999?).

- Ben
Title: Re: A math problem in the USB-SERVO2 project
Post by: nick on June 22, 2008, 06:47:13 AM
Thanks Ben.
I am just wondering why it uses 12MHz and 256 prescaler to generate a not-so-accurate top count.
AVR-USB runs fine with 12,16..MHz.
Title: Re: A math problem in the USB-SERVO2 project
Post by: dunk on June 22, 2008, 08:32:41 AM
Quote
AVR-USB runs fine with 12,16..MHz.
hobby servos don't need to be at exactly 50Hz either. the actual pulse duration has far more effect than the exact frequency.

dunk.
Title: Re: A math problem in the USB-SERVO2 project
Post by: nick on June 22, 2008, 09:44:27 AM
Thanks dunk.