cognew questions
Eric R
Posts: 225
From the book:
{{Output.spin
Toggles two pins, simultaneously.}}
VAR
· long Stack[noparse][[/noparse]9]················ 'Stack space for new cog
Pub Main
· cognew(Toggle(16, 3_000_000, 10), @Stack)············· 'Toggle P16 ten times, 1/4 s each
· Toggle(17, 2_000_000, 20)···························· 'Toggle P17 twenty times, 1/6 s each
···
PUB Toggle(Pin, Delay, Count)
{{Toggle Pin, Count times with delay clock cycles in between.}}
· dira[noparse][[/noparse]Pin]~~·················· 'Set I/O pin to output direction
· repeat Count················· 'Repeat for Count iterations
··· !outa[noparse][[/noparse]Pin]················· '· Toggle I/O pin
··· waitcnt(Delay + cnt)······· '· Wait for delay cycles
As you can see, I am not far into the book but I have a question. Pub Main starts and fires a new cog then continues to run the code on cog 0. It makes sense that cognew starts cog one but in a larger code block, how will one know which cog is being started if you start multiple cogs. Can you specify a cog to start or do you just count cogs in your code? Does it really matter?
If you have 8 cogs running and the code ends on cog 3, can you start a new cog?
{{Output.spin
Toggles two pins, simultaneously.}}
VAR
· long Stack[noparse][[/noparse]9]················ 'Stack space for new cog
Pub Main
· cognew(Toggle(16, 3_000_000, 10), @Stack)············· 'Toggle P16 ten times, 1/4 s each
· Toggle(17, 2_000_000, 20)···························· 'Toggle P17 twenty times, 1/6 s each
···
PUB Toggle(Pin, Delay, Count)
{{Toggle Pin, Count times with delay clock cycles in between.}}
· dira[noparse][[/noparse]Pin]~~·················· 'Set I/O pin to output direction
· repeat Count················· 'Repeat for Count iterations
··· !outa[noparse][[/noparse]Pin]················· '· Toggle I/O pin
··· waitcnt(Delay + cnt)······· '· Wait for delay cycles
As you can see, I am not far into the book but I have a question. Pub Main starts and fires a new cog then continues to run the code on cog 0. It makes sense that cognew starts cog one but in a larger code block, how will one know which cog is being started if you start multiple cogs. Can you specify a cog to start or do you just count cogs in your code? Does it really matter?
If you have 8 cogs running and the code ends on cog 3, can you start a new cog?
Comments
In assembly language, COGNEW and COGINIT are really the same instruction with an option bit specifying the difference in behavior.
Post Edited (Mike Green) : 9/29/2006 2:47:53 AM GMT
I guess I was reading more into it than needed as the next section talked about my question. I am sure that by the time I am finished with the book I will understand but for now I am entering code, testing but not understanding it all. Take this code for example:
{{Output.spin}}
VAR
· long Stack[noparse][[/noparse]9]················ 'Stack space for new cog
· byte Cog····················· 'Hold ID of cog in use if any
··
PUB Start(Pin, Delay, Count): Success
{{Start new toggling process in a new cog; Return true if sucessful}}
· Stop
· Success := (cog := cognew(Toggle(Pin, Delay, Count), @Stack) + 1)
PUB Stop
{{Stop toggling process, if any.}}
· if Cog
··· cogstop(cog~ - 1)
Pub Active: YesNo
{{Return TRUE if process is active, FALSE otherwise.}}
· YesNo := Cog > 0
···
PUB Toggle(Pin, Delay, Count)
{{Toggle Pin, Count times with delay clock cycles in between.
· If count = 0, toggle Pin forever.}}
· dira[noparse][[/noparse]Pin]~~·················· 'Set I/O pin to output direction
· repeat······················· 'Repeat the following
··· !outa[noparse][[/noparse]Pin]················· '· Toggle I/O pin
··· waitcnt(Delay + cnt)······· '· Wait for delay cycles
· while count := --Count #> -1· 'While not 0 (make min -1)
· Cog~···
···
Where do I find the cog ID from the byte or if the start was a success? Yes, I am getting tired and maybe re reading this section later will help. For now,·it looks like it was a success as the program is running.