Programming a servo in SPIN
Hello all. I am very new to programming and seem to hit a wall every time I try and write a program. I'm playing around with a servo, and trying to get it to stop at 0 and 180 degrees. I used the example programs in the propeller tool, and was able to get it right. I don't really understand everything that the object files were doing, so I'm trying to recreate using my own code. Maybe my problems stem from not fully understanding the pulse widths, but my own code runs smooth to 0 degrees and then begins to step to 180.
I'm trying to gain an understanding of using multiple cogs, so that is why I implemented the PUB LED. But after adding this my program does nothing. The aim of the code was to have the servo to and fro the extremes, and light the led when it gets there. I actually got the idea for the program from one of the exercises in the propeller education kit.
I've been bouncing around with this for a bit, so any direction and insight is most appreciated.
{{Servomotor driver}}
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
long stack[30]
long count 'variable to establish start angle
long count1 'variable to establish end angle
PUB Main
cognew(Moveservo(5), @stack[0]) 'cog to move the servo
cognew(LED, @stack[20]) 'cog to flash the led
PUB Moveservo (pin)
dira[5] := 1 'pin 5 set to ouput
repeat 'repeat the cycle
count1 := (18 * 1111 + 50000)/1000 'set the variable to 18 degrees
outa[pin]~~ 'set pin to high
waitcnt(clkfreq/100000*count1 + cnt) 'use count 1 and create pulse for 18 degrees
outa[pin]~ 'set pin to low
waitcnt(clkfreq/200*2 + cnt) 'wait 20 ms
count := (162 * 1111 + 50000)/1000 'set the count variable to 162 degrees
outa[pin]~~ 'set pin high
waitcnt(clkfreq/100000*count + cnt) 'use count and create pulse for 162 degrees
outa[pin]~ 'pin low
waitcnt(clkfreq/200*2 + cnt) 'wait 20 ms
PUB LED
dira[0] := 1 'pin 0 set to output
repeat 'repeat cycle
if outa[5]~ 'if pin 5 low
outa[0] := 1 ' led on
else
outa[0] := 0 'else led off
I'm trying to gain an understanding of using multiple cogs, so that is why I implemented the PUB LED. But after adding this my program does nothing. The aim of the code was to have the servo to and fro the extremes, and light the led when it gets there. I actually got the idea for the program from one of the exercises in the propeller education kit.
I've been bouncing around with this for a bit, so any direction and insight is most appreciated.

Comments
Here's one example of code that would move the motor from one position to another with a pause after each movement.
con totalRotationDegrees = 180 ' limit to limit movement in degrees usMinPulseWidth = 500 ' minimum pulse width in us at one end of rotation usMaxPulseWidth = 2500 ' maximum pulse width in us at other end of rotation lowLimit = 18 * (usMaxPulseWidth-usMinPulseWidth) / totalRotationDegrees + usMinPulseWidth highLimit = 162 * (usMaxPulseWidth-usMinPulseWidth) / totalRotationDegrees + usMinPulseWidth pulses = 100 ' 20 ms cycles var long stackMove[20] pub main coginit(moveServo(5,lowLimit,highLimit,pauseTime),@stackMove) ' this program could continue on here with something else pub waitms(ms) waitcnt((clkfreq/1000)*ms + cnt) pub servoPulse(pin, usTime) ' Generate a positive-going pulse on I/O pin whose width is given in us outa[pin]~~ waitcnt((clkfreq/1000000)*usTime + cnt) outa[pin]~ pub moveServo(pin,fromWidth,toWidth,pulseCnt) dira[pin]~~ ' Initialize I/O pin outa[pin]~ repeat repeat pulseCnt servoPulse(pin,fromWidth,toWidth) waitms(20) repeat pulseCnt servoPulse(pin,toWidth,fromWidth) waitms(20)I because I like to show off, whenever I see mention of using a Propeller to control a servo, I feel the need to remind people of my 32 servo demo. I had seen it mentioned many times that the Propeller could drive up to 32 servos but I hadn't see it done (probably because it's kind of a lot of work without a real useful purpose (other than to show off the fact I have 32 servos)).
It's very possible to add a LED to the servo code. There are many ways to do it. If you want to use another cog, you need to keep a few rules in mind. One rule is don't set pin states from more than one cog. Another is to make sure you have enough stack space.
The easiest way to see what went wrong is to post the code.
BTW, are you using a Propeller board or a DIP chip on a breadboard? If you are using a board, which one?
Note: this is great for very simple control of one or two servos. When you want to control multiple servos and add features like ramping, it's best to use a servo object -- many of us have written them so you have a lot of good choices.