View Full Version : Another round of passing data

01-30-2008, 07:44 PM
Can someone point me to some simple examples in SPIN where two or more COGS are accessing the same set of data ? I see some assembler, but I'd rather not add to my list. The application has a sensor package that is posting an array of longs. Two other COGS are running and use the data (one to make physical changes, one to log to storage). I'm pretty new to this so simple is best.


Martin Hebel
01-30-2008, 07:50 PM
Simply create a global variable and use that same variable in two different methods running in different cogs. Almost too simple for an example :)

As long as you aren't using data sizes bigger than longs, it doesn't take much. Any longer and you'll have to start dealing with methods to synch using locks or what not.


SelmaWare Solutions - StampPlot GUI for controllers, XBee and Propeller Application Boards (http://www.selmaware.com/)[/url]

[u]Southern Illinois University Carbondale, Electronic Systems Technologies (http://www.selmaware.com)

American Technical Educator's Assoc. Conferenceˇ- April, Biloxi, MS. -- PROPELLER WORKSHOP! (http://www.ateaonline.org/)

01-30-2008, 07:57 PM
I had the same needs and here is how i solved it (Thanks to all helpers from this forum!)

Here the routine running on the first cog


long StepTime
long ActPos
long RefPos
byte MotStat
byte SynchControl

byte Pin0

Motion : "Stepper_A2919"

PUB Main

StepTime := 100_000
MotStat := 0
Pin0 := 0


if ina[11..8] == 14
MotStat := 1
elseif ina[11..8] == 12
MotStat := 3
MotStat := 0

and here the routine(s) running on an other:

Bit 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | State: 0 | 1
I I I I I I I I-------> Stop | Run
I I I I I I I-----------> Forward | Backward
I I I I I I---------------> Halfstep | Fullstep not implemented yet
I I I I I-------------------> SPhase | TPhase not implemented yet
I I I I-----------------------> Continous | Controled not implemented yet
I I I---------------------------> NU
I I-------------------------------> NU
I-----------------------------------> NU

Pin0 = P1
Pin0+1 = P2
Pin0+2 = P3
Pin0+3 = P4

P5 = VSS!!! (GND)


long Stack[14] 'Stack space for new cog
byte Cog 'Hold ID of cog in use, if any
byte StepNr 'the actual step number
long MCounter 'counter to hold the waitcycles

Run = 1
Backward = 2
FullStep = 4
TPhase = 8
Synch = 128

PUB Start(Pin0,AdrMotStat, AdrStepTime): Success

Success := (Cog := cognew(ManageMotor(Pin0,AdrMotStat, AdrStepTime), @Stack) + 1)

PUB Stop
{{Stop toggling process, if any.}}

if Cog
cogstop(Cog~ - 1)

PUB ManageMotor(Pin0, AdrMotStat, AdrStepTime)

StepNr~ 'clear the StepNr counter
dira[Pin0+7 .. Pin0]~~ 'sets the needed pins as output

repeat until byte[AdrMotStat] & Run 'wait until motor has to run!

if byte[AdrMotStat] & Backward

--StepNr 'backward
if StepNr == 255
StepNr := 7

++StepNr 'foreward
if StepNr > 7
StepNr := 0

outa[Pin0+3 .. Pin0] := STEPTBL[StepNr] 'set the outputs depending on the StepNr
waitcnt(clkfreq / 1_000_000 * long[AdrStepTime] + cnt) 'Wait for steptime cycles


STEPTBL byte 4, 0, 1, 2, 6, 10, 9, 8 'table for controling the motor (halfsteps)

This is a stepper motor routine that (when finished!?) will run some stepper motors simultaneous controlled by the main program!

Saluti Joerg

01-31-2008, 03:09 AM
Perfect. Thank you both. That will work nicely.