Shop OBEX P1 Docs P2 Docs Learn Events
Code review with multiple cogs and cognew — Parallax Forums

Code review with multiple cogs and cognew

Brian218Brian218 Posts: 92
edited 2008-10-02 01:13 in Propeller 1
I am really stuck when it comes to using multiple cogs. I wrote the following code in-order to understand multiple cogs, and can't get it to run correctly. Could someone review my code and let me know what I'm doing wrong? As it is I'm not getting the leds to illuminate at all.

Thanks

Addendum....I uploaded the actual file, the 'cut and paste' in the original post did not show the correct spacing.

CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
LONG STACK[noparse][[/noparse]20]' STACK SPACE FOR NEW COG
LONG STACK1[noparse][[/noparse]20]
LONG STACK2[noparse][[/noparse]20]

PUB MAIN
DIRA[noparse][[/noparse]0..7]~~ 'SET PINS TO OUTPUT
DIRA[noparse][[/noparse]24..26]~~

COGNEW(SLOW, @STACK)
COGNEW(MEDIUM, @STACK1)
COGNEW(FAST, @STACK2)




PUB SLOW

REPEAT
!OUTA[noparse][[/noparse]0..2] ' TOGGLES 3 GREEN LEDs
WAITCNT(80_000_000 + CNT)




PUB MEDIUM

REPEAT
!OUTA[noparse][[/noparse]3..6] ' TOGGLES 3 YELLOW LEDs
WAITCNT(40_000_000 + CNT)



PUB FAST

REPEAT
!OUTA[noparse][[/noparse]24..26] ' TOGGLES 3 RED LEDs
WAITCNT(10_000_000 + CNT)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
This post is a work of art. Variations in spelling and grammar are intentional, artistic endeavors that add value to all of mankind.

Post Edited (Brian218) : 10/1/2008 11:47:03 AM GMT

Comments

  • pmrobertpmrobert Posts: 673
    edited 2008-10-01 10:47
    Is your code really not indented as your post shows? Indents are crucial to Spin. Ex.,
    You have
    REPEAT
    !OUTA[noparse][[/noparse]0..2] ' TOGGLES 3 GREEN LEDs
    WAITCNT(80_000_000 + CNT)
    
    


    which will not work. A more correct version would be
    REPEAT
      !OUTA[noparse][[/noparse]0..2] ' TOGGLES 3 GREEN LEDs
      WAITCNT(80_000_000 + CNT)
    
    
  • Brian218Brian218 Posts: 92
    edited 2008-10-01 10:54
    Thanks PMRobert, the code is indented; I tried to cut and past, and it didn't occur to me that pasting would omit the spacing...


    **I just went back and uploaded the actual file; sorry for any confusion.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    This post is a work of art. Variations in spelling and grammar are intentional, artistic endeavors that add value to all of mankind.

    Post Edited (Brian218) : 10/1/2008 11:08:28 AM GMT
  • agfaagfa Posts: 295
    edited 2008-10-01 12:00
    i believe you have to define the pins as outputs within·each method that is in a separate cog, not in the MAIN.

    example:

    PUB SLOW

    DIRA[noparse][[/noparse]0..2]~~
    REPEAT
    ··· !OUTA[noparse][[/noparse]0..2] ' TOGGLES 3 GREEN LEDs
    ··· WAITCNT(80_000_000 + CNT)




    Post Edited (agfa) : 10/1/2008 12:10:00 PM GMT
  • hippyhippy Posts: 1,981
    edited 2008-10-01 12:10
    As agfa said.

    When you use DIRA in the method which executes the CogNew it is setting DIRA for its Cog only, not for the Cogs which will started by CogNew.

    If you make your launched Cog code more complicated you may need to increase the size of your stack. If you hit any peculiar behaviour try that as a first resort.
  • Brian218Brian218 Posts: 92
    edited 2008-10-02 01:13
    PMRobert, Agfa and Hippy, Thank-you for your replies, I really appreciate all of your suggestions. Setting the pins to outputs inside the individual methods did the trick.

    Thanks again
    B-218

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    This post is a work of art. Variations in spelling and grammar are intentional, artistic endeavors that add value to all of mankind.
Sign In or Register to comment.