Software > Software

please help me cure my servoes


could somone please compile my c program for running 4 servoes and see if your servoes also jidders?
im affraid its my program thats wrong sins i have tried all other things to get them to stand still...
im using an atmega 16 microcontroller
(the program is in norwegian, but i have translated most words...)

my goal is to build somthing like this:

in this program the servoes just goes back and forth... for testing...

--- Quote ---#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>

// ****strukturer****

typedef struct {
   unsigned long teller; // teller = counter in english
   unsigned char flag;
   } timer_type;

timer_type T1, servo1_timer, servo2_timer, servo3_timer, servo4_timer;

// ***Avbruddsrutine***



if(T1.teller !=0){

if (servo1_timer.teller !=0){

   if ((servo1_timer.teller==0))

if (servo2_timer.teller!=0) {

   if ((servo2_timer.teller==0))

if (servo3_timer.teller !=0){

   if ((servo3_timer.teller==0))

if (servo4_timer.teller !=0){

   if ((servo4_timer.teller==0))


// ***Deklarerer tilstander***

static enum {av1,paa1} servo1=av1;
static enum {av2,paa2} servo2=av2;
static enum {av3,paa3} servo3=av3;
static enum {av4,paa4} servo4=av4;
static enum {en,to,tre,fire} tilstand=en; //en,to,tre,fire = one, two, three and four in english

   unsigned char pos1=0;
   unsigned char pos2=0;
   unsigned char pos3=0;
   unsigned char pos4=0;

// ***oppdaterer posisjon***

void oppdater(void) {

   if (T1.flag!=0) {
switch(tilstand) {

case en: // one
   pos2= 30;
   T1.teller = 10000;
case to: // two
   pos2 = 10;
   T1.teller = 10000;
case tre: //three
   pos3 = 10;
   T1.teller = 10000;
case fire: // four
   pos4 = 10;
   T1.teller = 10000;



//***initialiserer timer***

void init_timer1(void) {

//***initialiserer porter***

void init_porter(void) {

   DDRB=0xFF; //OUT
   DDRA=0; //INN

//***oppdaterer servoer*** (servo update)

void servo_oppdater(servopos1,servopos2,servopos3,servopos4) {
         case paa1:
            if(servo1_timer.flag==1) {
            servo1_timer.teller=(400-servopos1); // 20 ms

         case av1:
            if(servo1_timer.flag==1) {

         case paa2:
            if(servo2_timer.flag==1) {

         case av2:
            if(servo2_timer.flag==1) {

         case paa3:
            if(servo3_timer.flag==1) {

         case av3:
            if(servo3_timer.flag==1) {

         case paa4:
            if(servo4_timer.flag==1) {

         case av4:
            if(servo4_timer.flag==1) {

} //end switch


void main(void){
   T1.teller = 10000;
   while(1) {
   } //end while
   } //end main

--- End quote ---

hmmm i cant seem to figure out your code . . .

have you tried my method of doing servos?

when sharing code, its really really good to put comments up the wazoo so others have less trouble figuring it out (and so you can figure out your own code a year after programming it)

as for servo jerking, making sure you do the following things:
servo off -> servo on -> servo off (not the inverse)
make sure about 20ms pass before the next square wave going to the servo
make sure there isnt electrical signal interference causing false PWM

hope this helps . . .

ok, glancing through your code it looks like you are trying to do the right sort of thing.
i *think* the problem might be you are trying to send the pulses too close together.
in the line

--- Quote ---            servo1_timer.teller=(400-servopos1); // 20 ms
--- End quote ---
try increasing 400 to a higher value.

with servos it doesn't matter too much how often you sent the pulse, as long as the pulse is the right length.

so, that's what i think is causing your problem.
just for the record though, here's a list of things that have caused servo jitter on my projects in the past:
1. too short a gap between pulses. (this is what i described above.)
2. too long a gap between pulses. (this tends to cause the servo to make larger random movements rather than the slight vibration you are describing.)
3. unclean power supply. (try using a different power source. use more capacitors. i saw this once on a board powered from a laptop USB port. weird hu?)
4. interupt from different microcontroller peripheral being called during timing pulse. (i don't think this is your problem as you don't have any interupts other than your timer enabled.)
5. USART being enabled on AVR ATMEGA8. i don't know if this is a problem on other AVRs but i have found the timer on the ATMEGA8 doesn't keep time precisely if you switch the USART on. (again, this is not causing your problem. i'm just putting it in here to be complete.)

so the other thing you could try is using someone else's servo controll program to test your hardware.
there is an example servo controll program in the /examples directory of

let us know how you get on,


well, the problem i solved. i had to move the case where i say the gate should go high or low into the interrupt routine. maby it shouldt have been obvious, but im new to programming C, or everything else for that sake...
(please dont correct my writing errors, i know i suck...)
tnx for all the help!

tnx to whoever made society of robots! great site! =)


[0] Message Index

Go to full version