pinMode(11, OUTPUT); TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20); TCCR2B = TCCR2B & 0b11111000 | 0x07; OCR2A = 180; OCR2B = 50;
pinMode(3, OUTPUT); pinMode(11, OUTPUT); TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20); TCCR2B = _BV(WGM22) | _BV(CS22)| _BV(CS21)| _BV(CS20); // OCR2A = 156; OCR2B = 4;
You want to pay special attention to this section of the :Varying the timer top limit: fast PWM on this pagehttp://arduino.cc/en/Tutorial/SecretsOfArduinoPWMThis is probably the most confusing configuration you'll run into. It's not very intuitive because it looks like you will be toggling OC2A (Pin 11), but you really toggle OC2B (Pin 3). This is because you hijack the OCR2A register to set the TOP limit so it can't possibly set a duty cycle now. The duty cycle is set by OCR2B and consequently it toggles OC2B (Pin 3). This is all necessary because you want to use a "non standard" PWM frequency.Code: [Select] pinMode(3, OUTPUT); pinMode(11, OUTPUT); TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20); TCCR2B = _BV(WGM22) | _BV(CS22)| _BV(CS21)| _BV(CS20); // OCR2A = 156; OCR2B = 4;This should give you a 50.1 Hz update rate with a .512mS pulse width on Pin 3. I haven't tested it, but I think it's right.EDIT: I tested it on the scope and it works. Pin 11 has a 25Hz 50% duty cycle waveform on it.
I glanced (literally five minutes) at the servo library code and it looks like it uses Timer1 initially. This is a 16 bit counter. It puts it in ordinary count mode and enables overflow interrupts. With the prescaler of 8, it should overflow roughly 30.5 times per second. This would be the update rate to the servo, not 50Hz
Thanks alot for all the help. I will look in the servo library and try to use timer1.
Please enter a valid email to subscribe
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!
via Egeo 16