Driving Servo Motors

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

Post Reply
Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Driving Servo Motors

Post by Spanish_dude »

Hi everyone,

I just wanted to know how you are programming your PIC to drive more than one servo motor.

I do something like this:

As you know a pulse sent to the servo motor is max 2ms so I can drive 10 servo's (2ms * 10 = 20ms)

Code: Select all

short number_of_servo = 8; // max 10 

short T = 20000; // 20 ms
short T_on[number_of_servo], T_off = 0, i = 0, PortB = 1;

// initialise T_on
for (i = 0; i < number_of_servo; ++i)
     T_on[i] = 1000; // 1 ms

while (1)
{
     for (i = 0; i < <number_of_servo; ++i)
     {
          short j = 0;

          portb = 0; // pseudocode: all output pins of portb set to 0
          portb = PortB; // pseudocode: uses portb as output with PortB pin high
          
          //The problem of the delay_us function is that it only allows a char variable as 
          //argument so I came up with this 3 lines of code to "bypass" this little problem.
          for (j = 0; j < T_on[i] / 255; ++j)
               delay_us(255);
          delay_us(T_on[i] - (T_on[i] * 255) / 255);

          PortB = PortB * 2; // or PortB << 1, a little shift register to change the output pin
          T_off = T_off + T_on[i];
     }

     portb = 0; //pseudocode: all output pins of portb set to 0.
     PortB = 1; //put the output pin back to the initial pin
     T_off = T - T_off;

     for (i = 0; i < T_off / 255; ++i)
          delay_us(255);
     delay_us(T_off - (T_off / 255) * 255); 
     
}
I think that's it. :)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Driving Servo Motors

Post by Benj »

Hello,

I think we have limited the number of servos to 8 so the maximum duty cycle is around 2.4ms. This allows for full movement to be acheived on all servos. I think you would get away with 10 channels though if you needed all 10.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Driving Servo Motors

Post by Spanish_dude »

Hi

I forgot to mention I'm using flowcode v3

I'm not using the PWM macro because I don't know how to use it xD, but my program is good enough :D

Nicolas

Post Reply