Table of indirect address's
bmentink
Posts: 107
Hi All,
I am trying to load some variables at run time from a table that holds their address's ... i.e the DAT section is
So the table holds the address of the variables, now my problem is how do I load up those variables via the table ..
I tried the following:
That did not do it ... then I discovered @@ so tried the following:
but all I got was a compile error ..
Can anyone help?
I am trying to load some variables at run time from a table that holds their address's ... i.e the DAT section is
DAT DutyA long 0 DutyB long 0 DutyC long 0 phases long @DutyA, @DutyB, @DutyC long @DutyA, @DutyC, @DutyB long @DutyB, @DutyC, @DutyA long @DutyB, @DutyA, @DutyC long @DutyC, @DutyA, @DutyB long @DutyC, @DutyB, @DutyA
So the table holds the address of the variables, now my problem is how do I load up those variables via the table ..
I tried the following:
repeat i from 0 to 5 long[@phases] [i*3] := 10 long[@phases] [(i*3) +1] := 20 long[@phases] [(i*3) +2] := 40
That did not do it ... then I discovered @@ so tried the following:
repeat i from 0 to 5 @@phases[i*3] := 10 @@phases[(i*3) +1] := 20 @@phases[(i*3) +2] := 40
but all I got was a compile error ..
Can anyone help?
Comments
Otherwise you will need a variable to hold the calculated offset.
You'd then add this offset to all references to the "phases" array.
Can you please explain why you added + 16 to each address?
I am in the top object, so how do I access the variables?
Thanks
I added 16 because that's what the "offset" variable would always equal as long as it's in the top object (based on prior experience).
How you access the values will depend on what you're trying to do.
x := long[phases][2] Edit: this is incorrect. See post #16
will set x to whatever the value of "DutyC" is (in this case zero).
If you were to use the offset variable (instead of adding 16 to each element) you'd access the same value with
x := long[phases + offset][2] Edit: this is also incorrect.
In the top object "offset" would equal 16. See post #16
In this example, I assumed you wanted to access the third element (element "2") of the "phases" array.
Like my original example, I am trying to indirectly set the contents of DutyA,B,C via the table ... if I set the table with offest of 16 as you say then
long[phases] [0] := 20
long[phases] [1] := 40
long[phases] [2] := 60
.. works as intended, but
long[phases] [3] := 20
long[phases] [4] := 40
long[phases] [5] := 60
doesn't work! It seems only the 1st line in the table is available ..
Also, I found that the following syntax works too (without the 16 offset in the table)
long[@@phases] [0] := 20
long[@@phases] [1] := 40
long[@@phases] [2] := 60
but again, only works for the 1st row ..
[EDIT] It only works with the 1st 3 items, no matter how many there are in the row ..
It gives you different results depending of whether it is used in DAT sections to initialize things, as you do, or in Spin code.
Neither are what you want which is the actual address in HUB RAM of whatever it is applied to.
@@ does not help.
That offset of 16 is there because @ returns the offset from the beginning of the object which is 16 in HUB RAM. Of course that only works if you are in the main, first, object.
The BST Spin tool has a @@@ operator which does give you what you want but then you end up with code that Prop Tool users cannot use.
Hi Heater,
Cool, I am using the BST tool, can you explain how to use the @@@ operator?
Can I just do: long[@@@phases] [0] :=
Cheers,
B.
you have 3 variables (longs)
in your table you keep the addresses of those 3 longs multiple times
so writing
long[phases] [0] := 20
long[phases] [1] := 40
long[phases] [2] := 60
will do the same as
long[phases] [3] := 20
long[phases] [4] := 40
long[phases] [5] := 60
will do the same as
DutyA := 20
DutyB := 40
DutyC := 60
what is the goal?
Enjoy!
Mike
No, not quite ...
long[phases] [0] := 20
long[phases] [1] := 40
long[phases] [2] := 60
DutyA := 20
DutyB := 40
DutyC := 60
long[phases] [3] := 20
long[phases] [4] := 40
long[phases] [5] := 60
DutyA := 20
DutyB := 60 <--
DutyC := 40 <-- swapped because Order of DutyB and C changed in table ..
The goal is to load to DutyA .. C with a different order ... part of code for a six step BLDC motor ..
I have simplified it somewhat for you guys ... the contents will be of course different than the constants I have shown. What is important, if you look at the table
is the sequence ..
Still don't have an answer for this ...
Can you attach an archive of your simplified code that shows this strange behavior? The problem might be elsewhere in the code.
Not sure what you want ... the relevant code is in the 1st post, there is no other code to speak of ...
I just need to know how to make a table of addresses of variables and then how to store data in the variables via the table (indirect addressing) .. if that is not clear to anyone, how do I make it clearer ??
Sounds clear enough.
I think this works in BST:
Note that BST has a means of showing you a listing of what it compiles your code to so you can verify what addresses it actually puts in those tables. Just select the "Compiler Listing" option in the "View" menu before compiling your code and a listing window will appear.
Now do you want to be changing those addresses in the table as you run?
' long[long[@phase_1][0]] := 21 ' set phase_1[0] to 21
' FDS.dec(long[long[@phase_1][0]]) ' display value in phase_1[0]
Quick program to show the values in all of the phases.
I'm going to have to think about this.
I don't see a difference between "long[@phases[n++]]" and "phases[n++]".
I personally think the second "long" is an unnecessary complication.
This:
should have been this:
In several of my touchscreen projects I list the various button parameters (location, color, text, etc) in a table. Here's an example (for TV control):
The list tell a method that creates the touch menu what buttons to put where and what they should say. "_BoxPower" indicates the action to perform. This "action" could be the address of another menu (if it were an address the action would require two bytes and the zero in the above examples would hold the upper byte of the address).
The list of these buttons in stored elsewhere in the DAT section.
This makes creating menus much easier since I just add the parameters of each button to the DAT section and list the buttons the menu is to use in other section.
The menu one level higher than "menuTv" would have this as one of the buttons.
When either of the above button locations is touched, it would launch the menu located at "@menuTv".
The parameter "@menuTv" is passed to a method which generates the menu and monitors selections.
I imagine there are other reasons one would want to list address in DAT section but I haven't found many myself. When it is useful to list address this way, I think it can be a very powerful tool.
You just overwrite your phase table and not the variables DutyA..C.
With your table you can do it so:
But why not store only the variable indexes in the phase table, like that: This makes much more readable.
Andy
Wow, that's great .. worked a treat ..
I have a 6-step 3-phase BLDC working great in open-loop now ... next step, to add sensorless commutation ..
Cheers,
B.
Jonathan
Ah the "+= @@0" trick. I forgot about that.
I really wonder on what planet that thing makes any sense. Seems to work though.
Is it actually specified in the Spin language that it should work? I mean 0 is not any kind of location of any data in HUB to take the address of.
Jonathan
As far as I know their is no rigorous definition of Spin. As in syntax described in BNF or whatever. Never mind the semantics.
So I guess anything might work.
One of my favorites is:
Who would guess what string of byte values that puts into a DAT section?
Jonathan
Weird stuff anyway.
Then there is the risk that such a fix breaks some existing code. I bet there is someone out there who has used this "feature".
Actually, thinking about it a bit, perhaps "string" + "string" should just be an error. I never did like + as a concatenation operator and it causes all kinds of confusion in languages like JavaScript.