PWM with multiple servos

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times

PWM with multiple servos

Post by Ron »

Hi,

I want to have several servos (appear) to move at the same time. I am told it is possible to do this.

Can someone please let me know how this works and will the PWM macro support this?

I think I need to use INT and PWM together if I understand what I have read.

Thanks for all the help,

Ron

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Post by Steve »

Hello Ron,

I'm not sure if the PWM module of a PIC will be suitable for servo motors. I could be wrong here, but I think a servo needs a period of 20ms. The PWM (with a clock speed of 4MHz) can operate no slower than around 4ms.

The way to do this would be to use a timer interrupt that interrupts as a convenient period (e.g. 4800 Hz) and set the output of your pins appropriately.

Use a counter variable that increases each time the interrupt is called. Once this counter has reached 96, you know 20ms has elapsed and you should reset the duty cycle (4800Hz * 0.02s = 96). You should also keep a variable for each servo that dictates when each servo output changes from 1 to 0.

Here's a quick example for 2 servos using an interrupt of 4800 Hz. The code be in the macro which is called by the timer interrupt:

Code: Select all

Cnt = Cnt + 1
if (Cnt >= 96)
  Cnt = 0
endif

if (Cnt < MyDuty1)
  Output 1 -> B0
else
  Output 0 -> B0
endif

if (Cnt < MyDuty2)
  Output 1 -> B1
else
  Output 0 -> B1
endif
The servo outputs are on B0 and B1 and the MyDuty1 and MyDuty2 variables should be set between 0 and 95 to set the servo duty cycle.

I hope this helps.

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times

Post by Ron »

Hi,

This is a great start...

Thanks,

Ron

Post Reply