Shop OBEX P1 Docs P2 Docs Learn Events
Just assembled my board, trying to get my head wrapped around SPIN — Parallax Forums

Just assembled my board, trying to get my head wrapped around SPIN

turbosupraturbosupra Posts: 1,088
edited 2009-10-03 02:49 in Propeller 1
Ok, I know my coding is wrong, and I'm trying to understand the SPIN coding language. Can anyone tell me what I've done wrong?

I was able to get the green led on pin 3 to flash at the clock speed I programmed, but now I'm trying to get the yellow led on pin 6 to flash at a different clock speed, how should my code be different so that I can have all the inputs and outputs function independently?

Thanks for reading





My modified PushButtonLedTest.spin code



  LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
  LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
  PUSHBUTTON    = 18                                    ' Pushbutton Input Pin

PUB ButtonBlinkSpeed                                    ' Main method

  '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.

  dira[noparse][[/noparse]LEDs_Green]~~                                    ' Set entire pin group to output

  repeat                                                ' Endless loop

    ! outa[noparse][[/noparse]LEDs_Green]                                  ' Change the state of pin group

    if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
      waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/4 second -> 2 Hz
    else
      waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz 


PUB ButtonBlinkSpeed2 
  
  dira[noparse][[/noparse]LEDs_Yellow]~~                                   ' Endless loop

  repeat

    ! outa[noparse][[/noparse]LEDs_Yellow]

    if ina[noparse][[/noparse]PUSHBUTTON] == 1
      waitcnt(clkfreq / 20 + cnt)
    else
      waitcnt(clkfreq / 4 + cnt)  








Original PushButtonLedTest.spin code


'' File: PushbuttonLedTest.spin
'' Test program for the Propeller Education Lab "Pe Platform Setup"

CON

  _clkmode      = xtal1 + pll16x                        ' Feedback and  PLL multiplier                        
  _xinfreq      = 5_000_000                             ' External oscillator = 5 MHz

  LEDs_START    = 0                                     ' Start of I/O pin group for on/off signals
  LEDs_END      = 15                                    ' End of I/O pin group for on/off signals
  PUSHBUTTON    = 18                                    ' Pushbutton Input Pin

PUB ButtonBlinkSpeed                                    ' Main method

  '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.

  dira[noparse][[/noparse]LEDs_START..LEDs_END]~~                          ' Set entire pin group to output

  repeat                                                ' Endless loop

    ! outa[noparse][[/noparse]LEDs_START..LEDs_END]                        ' Change the state of pin group

    if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
      waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz
    else                                                ' If pushbutton not pressed
      waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/20 second -> 10 Hz


Comments

  • James LongJames Long Posts: 1,181
    edited 2009-10-02 01:25
    turbosupra said...
    Ok, I know my coding is wrong, and I'm trying to understand the SPIN coding language. Can anyone tell me what I've done wrong?

    I was able to get the green led on pin 3 to flash at the clock speed I programmed, but now I'm trying to get the yellow led on pin 6 to flash at a different clock speed, how should my code be different so that I can have all the inputs and outputs function independently?

    Thanks for reading





    My modified PushButtonLedTest.spin code

    
    
      LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
      LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin
    
    PUB ButtonBlinkSpeed                                    ' Main method
    
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.
    
      dira[noparse][[/noparse]LEDs_Green]~~                                    ' Set entire pin group to output
    
      repeat                                                ' Endless loop
    
        ! outa[noparse][[/noparse]LEDs_Green]                                  ' Change the state of pin group
    
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
          waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/4 second -> 2 Hz
        else
          waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz 
    
    
    PUB ButtonBlinkSpeed2 
      
      dira[noparse][[/noparse]LEDs_Yellow]~~                                   ' Endless loop
    
      repeat
    
        ! outa[noparse][[/noparse]LEDs_Yellow]
    
        if ina[noparse][[/noparse]PUSHBUTTON] == 1
          waitcnt(clkfreq / 20 + cnt)
        else
          waitcnt(clkfreq / 4 + cnt)  
    
    
    
    





    Original PushButtonLedTest.spin code

    
    '' File: PushbuttonLedTest.spin
    '' Test program for the Propeller Education Lab "Pe Platform Setup"
    
    CON
    
      _clkmode      = xtal1 + pll16x                        ' Feedback and  PLL multiplier                        
      _xinfreq      = 5_000_000                             ' External oscillator = 5 MHz
    
      LEDs_START    = 0                                     ' Start of I/O pin group for on/off signals
      LEDs_END      = 15                                    ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin
    
    PUB ButtonBlinkSpeed                                    ' Main method
    
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.
    
      dira[noparse][[/noparse]LEDs_START..LEDs_END]~~                          ' Set entire pin group to output
    
      repeat                                                ' Endless loop
    
        ! outa[noparse][[/noparse]LEDs_START..LEDs_END]                        ' Change the state of pin group
    
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
          waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz
        else                                                ' If pushbutton not pressed
          waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/20 second -> 10 Hz
    
    
    


    Turbo,

    The main problem I see here....

    The code is only going to get to the first repeat cycle.....and will continue in this loop forever.

    "PUB ButtonBlinkSpeed2" Will never be called.

    The organization of your code is the main problem here.....your are thinking linearly....rather than by methods.

    My first question, Do you want to do this at any cost? Or do you specifically want to blink these two LED's with one COG?

    This task is pretty easy with the use of two cogs.....but with one, it is slightly harder.

    I will assume you are just trying to get two leds to blink independently at any cost.

    James L

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L
    Partner/Designer

    Lil Brother SMT Assembly Services

    Please note: Due to economic conditions the light at the end of the tunnel will be turned off until further notice. Thanks for your understanding.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-02 01:35
    Hey James

    At this point I don't mind how many cogs I have to use, I'm just trying to familiarize myself with the code and how it interacts with the chip

    How should I alter my code, in the easiest manner, so that I can get the two lights to blink independent of each other?
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-10-02 01:47
    Here is my code that flashes 2 leds on different pins with different cogs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • photomankcphotomankc Posts: 943
    edited 2009-10-02 01:58
    As you have it written ButtonBlinkSpeed2 is never called and thus never executes... ButtonBlinkSpeed is the starting routine that is loaded at bootup and it simply executes your loop which never calls the second routine ButtonBlinkSpeed2.· There are two ways to do this.· Using a single cog you could execute the loop about every millisecond (clkfreq/1000+cnt) or so and count each loop as it runs.· You would toggle the green LED at say every 125·loops and the yellow LED at every·63 loops.·
      LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
      LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin
     
    PUB ButtonBlinkSpeed | runCt                                   ' Main method
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.
      dira[noparse][[/noparse]LEDs_Green]~~
      dira[noparse][[/noparse]LEDs_Yellow]~~
      
      runCt := 0
      repeat
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                 ' If pushbutton pressed
          if runCt // 250 == 0                  '4 toggles per second / 2 cycles per second
            !outa[noparse][[/noparse]LEDs_Green]
            !outa[noparse][[/noparse]LEDs_Yellow] 
                                                 
        elseif ina[noparse][[/noparse]PUSHBUTTON] == 0
          if runCt // 125 == 0                    '8 toggles per second / 4 cycles per second
            !outa[noparse][[/noparse]LEDs_Green]
          if runCt // 63 == 0                     '16 toggles per second / 8 cycles per second
            !outa[noparse][[/noparse]LEDs_Yellow]
       
       waitcnt(clkfreq/1100+cnt)                  'Wait a little less than a millisecond to allow for code execution but this is
                                                  'just a guess at the value....
       runct++                                 
    

    I couldn't test the button part here but the blinking works.

    You could then increase or decrease that value if a button is being pressed.· This would all run in the first cog (internal processor)
    Option two is you use the cognew command and launch the second function into another processor which then completely independently runs that loop ie cognew(ButtonBlinkSpeed2, @Stack).· Stack is a long array that the launched cog will use to store it's stack.· Look for the PE Labs document in the tacked threads up the top of the forum.· It's a great intro to Spin and goes into detail on how to launch cogs to run processes in parallel.

    Post Edited (photomankc) : 10/2/2009 2:04:25 AM GMT
  • James LongJames Long Posts: 1,181
    edited 2009-10-02 01:59
    I looked at my post...and figured...I asked more questions than I answered......

    So here is a cheat::

    
      LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
      LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin
    
    VAR
      Long  stack[noparse][[/noparse]9]                            '<--------[b] this is new[/b]
    
    PUB ButtonBlinkSpeed                                    ' Main method
    
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.
    
      cognew(ButtonBlinkSpeed2(LEDS_Yellow, pushbutton), @stack)   '<--------[b] Another item that is new.....calls a new cog to start![/b]
    
      dira[noparse][[/noparse]LEDs_Green]~~                                    ' Set entire pin group to output
    
      repeat                                                ' Endless loop
    
        ! outa[noparse][[/noparse]LEDs_Green]                                  ' Change the state of pin group
    
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
          waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/4 second -> 2 Hz
        else
          waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz 
    
    
    PUB ButtonBlinkSpeed2(LEDS_color, buttonloc)            '<---------[b] this has changed[/b]
      
      dira[noparse][[/noparse]LEDS_color]~~                                   ' Endless loop 
    
      repeat
    
        ! outa[noparse][[/noparse]LEDS_color]                                    
    
        if ina[noparse][[/noparse]buttonloc] == 1
          waitcnt(clkfreq / 20 + cnt)
        else
          waitcnt(clkfreq / 4 + cnt)
    
    
    



    I didn't try this....but it will compile.

    James L

    EDIT: the bottom method timing should be changed...or the led's will blink the same when the button is pushed........if you change the timing....they will blink at different rates.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L
    Partner/Designer

    Lil Brother SMT Assembly Services

    Please note: Due to economic conditions the light at the end of the tunnel will be turned off until further notice. Thanks for your understanding.

    Post Edited (James Long) : 10/2/2009 2:09:11 AM GMT
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-02 02:36
    Thanks!

    If I understand this correctly,

    In the first section you have a description

    In the second section you have the crystal frequency value and variables defined to pins 24 and 26. What does CON mean? Constants? And what's with the function _Clkmode? Is that something that is in every circuit that needs to count?

    In the 3rd section it appears you define more variables, I am not sure as to why just yet

    In the 4th section you have PUB start, which I think defines where the instructions start? And then variable = and a function where it grabs a new cog

    And the rest I'm lost. I can make it work, but I'm trying to figure out what each part of the code does in making it work



    Cluso99 said...
    Here is my code that flashes 2 leds on different pins with different cogs.



    
    '' &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    '' &#9474; Cluso's TriBladeProp PCB:  Led Flash on P24                       v0.000 &#9474;
    '' &#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
    '' &#9474;  Author:            "Cluso99" (Ray Rodrick)                              &#9474;
    '' &#9474;  Copyright (c) 2009 "Cluso99" (Ray Rodrick)                              &#9474;
    '' &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
                                 
    '' RR20090208   First version
    '' RR20090331   Modified for TriBladeProp pcb testing
    
    
    Con
    _CLKMODE = xtal1 + pll16x
    _XINFREQ = 5_000_000 + 0000
    
      PinBlue   =   24
      PinRed    =   26        
    
    
    
    VAR
      long  Stack[noparse][[/noparse]32]               'stack space for new cog
      long  Stack2[noparse][[/noparse]32]               'stack space for new cog
      byte  Cog                     'hold ID of cog in use, if any
      byte  Cog2                     'hold ID of cog in use, if any
      
    PUB Start
    
      Cog := cognew(Toggle(PinBlue, 100, 0), @Stack) + 1   'flash led on/off=500mS
      Cog2 := cognew(Toggle(PinRed, 500, 0), @Stack2) + 1   'flash led on/off=2500mS
    
    
    PRI Toggle(Pin, DelayMS, Count)  
    {{toggle pin, delay between toggle, for Count toggles
      If Count = 0,toggle forever.}}
          
      dira[noparse][[/noparse]Pin]~~                   'make Pxx output
      repeat                        'Starts a repeat loop
         !outa[noparse][[/noparse]Pin]                 'Invert output Pxx
         waitcnt(clkfreq / 1000 * DelayMS + cnt)  'wait mS
      while Count := --Count #> -1  'while not 0 (make min -1)
      Cog~                          'clear Cog ID variable
    
    
    
    
  • photomankcphotomankc Posts: 943
    edited 2009-10-02 02:49
    http://forums.parallax.com/forums/default.aspx?f=25&m=156644

    CON is constants -- symbols that are fixed values in the program
    _Clkmode is a constant used to control the system clock speed.
    _XINFREQ = is a constant that tells the prop what the crystal input freq is.
    if you don't define these then the prop uses it's internal clock at RCFAST which is 12Mhz give or take.... a lot.

    VAR section is where you define global variables for the spin object (file) these can be accessed by any function in the spin object (file)

    PUB Start is the first function in the object and so it is the start-up function. From there all other code that runs must be executed from here or launched into another cog (processor) using Cognew. In this case it launched two new cogs in seperate processors and then it quits leaving the other two processors running. Each one is running the code in Toggle completely seperately.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-02 03:06
    I ran this and it works ... thank you

    Can you tell me the math you used to calculate the toggles and cycles per second, and is runCt an object that is already built into spin?


    photomankc said...
    As you have it written ButtonBlinkSpeed2 is never called and thus never executes... ButtonBlinkSpeed is the starting routine that is loaded at bootup and it simply executes your loop which never calls the second routine ButtonBlinkSpeed2. There are two ways to do this. Using a single cog you could execute the loop about every millisecond (clkfreq/1000+cnt) or so and count each loop as it runs. You would toggle the green LED at say every 125 loops and the yellow LED at every 63 loops.

      LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
      LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin 
    
      
    
    PUB ButtonBlinkSpeed | runCt                                   ' Main method 
    
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz. 
    
      dira[noparse][[/noparse]LEDs_Green]~~
      dira[noparse][[/noparse]LEDs_Yellow]~~
      
      runCt := 0
      repeat
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                 ' If pushbutton pressed
          if runCt // 250 == 0                  '4 toggles per second / 2 cycles per second
            !outa[noparse][[/noparse]LEDs_Green]
            !outa[noparse][[/noparse]LEDs_Yellow] 
                                                 
        elseif ina[noparse][[/noparse]PUSHBUTTON] == 0 
    
          if runCt // 125 == 0                    '8 toggles per second / 4 cycles per second
            !outa[noparse][[/noparse]LEDs_Green] 
    
          if runCt // 63 == 0                     '16 toggles per second / 8 cycles per second
            !outa[noparse][[/noparse]LEDs_Yellow] 
    
        
    
       waitcnt(clkfreq/1100+cnt)                  'Wait a little less than a millisecond to allow for code execution but this is
                                                  'just a guess at the value.... 
    
       runct++                                  
    
    


    I couldn't test the button part here but the blinking works.


    You could then increase or decrease that value if a button is being pressed. This would all run in the first cog (internal processor)

    Option two is you use the cognew command and launch the second function into another processor which then completely independently runs that loop ie cognew(ButtonBlinkSpeed2, @Stack). Stack is a long array that the launched cog will use to store it's stack. Look for the PE Labs document in the tacked threads up the top of the forum. It's a great intro to Spin and goes into detail on how to launch cogs to run processes in parallel.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-02 03:18
    Thanks [noparse]:)[/noparse]

    How come you had to add the Variable stack? And how come/where was LEDs_Yellow changed to LEDs_color?

    James Long said...
    I looked at my post...and figured...I asked more questions than I answered......

    So here is a cheat::

    
      LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
      LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin
    
    VAR
      Long  stack[noparse][[/noparse]9]                            '<-------- this is new
    
    PUB ButtonBlinkSpeed                                    ' Main method
    
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.
    
      cognew(ButtonBlinkSpeed2(LEDS_Yellow, pushbutton), @stack)   '<-------- Another item that is new.....calls a new cog to start!
    
      dira[noparse][[/noparse]LEDs_Green]~~                                    ' Set entire pin group to output
    
      repeat                                                ' Endless loop
    
        ! outa[noparse][[/noparse]LEDs_Green]                                  ' Change the state of pin group
    
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
          waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/4 second -> 2 Hz
        else
          waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz 
    
    
    PUB ButtonBlinkSpeed2(LEDS_color, buttonloc)            '<--------- this has changed
      
      dira[noparse][[/noparse]LEDS_color]~~                                   ' Endless loop 
    
      repeat
    
        ! outa[noparse][[/noparse]LEDS_color]                                    
    
        if ina[noparse][[/noparse]buttonloc] == 1
          waitcnt(clkfreq / 40 + cnt)
        else
          waitcnt(clkfreq / 2 + cnt)
    
    
    



    I didn't try this....but it will compile.

    James L

    EDIT: the bottom method timing should be changed...or the led's will blink the same when the button is pushed........if you change the timing....they will blink at different rates.
  • photomankcphotomankc Posts: 943
    edited 2009-10-02 03:22
    Ok,

    A millisecond is 1/1000th of a second so we take the number of clock cycles in one second and divide that by 1000 or in this case 1100 so that we give the code some time to execute since it will obviously take some time to run. This tells waitcnt how many system clock ticks we want to it to wait before it goes on. So knowing the code will loop about 1000 times a second then if we want the light to blink on and off every 1/4 of a second we need it to turn on AND off every 250 loops (1000/4). Since we need it turn on and off we need the toggle to happen twice in that period of time so we need it to execute every 125 loops (250/2). The // operator divides runCt by 125 and reports the remainder.... if runCt is a multiple of 125 on any particular loop iteration the remainder will be zero and then we toggle the pins output.

    runCt is a local variable declared by ButtonBlinkSpeed | runCt. Local variables exist only in the function in which they are defined. Just before the loop starts we set that variable to zero to initialize it and after that the loop increments it (runCt++) each time it loops. It will just keep incrementing forever until it overflows and returns to zero.
  • photomankcphotomankc Posts: 943
    edited 2009-10-02 03:27
    @Turbo,

    You really need to pick up the "PE Labs" manual and start from the beginning. Otherwise your going to be stumbling around in the dark like this for a long time. I was in your shoes at first and it took me about a week of working through the examples in the lab before I was writing programs to actually do something useful.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-02 03:44
    I'm on page 40 of the "Propeller Educational Kit Labs: Fundamentals"

    Is that the book you are talking about? If so, I guess I will read some more before I ask any more questions



    photomankc said...
    @Turbo,

    You really need to pick up the "PE Labs" manual and start from the beginning. Otherwise your going to be stumbling around in the dark like this for a long time. I was in your shoes at first and it took me about a week of working through the examples in the lab before I was writing programs to actually do something useful.
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-10-02 03:51
    You need to read some of the prop manual. Lots of good info in there for beginners.
    CON and VAR have been explained by others and there is more info in the manual.
    The PUB Start is a public routine and since it is the first, it is where execution begins.
    Cog := cognew(Toggle(PinBlue, 100, 0), @Stack) + 1   'flash led on/off=500mS
    

    This just starts a new cog with the program (routine) called Toggle. The rest are parameters which are passed to this routine. cognew returns the cog number (0..7) that it starts, so we add +1 to this to give a value 1-8. If no cog can be started -1 is returned, so we have just converted this to 0. This is quite a common practice and cog (or cog2) is can be used later to stop the cog. You need to supply a seperate stack space for each cog running spin.

    Obviously, the next line does exactly the same thing, but in another cog, using a different time, led pin and stack space.

    The routine should be self explanatory. If you don't understand the hyroglyphics such as ~~, !, look them up in the manual. Unfortunately, these are shorthand methods and are not readily obvious.

    The last line cog~ is irrelevant and also incorrect as it does not take into account the 2 cogs (cog and cog2). Each cog will stop after this instruction. However, this will not happen as I called the routines with 0 count so the repeat loop will run indefinately.

    Postedit: Sorry I was interrupted during my replay and others have responded. But I notice you are trying to set a group of pins. I suggest you look at the manual in the help section while you have proptool open, and look up DIRA. There are numerous ways to do this.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm

    Post Edited (Cluso99) : 10/2/2009 3:58:49 AM GMT
  • photomankcphotomankc Posts: 943
    edited 2009-10-02 04:37
    turbosupra said...
    I'm on page 40 of the "Propeller Educational Kit Labs: Fundamentals"

    Is that the book you are talking about? If so, I guess I will read some more before I ask any more questions



    photomankc said...
    @Turbo,

    You really need to pick up the "PE Labs" manual and start from the beginning. Otherwise your going to be stumbling around in the dark like this for a long time. I was in your shoes at first and it took me about a week of working through the examples in the lab before I was writing programs to actually do something useful.
    Keep going with it.· Don't feel bad for asking, but without a certain level of understanding it's going to be tedious and slow going to get answers here.· It'll be easier if you have·a basic understanding of the structure of a spin object and methods (functions) and calling them.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-02 11:52
    Thanks all

    I'll do some more reading this weekend and hopefully be able to have a better understanding of some of the basic things by Monday
  • James LongJames Long Posts: 1,181
    edited 2009-10-02 16:15
    turbosupra said...
    Thanks [noparse]:)[/noparse]

    How come you had to add the Variable stack? And how come/where was LEDs_Yellow changed to LEDs_color?

    James Long said...
    I looked at my post...and figured...I asked more questions than I answered......

    So here is a cheat::

    
      LEDs_Green    = 3                                     ' Start of I/O pin group for on/off signals
      LEDs_Yellow   = 6                                     ' End of I/O pin group for on/off signals
      PUSHBUTTON    = 18                                    ' Pushbutton Input Pin
    
    VAR
      Long  stack[noparse][[/noparse]9]                            '<-------- this is new
    
    PUB ButtonBlinkSpeed                                    ' Main method
    
      '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz.
    
      cognew(ButtonBlinkSpeed2(LEDS_Yellow, pushbutton), @stack)   '<-------- Another item that is new.....calls a new cog to start!
    
      dira[noparse][[/noparse]LEDs_Green]~~                                    ' Set entire pin group to output
    
      repeat                                                ' Endless loop
    
        ! outa[noparse][[/noparse]LEDs_Green]                                  ' Change the state of pin group
    
        if ina[noparse][[/noparse]PUSHBUTTON] == 1                             ' If pushbutton pressed
          waitcnt(clkfreq / 20 + cnt)                       ' Wait 1/4 second -> 2 Hz
        else
          waitcnt(clkfreq / 4 + cnt)                        ' Wait 1/4 second -> 2 Hz 
    
    
    PUB ButtonBlinkSpeed2(LEDS_color, buttonloc)            '<--------- this has changed
      
      dira[noparse][[/noparse]LEDS_color]~~                                   ' Endless loop 
    
      repeat
    
        ! outa[noparse][[/noparse]LEDS_color]                                    
    
        if ina[noparse][[/noparse]buttonloc] == 1
          waitcnt(clkfreq / 40 + cnt)
        else
          waitcnt(clkfreq / 2 + cnt)
    
    
    



    I didn't try this....but it will compile.

    James L

    EDIT: the bottom method timing should be changed...or the led's will blink the same when the button is pushed........if you change the timing....they will blink at different rates.

    Turbo,

    The stack is for run time work space (stack space). This is for use of variables and such.......in case you want to change the LED you are blinking. This is on Pg. 107 of the Propeller manual.

    Because the nomenclature on spin...the names can not be the same. The cognew command uses the constants that you have named, but the PUB method must have a different name for those items. This can be a confusing area of spin........

    The PUB method just uses an alias (something you make up to keep things straight). This is my way of thinking........sort of having a nickname for the same thing.

    James L

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L
    Partner/Designer

    Lil Brother SMT Assembly Services

    Please note: Due to economic conditions the light at the end of the tunnel will be turned off until further notice. Thanks for your understanding.
  • JonnyMacJonnyMac Posts: 9,198
    edited 2009-10-02 16:35
    There's a pretty good Spin tutorial in the manual and, if memory serves me, demonstrates how to flash multiple LEDs using different cogs.
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2009-10-02 17:21
    JonnyMac said...
    There's a pretty good Spin tutorial in the manual and, if memory serves me, demonstrates how to flash multiple LEDs using different cogs.
    This tutorial is no longer in the Propeller Manual v1.1, it was ported to the Propeller Help file along with the code listings in the Propeller Tool v1.2.6. The Propeller Manual v1.1, the PE Kit Labs PDF and sample code listings are bundled in there as well.

    -Stephanie Lindsay
    Editor, Parallax Inc.
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-10-03 02:20
    Thanks Steph.

    Yes, the PropTool download 1.2.6 comes with lots of good info and documents. Greatly improved. The only think missing IIRC, the web doesn't state all this great info is present in the download.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-03 02:49
    Thanks for the help all


    I'm making a little progress [noparse]:)[/noparse]

Sign In or Register to comment.