Shop OBEX P1 Docs P2 Docs Learn Events
Passing variable memory address to a method — Parallax Forums

Passing variable memory address to a method

roboticsrobotics Posts: 90
edited 2010-07-01 16:30 in Propeller 1
Hi,

I am having a bit of trouble understanding how to start a new cog to run a method while passing a variable's ("X") memory address to the method so that the changing value of the variable represented by the variable's memory address can be monitored by the calling method. A pretty much bare essentials example follows whereby the looped printing of the variable's address for some reason prints only one number "1408" over and over again where I was attempting to have it print a number incrementing by "1" for each pass through the loop. (I also don't understand where the repeating number "1408" came from! ) Any help is greatly appreciated. In advance, thank you.

Here's the code:


CON

_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x


VAR
long SqStack[noparse][[/noparse]60]


OBJ
debug : "FullDuplexSerialPlus"

PUB Main | X

debug.Start(31, 30, 0, 57600)
waitcnt(clkfreq * 2 + cnt)
debug.tx(16) ' CLS - Clear Screen

X := 10

cognew( Increase(@X), @SqStack)

repeat

debug.dec(@X)
debug.str(string(13))
waitcnt(clkfreq * 2 + cnt)


PUB Increase(XAddr)

repeat
XAddr := XAddr + 1
waitcnt(clkfreq * 2 + cnt)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-01 15:10
    1) In the future, please do not put program code in the text of a message. It loses all formatting which is vital to Spin. At a minimum use the [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] tags (without the extra spaces).

    2) XAddr is set to the address of X. To reference the variable itself, use LONG[noparse][[/noparse] XAddr ] as in LONG[noparse][[/noparse] XAddr ] := LONG[noparse][[/noparse] XAddr ] + 1. Look up the description of this in the Propeller Manual or the Prop Tool help files.

    3) @X is the address of X. Your "debug.dec(@X)" is displaying the address of X. Use "debug.dec(X)".
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-07-01 15:10
    X is a long variable that is located in the main stack.· @X is the address of X.· XAddr is located in cog 1's stack, and it contans the address of X.· Your code is just incrementing the value of XAddr, which is not visible to the main routine.· What you really want to do is increment the memory location pointed to by XAddr.· The last part of your code should look like this.

    · repeat

    ··· debug.dec(X)
    ··· debug.str(string(13))
    ··· waitcnt(clkfreq * 2 + cnt)


    PUB Increase(XAddr)

    repeat
    · long[noparse][[/noparse]XAddr] := long[noparse][[/noparse]XAddr] + 1
    · waitcnt(clkfreq * 2 + cnt)

    Edit: Mike posted as I was editing my post.· We're basically suggesting the same thing.
  • roboticsrobotics Posts: 90
    edited 2010-07-01 15:33
    Mike and Dave

    Many thanks for your help, this was a very elusive problem for me!
  • ErNaErNa Posts: 1,752
    edited 2010-07-01 16:30
    Hello robotics. that is really not the first time, someone comes up with this question, indeed, it is one of the most often asked questions.
    http://forums.parallax.com/showthread.php?p=919156 will show step by step, how I solved this problem for me. I'm not using another method any more, because it is now "normalized", that is, I no longer think about how to pass a variable, I just change the number and the table and that's it. But like always: in the beginning, one might ask: why do it so complicated? The answer: because it is incredible simple!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    cmapspublic3.ihmc.us:80/servlet/SBReadResourceServlet?rid=1181572927203_421963583_5511&partName=htmltext
    Hello Rest Of The World
    Hello Debris
    Install a propeller and blow them away wink.gif
Sign In or Register to comment.