Shop OBEX P1 Docs P2 Docs Learn Events
Cogstop — Parallax Forums

Cogstop

MD2010MD2010 Posts: 11
edited 2011-02-17 12:26 in Propeller 1
Hi,
I wrote two programs that launch a Blink method. One works as expected and the other one is not. In both programs I have a loop that is waiting for two conditions, if condition one is meet LED (x) will turn on. If the second condition is met a cog[2] will launch a Blink method. If condition one is met again cog[2] should stop and LED(X) will turn on again. This works in program “A” but not in program “B.” I was wondering if anyone could help me with that?

program “A”

PUB LedOn | Hr , GUTS, s1, s2, count, cog[2] ' Method declaration
Debug.Start(31, 30, 0, 115200)

Debug.tx(CLS)
dira[10] := 1
dira[11] := 1
dira[1] := 1
dira[5] := 1

dira[16] := 0
dira[17] := 0
repeat


menu := debug.getstr(menu)
Debug.Str(String("wating for String:"))
Debug.tx(Debug#CR)

if STRCOMP (menu, @Str1) 'B1 "Red LED will flash"
cogstop(2)
cogstop(3)
outa[11]~~
outa[1]~~

elseif STRCOMP (menu, @Str3) 'B2 "Green LED will flash"

cog[2] := cognew(Blink(10), @stack[30])
cog[3] := cognew(Blink(5), @stack[50])
outa[11]~
outa[1]~


program “B”
PUB LedOnOff | counter, cog[2]

dira[10] := 1
dira[11] := 1
dira[1] := 1
dira[5] := 1

dira[16] := 0
dira[17] := 0
counter := 0
repeat
waitcnt(clkfreq/2 + cnt)
if ina[16]

cogstop(2)
cogstop(3)
outa[11]~~
outa[1]~~

elseif ina[17]
waitcnt(clkfreq*5 + cnt)
cog[2] := cognew(Blink(10), @stack[30])
cog[3] := cognew(Blink(5), @stack[50])
outa[11]~
outa[1]~

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-02-16 09:35
    MD2010,

    It would help us see you code properly if you used the code blocks. You can find them as the # icon if you "Go Advanced" or you can use:

    [C0DE] your code goes here and end with [\C0DE]

    I used a zero instead of "O" so you can see them. Make sure and use an oh not a zero.

    Welcome to the forum.

    Duane
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-16 09:51
    You define an array on the stack called cog[2]. It's elements are cog[0] and cog[1]. When you do a cognew you are saving the returned value in cog[2] and cog[3], which is scribbling on the stack. You should be using cog[0] and cog[1]. Also, your cog stops are hardcoded as cogstop(2) and cogstop(3). You should use cogstop(cog[0]) and cogstop(cog[1]).
  • MD2010MD2010 Posts: 11
    edited 2011-02-16 16:09
    Please see programs below. By the way I already tried using cogstop(cog[2]) and cogstop(cog[3]) , that didn't help. When using cog[0] := cognew(Blink(10), @stack[30]) and cog[1] := cognew(Blink(10), @stack[30]), LED will not blink in program B, obviously using cogstop(cog[0]) and cogstop(cog[1]) to stop cogs.


    Program A.
    CON

    _clkmode = xtal1 + pll16x ' use crystal x 16
    _xinfreq = 5_000_000

    ''Parallax Serial Terminal Control Character Constants
    ''────────────────────────────────────────────────────
    HOME = 1 ''HOME = 1
    CRSRXY = 2 ''CRSRXY = 2
    CRSRLF = 3 ''CRSRLF = 3
    CRSRRT = 4 ''CRSRRT = 4
    CRSRUP = 5 ''CRSRUP = 5
    CRSRDN = 6 ''CRSRDN = 6
    BELL = 7 ''BELL = 7
    BKSP = 8 ''BKSP = 8
    TAB = 9 ''TAB = 9
    LF = 10 ''LF = 10
    CLREOL = 11 ''CLREOL = 11
    CLRDN = 12 ''CLRDN = 12
    CR = 13 ''CR = 13
    CRSRX = 14 ''CRSRX = 14
    CRSRY = 15 ''CRSRY = 15
    CLS = 16 ''CLS = 16
    term = $0D
    VAR

    byte menu
    long stack[60]

    OBJ

    debug : "FullDuplexSerialPlus"
    delay : "timing"

    PUB LedOn | Hr , GUTS, s1, s2, count, cog[2] ' Method declaration
    Debug.Start(31, 30, 0, 115200)

    Debug.tx(CLS)
    dira[10] := 1
    dira[11] := 1
    dira[1] := 1
    dira[5] := 1

    dira[16] := 0
    dira[17] := 0
    repeat


    menu := debug.getstr(menu)
    Debug.Str(String("wating for String:"))
    Debug.tx(Debug#CR)

    if STRCOMP (menu, @Str1) 'B1 "Red LED will flash"
    cogstop(2)
    cogstop(3)
    outa[11]~~
    outa[1]~~

    elseif STRCOMP (menu, @Str3) 'B2 "Green LED will flash"

    cog[2] := cognew(Blink(10), @stack[30])
    cog[3] := cognew(Blink(5), @stack[50])
    outa[11]~
    outa[1]~
    PUB Blink( pin)

    dira[pin]~~
    outa[pin]~

    repeat
    waitcnt(clkfreq/2 + cnt)
    !outa[pin]
    DAT
    Str1 byte "B1", 0
    Str3 byte "B2", 0


    program B


    CON

    _clkmode = xtal1 + pll16x ' use crystal x 16
    _xinfreq = 5_000_000

    VAR

    byte menu
    long stack[60]

    OBJ

    debug : "FullDuplexSerialPlus"
    PUB LedOnOff | counter, cog[2]

    dira[10] := 1
    dira[11] := 1
    dira[1] := 1
    dira[5] := 1

    dira[16] := 0
    dira[17] := 0
    counter := 0
    repeat
    waitcnt(clkfreq/2 + cnt)
    if ina[16]

    cogstop(cog[0])
    cogstop(cog[1])
    outa[11]~~
    outa[1]~~

    elseif ina[17]
    waitcnt(clkfreq*5 + cnt)
    cog[0] := cognew(Blink(10), @stack[30])
    cog[1] := cognew(Blink(5), @stack[50])
    outa[11]~
    outa[1]~

    PUB Blink( pin)

    dira[pin]~~
    outa[pin]~

    repeat
    waitcnt(clkfreq/2 + cnt)
    !outa[pin]
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-16 16:37
    If ina[16] evaluates to TRUE before ina[17] you'll effectively stop two random cogs which may well include cog 0 (the one running this code). NG. Apart from that it works just fine.
  • MD2010MD2010 Posts: 11
    edited 2011-02-17 11:47
    Is there any way to call specific cogs than stop those cogs back and forth in endless loop?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-17 12:04
    Yes, use coginit.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-02-17 12:26
    Using coginit is a bad habit to get into. It's much better to use cognew and to keep track of the values it returns for later use in cogstop.

    -Phil
Sign In or Register to comment.