Hi,
I am developing a robot arm consisting of 5 servo motors using an ATmega16 and interfacing it via USART. the USART is configured to run at 9600 kbps and the ATmega16 is running at 16 MHz. For controlling 3 servos I am using the timer 1. Here 2 servos are controlled via OCR1A and the 3rd servo via OCR1B. For the remaining 2 servos, I intend on using the timer 0 and timer 2 for generating the PWM.
Here is the test code for the timer 0 PWM output on OC0 pin.
#include#include //prototypes void InitPWM(void); void SetPWMOutput(uint8_t duty); void Wait(void); int main(void) { uint8_t brightness=0; InitPWM(); while(1) { for(brightness = 0; brightness < 255; brightness++) { SetPWMOutput(brightness); Wait(); } /*for(brightness = 255; brightness > 0; brightness--) { SetPWMOutput(brightness); Wait(); }*/ } return 0; } void InitPWM(void) { //Fast PWM mode; Prescaler 1024; Non-inverted PWM TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00)|(1<<CS02); //TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00); //Set OC0 PIN as output. It is PB3 on ATmega16 ATmega32 DDRB|=(1<<PB3); } void SetPWMOutput(uint8_t duty) { OCR0=duty; } void Wait(void) { //_delay_loop_2(8000); _delay_ms(100); }
Here I am seeing a lot of twitch on the servo driven via the OC0 pin. I am using a prescalar of 1024 to give me PWM pulses of roughly 16 ms. I am using fast PWM so the formula used is PWM freq. = CPU clock frequency / (N * 256) where N is set to 1024.
Not able to figure out why there is a twitching of the servo. Need help.
Thanks,
Sumair