Author Topic: Control 4 servos with differenet timers in Atmega16?  (Read 3203 times)

0 Members and 1 Guest are viewing this topic.

Offline Karlis.RasisTopic starter

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
Control 4 servos with differenet timers in Atmega16?
« on: March 22, 2012, 06:24:21 AM »
HI! Question is, can Atmega16 control 4 servos with different timers?
Two servos are continuous rotation and two are normal 180 degrees.
Datasheet says that MCU have Two 8-bit Timer and One 16-bit timer.
180 degrees servos are connected to (OC1B)- PD4 and (OC1A)-PD5.
But where I can connect other two continuous rotation servos? How to activate other timers?
Code: [Select]
void main()
{
   //Configure TIMER1
   TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);        //NON Inverted PWM
   TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)

   ICR1=4999;  //fPWM=50Hz (Period = 20ms Standard).

   DDRD|=(1<<PD4)|(1<<PD5);   //PWM Pins as Out

   while(1)
   {

      OCR1A=316;  //90 degree
      OCR1B=316;  //90 degree

   }
}
If I connect them to PB0 and PB1 and  write this code
Code: [Select]
           PORTB =  0b00000000;
       _delay_ms(0.7);
    PORTB = 0b00000001;
       _delay_ms(0.7);
   PORTB =  0b00000000;
       _delay_ms(0.7);
    PORTB = 0b00000010;

to main() {...} servos rotate incorect, and I guess that I need another timer or way to control them.
Have any Ideas?  ???


« Last Edit: March 25, 2012, 05:29:58 AM by Karlis.Rasis »

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Control 4 servos with differenet timers in Atmega16?
« Reply #1 on: March 22, 2012, 07:59:58 AM »
Atmel has an application note about this with source code. Look at http://www.atmel.com/products/microcontrollers/avr/default.aspx?tab=documents and search for AVR136. The .pdf is the icon on the left, and the little circle icon that looks like a CD is the source code archive.

It works pretty well.

Joe

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Control 4 servos with differenet timers in Atmega16?
« Reply #2 on: March 22, 2012, 07:37:47 PM »
Hi,

HI! Question is, can Atmega16 control 4 servos with different timers?
One timer will do, but please post software questions in "Software"!
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline Karlis.RasisTopic starter

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
Re: Control 4 servos with differenet timers in Atmega16?
« Reply #3 on: March 25, 2012, 05:28:45 AM »
I found simple way how to control 4 servos with 3 different timers on ATmega16!

Code: [Select]
int main(void)

 //Initialize OC0-pin B3, OC2-pin D7, OC1A- pin D5,OC1B- pin D4.
   TCCR0 = (1<<WGM00) | (1<<COM01) | (1<<CS02) | (1<<CS00);//First 8 bit timer
   TCCR2 = (1<<WGM20) | (1<<COM21) | (1<<CS22) | (1<<CS20); //Second 8 bit timer
   TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);  //third 16bit timer
   TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); ////third 16bit timer

   //Setting ports as output
   DDRB|=(1<<PB3);//PWM Pins as Out
   DDRD|=(1<<PD7);//PWM Pins as Out
   DDRD|=(1<<PD5);//PWM Pins as Out
   DDRD|=(1<<PD4);//PWM Pins as Out

OCR0=1;//Set first servo
OCR2=100;//Set second servo
OCR1A = 150;//Set third servo
OCR1B = 80;//Set fourth servo

}