Trying to Access a global variable from with in a object.
Coder96
Posts: 42
I'm trying to access a global variable from with in a object.
When all the code is in one file. It works as expected.
This outputs what I expect. I have 0's when the button is not pressed, and 1's when it is.
The problem comes into play when I put the OtherCog in a separate object/file.
File:test_global1.spin
File:test_global2.spin
The value is 15 when run.
I'm new to the propeller and micro controllers in general.
what am I missing?
When all the code is in one file. It works as expected.
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 SerCon_Tx = 8 SerCon_Rx = 9 VAR long Stack[14] long GlobalVar1 OBJ SerCon : "FullDuplexSerialPlus" PUB init cognew(OtherCog, @Stack) dira[16] := 0 repeat if(ina[16] <> 0) GlobalVar1 := 1 else GlobalVar1 := 0 PUB OtherCog ' Serial Console port SerCon.start(SerCon_Tx, SerCon_Rx, 0, 57600) SerCon.str(string(" ",SerCon#CR,SerCon#LF)) SerCon.str(string(" Serial Console started.",SerCon#CR,SerCon#LF)) repeat SerCon.dec(GlobalVar1) SerCon.str(string(" ",SerCon#CR,SerCon#LF)) waitcnt(100*(clkfreq/100) + cnt)
This outputs what I expect. I have 0's when the button is not pressed, and 1's when it is.
The problem comes into play when I put the OtherCog in a separate object/file.
File:test_global1.spin
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long GlobalVar1 OBJ G2 : "test_global2" PUB init G2.init dira[16] := 0 repeat if(ina[16] <> 0) GlobalVar1 := 1 else GlobalVar1 := 0
File:test_global2.spin
CON SerCon_Tx = 8 SerCon_Rx = 9 VAR long Stack[14] long GlobalVar1 'Will not compile without this. OBJ SerCon : "FullDuplexSerialPlus" PUB init cognew(OtherCog, @Stack) PUB OtherCog ' Serial Console port SerCon.start(SerCon_Tx, SerCon_Rx, 0, 57600) SerCon.str(string(" ",SerCon#CR,SerCon#LF)) SerCon.str(string(" Serial Console started.",SerCon#CR,SerCon#LF)) repeat SerCon.dec(GlobalVar1) SerCon.str(string(" ",SerCon#CR,SerCon#LF)) waitcnt(100*(clkfreq/100) + cnt
The value is 15 when run.
I'm new to the propeller and micro controllers in general.
what am I missing?
Comments
cognew(OtherCog(@GlobalVar1), @Stack)
Then, in the othercog, access the var with long[TheVarToAccess]
Typically this is handled by passing the address of a variable in the outer object (test_global1.spin) as a parameter to the initialization routine in the inner object (test_global2.spin) and saving the address of this variable in a variable in the inner object like:
With the amount of variables I'm working with I'll probably just move it back into one object.
If you are suggesting that you would have to add a lot of parameters, that is not the case. You only need to point to the first variable in the line. Then access the following longs by +4. If using words or bytes, adjust the math accordingly.