Society of Robots - Robot Forum
Electronics => Electronics => Topic started by: Karlis.Rasis 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?
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 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? ???
-
Atmel has an application note about this with source code. Look at http://www.atmel.com/products/microcontrollers/avr/default.aspx?tab=documents (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
-
Hi,
HI! Question is, can Atmega16 control 4 servos with different timers?
One timer will do, but please post software questions in "Software"!
-
I found simple way how to control 4 servos with 3 different timers on ATmega16!
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
}