Quickstart Demo 5 Multiple Cogs
Hi all
I just downloaded the Quickstart demo from:
http://www.parallaxsemiconductor.com/quickstart5
This is supposed to show multiple cogs doing different stuff - but it does not on my Quickstart board. It flashes 3 LEDs at the rate determined by the last cognew statement. I was expecting to see three LEDs flashing at different rates - but I don't. Have I misunderstood the intention of the program?
Puzzled.
Edit: actually all three Leds flash the same rate but that rate seems to vary with time. Nothing is obviously being done independently.
Regards
Richard
I just downloaded the Quickstart demo from:
http://www.parallaxsemiconductor.com/quickstart5
This is supposed to show multiple cogs doing different stuff - but it does not on my Quickstart board. It flashes 3 LEDs at the rate determined by the last cognew statement. I was expecting to see three LEDs flashing at different rates - but I don't. Have I misunderstood the intention of the program?
{{
QuickStart 5: MulticogTwinkleDemo.spin
www.parallaxsemiconductor.com
}}
CON
_clkmode = xtal1 + pll16x 'Establish speed
_xinfreq = 5_000_000 '80Mhz
OBJ
led: "E555_LEDEngine.spin" 'Include LED methods object
VAR
byte Counter 'Establish Counter Variable
long stack[90] 'Establish working space
PUB Main
cognew(Twinkle(16,clkfreq/50), @stack[0]) 'start Twinkle cog 1
cognew(Twinkle(19,clkfreq/150), @stack[30]) 'start Twinkle cog 2
cognew(Twinkle(22,clkfreq/100), @stack[60]) 'start Twinkle cog 3
PUB Twinkle(PIN,RATE) 'Method declaration
repeat 'Initiate a master loop
repeat Counter from 0 to 100 'Repeat loop Counter
led.LEDBrightness(Counter, PIN) 'Adjust LED brightness
waitcnt(RATE + cnt) 'Wait a moment
repeat Counter from 100 to 0 'Repeat loop Counter
led.LEDBrightness(Counter,PIN) 'Adjust LED brightness
waitcnt(RATE + cnt) 'Wait a moment
Puzzled.
Edit: actually all three Leds flash the same rate but that rate seems to vary with time. Nothing is obviously being done independently.
Regards
Richard
Comments
As it is now, the variable Counter is being changed by three different cogs. Not a good way to do this (IMO).
Here's my suggestion.
Comment out the global "Counter" variable.
VAR byte Counter 'Establish Counter Variable
And make it local.
PUB Twinkle(PIN,RATE) | counter 'Method declaration
It should then behave as expected.
With these changes, the each cog uses its own variable "counter" so the other cogs aren't affecting it like the original program.
(I think.)
I fixed the demo code to be multi-cog so nobody shares any variables in the object or in the main program.
{{ QuickStart 5: MulticogTwinkleDemo.spin www.parallaxsemiconductor.com }} CON _clkmode = xtal1 + pll16x 'Establish speed _xinfreq = 5_000_000 '80Mhz OBJ led[3]: "E555_LEDEngine.spin" 'Include LED methods object VAR long stack[90] 'Establish working space PUB Main cognew(Twinkle(16,clkfreq/50,0), @stack[0]) 'start Twinkle cog 1 cognew(Twinkle(19,clkfreq/150,1), @stack[30]) 'start Twinkle cog 2 cognew(Twinkle(22,clkfreq/100,2), @stack[60]) 'start Twinkle cog 3 PUB Twinkle(PIN,RATE,INSTANCE)| counter 'Method declaration repeat 'Initiate a master loop repeat Counter from 0 to 100 'Repeat loop Counter led[INSTANCE].LEDBrightness(Counter, PIN) 'Adjust LED brightness waitcnt(RATE + cnt) 'Wait a moment repeat Counter from 100 to 0 'Repeat loop Counter led[INSTANCE].LEDBrightness(Counter,PIN) 'Adjust LED brightness waitcnt(RATE + cnt) 'Wait a moment
Now it's a happy and productive member of the Demo world!
That's much better thanks. It now looks like the LEDs are being independently controlled (which is what was presumably intended). That's a change Parallax might do well to consider!
Rick - thanks too. I like demos that demonstrate what they suggest they will!
Regards
Richard
I haven't tried running this program, but I don't see a need to include multiple instances when run from different cogs. It makes use of the cogs counters which shouldn't interfere with one another (as long as they're be run from different cogs).
I just updated it to use all 8 COGs, so I'lltry it with just one object instance......stay tuned.
I guess you're right. It appears to work with the localized counter variable and a single object instance. The object itself doesn't have any variable space and everything it does is with the passed variable (on the stack?). I need to learn my SPIN better!!
Here's and 8 COG version with a single object instance.....it's running on my desk now and is very relaxing!
{{ QuickStart 5: MulticogTwinkleDemo.spin Eight COGs, Eight LEDs, Eight different Twinkle Rates www.parallaxsemiconductor.com Demo is now multi-thread friendly. Counter variable was moved to a local variable belonging to the Twinkle routine It's fun to watch if you need to space out for a while. Modified by Rick Post /Duane Degn - 2/9/2012 }} CON _clkmode = xtal1 + pll16x 'Establish speed _xinfreq = 5_000_000 '80Mhz OBJ led: "E555_LEDEngine.spin" 'Include LED methods object VAR long stack[210] 'Establish working space PUB Main cognew(Twinkle(16,clkfreq/25), @stack[0]) 'start Twinkle cog 1 cognew(Twinkle(17,clkfreq/150), @stack[30]) 'start Twinkle cog 2 cognew(Twinkle(18,clkfreq/225), @stack[60]) 'start Twinkle cog 3 cognew(Twinkle(19,clkfreq/50), @stack[90]) 'start Twinkle cog 4 cognew(Twinkle(20,clkfreq/200), @stack[120]) 'start Twinkle cog 5 cognew(Twinkle(21,clkfreq/100), @stack[150]) 'start Twinkle cog 6 cognew(Twinkle(22,clkfreq/10), @stack[180]) 'start Twinkle cog 7 Twinkle(23,clkfreq/75) PUB Twinkle(PIN,RATE)| counter 'Method declaration repeat 'Initiate a master loop repeat Counter from 0 to 100 'Repeat loop Counter led.LEDBrightness(Counter, PIN) 'Adjust LED brightness waitcnt(RATE + cnt) 'Wait a moment repeat Counter from 100 to 0 'Repeat loop Counter led.LEDBrightness(Counter,PIN) 'Adjust LED brightness waitcnt(RATE + cnt) 'Wait a moment
Stack is way to big - the object says 11 longs, but for a demo, we have plenty of memory.
Just done that.
Cheers