Continuous rotation servo v2.0

Copyright © Parallax Inc. Parallax Continuous Rotation Servo (#900-00008) v2.0 7/9/2009 Page 6 of 6
Propeller Application
The program below uses counter modules to rotate the servo first clockwise at full speed for 2 seconds,
then rests for 2 seconds, and rotates counterclockwise at full speed for another 2 seconds. This code can
also be downloaded from the 900-00008 product page.
ServoRotation.spin
CON
_clkmode = xtal1 + pll16x ' System clock 80 MHz
_xinfreq = 5_000_000
PUB CenterServo | tInc, tc, tCtr, tCw, tCcw, t
ctra[30..26] := %00100 ' Configure Counter A to NCO
ctra[8..0] := 0
frqa := 1
dira[0]~~
tInc := clkfreq/1_000_000 ' 1 μs increment
tC := tInc * 21_500 ' Low pulse
tCtr := tInc * 1500 ' Center pulse = 1.5 ms
tCw := tInc * 1300 ' Clockwise fast = 1.3 ms
tCcw := tInc * 1700 ' Counter-Clockwise fast = 1.7 ms
t := cnt ' Mark counter time
repeat 100 ' Repeat PWM signal 100x
phsa := -tCw ' Set up clockwise fast pulse
t += tC ' Calculate next cycle repeat
waitcnt(t) ' Wait for next cycle (20 ms)
repeat 100 ' Repeat PWM signal 100x
phsa := -tCtr ' Set up the center pulse
t += (tC + 200) ' Calculate next cycle repeat
waitcnt(t) ' Wait for next cycle (20 ms)
repeat 100 ' Repeat PWM signal 100x
phsa := -tCcw ' Set up counter-clockwise fast pulse
t += (tC - 200) ' Calculate next cycle repeat
waitcnt(t) ' Wait for next cycle (20 ms)