Can't get the coginit working!!
Uncle George
Posts: 14
Having trouble to start different cog to do the function. Each method works fine by itself when call by the main routine. When do a "coginit(2, Brinking, @stack)", nothing is happening. Anyone interest in helping, I can send the entire source code.
Comments
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
InputPin_Left = 17
InputPin_Right = 18
LED1 = 1
LED2 = 2
LED3 = 3
LED4 = 4
LED5 = 5
LED6 = 6
LED7 = 7
LED8 = 8
VAR
Byte LEDOn ' No. of LED on
Byte LEDOn1 ' No. of LED on for brinking default
Long StackDefault[32] ' Stack workspace for cog 1
Long StackLeft[32] ' Stack workspace for cog 2
Long StackRight[32] ' Stack workspace for cog 3
byte Brinking_Type ' Current brinking type
byte Current_Input ' Current input
PUB Main
dira[LED1..LED8]~~ 'Set P1 thru P8 as outputs
dira[InputPin_Left..InputPin_Right]~ 'Set P17 & P18 as inputs
Brinking_Type := 0 'set switches to different values
repeat
if ina[InputPin_Left] == 1 'if P17 high
Current_Input := 2 'change the input switch to brink left
elseif ina[InputPin_Right] == 1 'if P18 high
Current_Input := 3 'change the input switch to brink right
else '
Current_Input := 1 'otherwise, change input switch to brink default
if Current_Input == Brinking_Type 'repeat same brinking fct if no change in input
else
Brinking_Type := Current_Input 'set the brink fct switch to same as input switch
Initialization 'stop previous brinking fct by stopping the cog
case Brinking_Type
1 : coginit(1, Brinking, @StackDefault) 'turn on cog1 for default brinking
2 : coginit(2, BrinkingLeft, @StackLeft) 'turn on cog2 for brinking left
3 : coginit(3, BrinkingRight, @StackRight) 'turb on cog3 for brinking right
Pri Initialization
outa[LED1..LED8]~
cogstop(1)
cogstop(2)
cogstop(3)
Pri Brinking
outa[LED1..LED8]~ 'turn off all LEDs
repeat 'set starting brink left LED
LEDOn := 5 'set starting brink right LED
LEDOn1 := 4 'repeat 4 times
repeat 4
LEDOn -=1
LEDOn1 +=1 'turn on LEDs
outa[LEDOn]~~
outa[LEDOn1]~~
waitcnt(clkfreq / 6 + cnt) 'wait for 1/6 of a second before turn on next LEDs
outa[LED1..LED8]~ 'turn off all the LEDs
waitcnt(clkfreq / 2 + cnt) 'wait 1/2 second, repeat brinking
Pri BrinkingLeft
outa[LED1..LED8]~
Repeat
LEDOn := 0
repeat 8
LEDOn += 1
outa[LEDOn]~~
waitcnt(clkfreq / 6 + cnt)
outa[LED1..LED8]~
waitcnt(clkfreq / 2 + cnt)
Pri BrinkingRight
outa[LED1..LED8]~
Repeat
LEDOn := 9
Repeat 8
LEDOn -=1
outa[LEDOn]~~
waitcnt(clkfreq / 6 + cnt)
outa[LED1..LED8]~
waitcnt(clkfreq / 2 + cnt)
Appreciate helping
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
InputPin_Left = 17
InputPin_Right = 18
LED1 = 1
LED2 = 2
LED3 = 3
LED4 = 4
LED5 = 5
LED6 = 6
LED7 = 7
LED8 = 8
VAR
Byte LEDOn
Byte LEDOn1
Long StackDefault[32]
Long StackLeft[32]
Long StackRight[32]
byte Brinking_Type
byte Current_Input
PUB Main
dira[LED1..LED8]~~
dira[InputPin_Left..InputPin_Right]~
Brinking_Type := 0
repeat
if ina[InputPin_Left] == 1
Current_Input := 2
elseif ina[InputPin_Right] == 1
Current_Input := 3
else
Current_Input := 1
if Current_Input == Brinking_Type
else
Brinking_Type := Current_Input
Initialization
case Brinking_Type
1 : coginit(1, Brinking, @StackDefault)
2 : coginit(2, BrinkingLeft, @StackLeft)
3 : coginit(3, BrinkingRight, @StackRight)
Pri Initialization
outa[LED1..LED8]~
cogstop(1)
cogstop(2)
cogstop(3)
Pri Brinking
outa[LED1..LED8]~
repeat
LEDOn := 5
LEDOn1 := 4
repeat 4
LEDOn -=1
LEDOn1 +=1
outa[LEDOn]~~
outa[LEDOn1]~~
waitcnt(clkfreq / 6 + cnt)
outa[LED1..LED8]~
waitcnt(clkfreq / 2 + cnt)
Pri BrinkingLeft
outa[LED1..LED8]~
Repeat
LEDOn := 0
repeat 8
LEDOn += 1
outa[LEDOn]~~
waitcnt(clkfreq / 6 + cnt)
outa[LED1..LED8]~
waitcnt(clkfreq / 2 + cnt)
Pri BrinkingRight
outa[LED1..LED8]~
Repeat
LEDOn := 9
Repeat 8
LEDOn -=1
outa[LEDOn]~~
waitcnt(clkfreq / 6 + cnt)
outa[LED1..LED8]~
waitcnt(clkfreq / 2 + cnt)
PUB Main
dira[LED1..LED8]~~
dira[InputPin_Left..InputPin_Right]~
Brinking_Type := 0
repeat
if ina[InputPin_Left] == 1
Current_Input := 2
elseif ina[InputPin_Right] == 1
Current_Input := 3
else
Current_Input := 1
if Current_Input == Brinking_Type
else
Brinking_Type := Current_Input
Initialization
case Brinking_Type
1 : coginit(1, Brinking, @StackDefault)
2 : coginit(2, BrinkingLeft, @StackLeft)
3 : coginit(3, BrinkingRight, @StackRight)
Pri Initialization
outa[LED1..LED8]~
cogstop(1)
cogstop(2)
cogstop(3)
That's the Main routine with proper indentation.
[ C O D E ]
some code here
[ / C O D E]
and leave all the spaces out of the code tags I provided as demo.
Enjoy!
Mike
I hope I did it correctly.
you set
dira[LED1..LED8]~~ 'Set P1 thru P8 as outputs
in your main cog.
I think this is wrong.
outa[LED1..LED8]~ in Initialization is not needed, but does not harm either.
but the main problem is that all the lines after else need to be indented
NOT
BUT
Enjoy!
Mike
if you use coginit YOU are responsible for using the right cognumbers. If you use COGNEW the spin-interpreter will do that for you.
As long as a cog is available the CIGNEW-Command will start a cog and you NEVER have to worry about which cog-number to start or to stop.
to stop a cog you simply call the cogstop-command by calling the cogstop-command from the method that was used in the cognew-command
without worrying which cognumbers are used.
best regards
Stefan