Shop OBEX P1 Docs P2 Docs Learn Events
Can't get the coginit working!! — Parallax Forums

Can't get the coginit working!!

Uncle GeorgeUncle George Posts: 14
edited 2014-02-24 12:25 in Propeller 1
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

  • kuronekokuroneko Posts: 3,623
    edited 2014-02-22 22:13
    I can send the entire source code.
    Yes please.
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 22:18
    Here is the source code.
    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
  • kuronekokuroneko Posts: 3,623
    edited 2014-02-22 22:19
    Please wrap your code in [noparse]
    
    [/noparse] tags.                        
  • kuronekokuroneko Posts: 3,623
    edited 2014-02-22 22:21
    Common mistake. Each cog has its own set of dira/outa registers. Meaning any method started in a new cog should handle dira itself. When you run those methods in the context of the primary cog they inherit its dira settings.
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 22:23
    Did the copy and paste and didn't realize all the indentation are gone. Here is how the source code with proper indentations.

    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)
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 22:31
    Did the same thing. The main routine is the following:

    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.
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 22:33
    Thanks for the code tags.
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 22:35
    Thanks. I'll try the dira/outa registers for each of the cog.
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 22:55
    How do you do the
    
    tags?                        
  • kuronekokuroneko Posts: 3,623
    edited 2014-02-22 22:59
    How do you do the
    
    tags?
    Just type them as listed and insert the code inbetween.
    ' some code follows
    
  • msrobotsmsrobots Posts: 3,709
    edited 2014-02-22 23:07
    Type something like this

    [ 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
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-22 23:14
    ''
    ''    File: SequentialBrinkingLED.spin
    ''    Eight LED lights, 3 options of sequential brinking
    ''    Default : 4 LEDs brink left and 4 LEDs brink right sequentially
    ''    Switch left : 8 LEDs brink left sequentially
    ''    Switch right : 8 LEDs brink right sequentially
    ''    Default brinking frequency, 1 Hz, 1/2 second - clkfreq / 2 + cnt.
    ''    cog 0 -- scan for input for blinking direction control
    ''    cog 1 -- default brinking
    ''    cog 2 -- brinking left
    ''    cog 3 -- brinking right
    ''
    
    
    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
      dira[LED1..LED8]~~                                    'Set P1 thru P8 as outputs
      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
      dira[LED1..LED8]~~                                    'Set P1 thru P8 as outputs
      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
      dira[LED1..LED8]~~                                  'Set P1 thru P8 as outputs
      outa[LED1..LED8]~
      Repeat
        LEDOn := 9
        Repeat 8
          LEDOn -=1
          outa[LEDOn]~~
          waitcnt(clkfreq / 6 + cnt)
        outa[LED1..LED8]~
        waitcnt(clkfreq / 2 + cnt)
    

    I hope I did it correctly.
  • msrobotsmsrobots Posts: 3,709
    edited 2014-02-23 03:11
    took me a while to figure this out.

    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
        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
    

    BUT
        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
    

    Enjoy!

    Mike
  • Uncle GeorgeUncle George Posts: 14
    edited 2014-02-23 09:11
    Thanks Mike. Indentation is extremely important for logical blocks. Now, it works perfect. Great help. Thanks again.
  • StefanL38StefanL38 Posts: 2,292
    edited 2014-02-24 12:25
    Hi George you should'nt use coginit but COGNEW.

    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
Sign In or Register to comment.