Shop OBEX P1 Docs P2 Docs Learn Events
newb question — Parallax Forums

newb question

Bobb FwedBobb Fwed Posts: 1,119
edited 2008-06-25 22:52 in Propeller 1
I am very new to the propeller....eh, just got it this morning.
I am trying to start something simple, something I am familiar with...rctime ... I know there are other objects and whatnot that do the same thing, but I'm learning a new language, so I'd like to figure it out for myself.

I am using the DIP chips on the Propeller RPM.

Here is the code I have:
'' RCTIME test

CON

  pin  = 3

VAR

  long time1
  long timestack[noparse][[/noparse]6]                         ' stack space for rctime cog
  long dispstack[noparse][[/noparse]6]                         ' stack space for LED display cog

PUB Main

  coginit(1, RCTIME(@time1), @timestack)    ' start cog 1 on RCTIME
  coginit(2, Disp_no(@time1), @dispstack)   ' dislpay RCTIME results on cog 2
  repeat                                    ' do nothing for forever

PUB RCTIME(timeAddr) | time                 ' get RCTIME value
  repeat
    DIRA[noparse][[/noparse]pin]~~                             ' set as output
    OUTA[noparse][[/noparse]pin]~~                             ' set HIGH
    waitcnt(clkfreq / 1000 + cnt)           ' wait 1ms
    time := cnt                             ' store current count
    DIRA[noparse][[/noparse]pin]~                              ' set as input
     
    waitpeq(|< pin, |< pin, 0)              ' wait until low again ?I think? ... it seems to work like this
     
    long[noparse][[/noparse]timeAddr] := ((cnt - time) - 3300) / 2000 ' store time to global var and compensate for minimum time and variable size

PUB Disp_no(nfoAddr) | tmp                  ' display binary value to 10 segment LED on PRPM
  dira[noparse][[/noparse]16..25]~~                            ' set leds to output
  outa[noparse][[/noparse]16..25]~~                            ' set leds HIGH (turns off LED...why?)
  repeat                                    ' repeat forever
    tmp := long[noparse][[/noparse]nfoAddr] <# 1023            ' get global var and limit it to 1023 (10 bits)
    outa[noparse][[/noparse]16..25] := tmp                     ' on (show binary value)
    waitcnt(381 + cnt)                      ' wait minimum
    outa[noparse][[/noparse]16..25]~~                          ' off
    waitcnt(40000 + cnt)                    ' reduce these friggin' bright LEDS!




What I am trying to accomplish: one cog (cog 1) continuously reads the RCtime of a photoresistor I have, and the other cog (cog 2) continuously displays the value (at a reduced brightness).

What happens: as the RCtime increases the LEDs start to blink (as it seems it is waiting for the RCtime), and for some unexplainable reason, no matter what divider I put on the "store time to global var" line, it doesn't actually affect the output, which seems to mean, I am getting the value from somewhere else.

I would also like to add some type of max time allowed to wait for the pin to go low. But everything I did made the resolution of RCtime go way down.

Please help, and let me know of any errors I have in my code.

Post Edited (Bobb Fwed) : 6/25/2008 9:13:41 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-25 22:06
    The stack sizes are way too small. Always use at least 20 longs at least.

    The WAITCNT in Disp_no is too small for Spin. 381 + CNT will probably pass before the WAITCNT can execute. The minimum is more like 1100 if I remember correctly.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-06-25 22:16
    The manual says 381 is minimum for the cnt operation--I actually discovered that first hand before I found it in the manual...look at page 323.

    I can't use 20 longs, anything higher than 6 makes the LED segments stay on permanently, and below six makes them stay off. Someone tell me why. At 6 stacks it operates, just not exactly how I want.
    I don't get the whole stack thing anyway...I thought each of these cogs have their own RAM...what do the stack accomplish?
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-06-25 22:28
    I take that back...with 10 or more on the stacks...it now works...I didn't have the right divider at the moment I posted last. So now it works, I am happy.
    But someone explain the need for stacks?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-25 22:45
    The Spin interpreter needs some place to keep some basic information that's part of a method call. The main method of a Spin program and the methods in the COGINIT / COGNEW calls have to be called somehow and they use the same mechanism as a normal method call. All expressions and statements each use some amount of stack space for computation. Even just "A := 5" uses a location or two. More complicated expressions use more stack space. Local variables in a method including the parameters and the result all are allocated in the stack.

    381 may be the "official" minimum, but I'd not count on it being adequate.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-06-25 22:52
    Mike Green said...
    381 may be the "official" minimum, but I'd not count on it being adequate.
    Yeah, it's not a super important part of the program...just a part so I can see the value, not important timing stuff.

    And thanks for the info...I'll keep in mind: "when in doubt, more stack space!"
Sign In or Register to comment.