Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Mulit Dimensional Array Questions
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sat Jan 31, 2015 7:41 pm    Post subject: Mulit Dimensional Array Questions Reply with quote

Hello all,


What is the correct syntax to convert this:

Code:

 Dim RxBuffer(10,64) As Byte
 Dim Temp_RxBuffer(64) As Byte

        spiin RxTempBuf(1), Dta_len  'Rx_FIFO_Bytes
 



to this?
Code:

 Dim RxBuffer(10,64) As Byte
 Dim Temp_RxBuffer(64) As Byte
 
       spiin Rxbuffer(idx, 1), Dta_len  'Rx_FIFO_Bytes
 


In the main .Bas file I do have the multi index array initialized so that is not the problem.
Code:

  b_temp1 = RxBuffer(Temp1 ,Temp2)
 


The latter does not compile. I am trying to avoid having to perform a loop operation to copy the data from RxTempBuf. I realize this may not be possible today, since the multi index arrays are new but if it is and I just have syntax wrong I would appreciate pointers!

Thanks all,
Tim




(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Sun Feb 01, 2015 8:02 am    Post subject: Reply with quote

Dim RxBuffer(10,64) As Byte 'array of 10 arrays(64)
Dim RxtempBuf(64) As Byte
Dim temp_b as byte

Imagine RxBuffer(10,64) as 1O rows, each row is an array(64):

1st_array(64) starts at adr RxBuffer(1,1) ends at RxBuffer(1,64) (1st row)
2nd_array(64) starts at adr RxBuffer(2,1) ends at RxBuffer(2,64) (2nd row)
3rd_array(64) starts at adr RxBuffer(3,1) ends at RxBuffer(3,64)
...
10th_array(64) starts at adr RxBuffer(10,1) ends at RxBuffer(10,64)


Now you want to place array RxTempBuf(64) into RxBuffer(10,64).
First choose if you want to place it into the 1st_array or the 2nd_array or...

In the example the 1st_array is chosen.
So you copy:

RxBuffer(1,1) = RxTempBuf(1) 'copy RxTempBuf(1) to the 1st row, 1st element of RxBuffer
RxBuffer(1,2) = RxTempBuf(2) 'copy RxTempBuf(2) to the 1st row, 2nd element of RxBuffer
RxBuffer(1,3) = RxTempBuf(3)
...
RxBuffer(1,64) = RxTempBuf(64)

Use loop:
For i = 1 to 64
RxBuffer(1,i) = RxTempBuf(i)
Next

Note that the first index is always 1, as the whole original array goes to the first row.

Lets read the 15th element from the array:
temp_b = RxBuffer(1,15) 'row 1, element 15

Write 25 to the 30th element
RxBuffer(1,30) = 25

Code:
 spiin Rxbuffer(idx, 1), Dta_len  'Rx_FIFO_Bytes

probably should be

 spiin Rxbuffer(1 , idx), Dta_len  'Rx_FIFO_Bytes
 
Back to top
View user's profile
Meister

Bascom Member



Joined: 27 May 2010
Posts: 319

blank.gif
PostPosted: Sun Feb 01, 2015 11:16 am    Post subject: Reply with quote

No way to use memcopy there instead of loop? Is there some change in memcopy in 2.0.7.8?
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 01, 2015 12:53 pm    Post subject: Reply with quote

Meister wrote:
No way to use memcopy there instead of loop? Is there some change in memcopy in 2.0.7.8?

Sure it works Very Happy
Only it won't, if the array dimensioning of the first range in the target array doesn't match the range of the source array.
Code:
$Regfile = "m328pdef.dat"
$Crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32


Dim RxBuffer(64 , 10) As Byte
Dim Temp_RxBuffer(64) As Byte
Dim bs As Word
Dim Index_RxBuffer as Byte

Temp_RxBuffer(5) = 69
Index_RxBuffer = 3

bs = MemCopy(Temp_RxBuffer(1) , RxBuffer(1 , Index_RxBuffer) , 64)

End


Btw., this doesn't work, however it should:
Code:
print RxBuffer(1 , 1) , 640
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sun Feb 01, 2015 8:42 pm    Post subject: Reply with quote

Hi eveyone,

The whole idea was NOT to use a tempbuffer. Maybe I muddied the water with the line showing the tempbuffer in what I wanted to change the code to. ie. I should have left out the Temp_RxBuffer(64) part to be clearer.

So to clarify, I want to make this work - which it currently does not.
Code:

       Dim RxBuffer(10,64) As Byte
       spiin Rxbuffer(idx, 1), Dta_len  'Rx_FIFO_Bytes
 



As for the memcopy, Mark wrote yesterday in http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=12743 that memcopy *should work* but does not. He is going to look into it.

Anyway, the main question is, is there a way to perform the spiin command -
spiin Rxbuffer(idx, 1), Dta_len 'Rx_FIFO_Bytes

Thanks again for all of the responses!
Tim
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 01, 2015 9:44 pm    Post subject: Reply with quote

TSEYFARTH wrote:
As for the memcopy, Mark wrote yesterday in http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=12743 that memcopy *should work* but does not. He is going to look into it.

I've updated only by today, and I can tell it works.
As you can tell too, if you run my code in simulator and watch the memory window.

The reason no code in the linked thread will work, is the wrong order of both indexes:
Code:
Dim RxBuffer(10,64) As Byte

sets up an array like that:
Code:
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
+60 times...
00 00 00 00 00 00 00 00 00 00

Obviously a straight copy with memcopy() of 64 bytes into several blocks of 10 bytes will fail.
Quote:
So to clarify, I want to make this work - which it currently does not.

You should be able to use this as workaround:
Code:
$Regfile = "m328pdef.dat"
$Crystal = 4000000
$hwstack = 40
$swstack = 16
$framesize = 32


Dim RxBuffer(64 , 10) As Byte
Dim RxBOvl(640) As Byte At RxBuffer Overlay
Dim tmp_idx As Word
Dim idx as Byte

Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 4 , Noss = 1

Spiinit

idx = 2

tmp_idx = idx - 1
tmp_idx = tmp_idx * 64
tmp_idx = tmp_idx + 1

spiin RxBOvl(tmp_idx) , 64                                  'Rx_FIFO_Bytes

End
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sun Feb 01, 2015 10:27 pm    Post subject: Reply with quote

Hi MWS,

The use of an overlay is interesting - I will try that.

For the multi-index arrays, the release notes said that the simulator will not work correctly with multi index arrays, so be aware of that.

I do not understand what you mean by:
Quote:

The reason no code in the linked thread will work, is the wrong order of both indexes:
Code:
Dim RxBuffer(10,64) As Byte


The intent is to have 10 indexes, each 64 bytes wide. The code
spiin Rxbuffer(idx, 1), Dta_len 'Rx_FIFO_Bytes Should be able to receive the number of bytes (Dta_len) into the Index identified by the variable idx.

Anyway, the overlay is interesting and may be something to look at for the near term - of faster than the looping. I bet though, that by the time this all gets settled, Mark will have an update ready that cures these ills Smile

Tim
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Feb 02, 2015 12:25 am    Post subject: Reply with quote

TSEYFARTH wrote:
For the multi-index arrays, the release notes said that the simulator will not work correctly with multi index arrays, so be aware of that.

I only need to see which bytes at which position are changed, as soon the array is written. This should not have changed, so I assume the memory window is correct. Additional information shown in the window may be incorrect, but I don't need it.
Quote:
I do not understand what you mean by:
Quote:

The reason no code in the linked thread will work, is the wrong order of both indexes:
Code:
Dim RxBuffer(10,64) As Byte

A multidimensional array is represented internally by occupation of a linear and one dimensional array, only with the difference, that either the compiler knows at compile time how to solve fixed indexed addresses, or if indexes are variables, it integrates code to enable the micro controller to calculate the address at runtime.

A RxBuffer(10,64) consists of (see my 00 example) 10 linear connected bytes, then the next 10, a.s.o., till 64.
For the user it looks the same, whether access is to RxBuffer(1,1 to 64) or to RxBuffer(1 to 64, 1), but for the controller it makes a huge difference.

To access RxBuffer(1,1 to 64), the controller accesses byte 1, 11, 21, 31,..., 631.
It is the reason memcopy() fails, as it is not a connected, instead a clustered array.

If in contrary the array is dimensioned RxBuffer(64,10), 64 bytes are linear connected, so
RxBuffer(1 to 64, 1) actually accesses byte 1 to 64 within the said big linear array.
Memcopy() works well on this, and also SPIIN will work, if you only tell the right position within the one dimensional array, my code does this.
Quote:
Anyway, the overlay is interesting and may be something to look at for the near term

The overlay just represents the container, where the multidimensional array is hosted. It allows to drive around such roadworks, where the multidimensional array is not fully integrated yet.
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Mon Feb 02, 2015 12:39 am    Post subject: Reply with quote

MWS,
Thanks for the detailed explanation!
Quote:

A multidimensional array is represented internally by occupation of a linear and one dimensional array, only with the difference, that either the compiler knows at compile time how to solve fixed indexed addresses, or if indexes are variables, it integrates code to enable the micro controller to calculate the address at runtime.

A RxBuffer(10,64) consists of (see my 00 example) 10 linear connected bytes, then the next 10, a.s.o., till 64.
For the user it looks the same, whether access is to RxBuffer(1,1 to 64) or to RxBuffer(1 to 64, 1), but for the controller it makes a huge difference.

To access RxBuffer(1,1 to 64), the controller accesses byte 1, 11, 21, 31,..., 631.
It is the reason memcopy() fails, as it is not a connected, instead a clustered array.

If in contrary the array is dimensioned RxBuffer(64,10), 64 bytes are linear connected, so
RxBuffer(1 to 64, 1) actually accesses byte 1 to 64 within the said big linear array.
Memcopy() works well on this, and also SPIIN will work, if you only tell the right position within the one dimensional array, my code does this.
Quote:
Anyway, the overlay is interesting and may be something to look at for the near term

The overlay just represents the container, where the multidimensional array is hosted. It allows to drive around such roadworks, where the multidimensional array is not fully integrated yet.



That really helps to make sense of this. It appears that, for my purposes, using ary(64,10) makes the most sense.

Thank you VERY much! I am going to try this in between Super Bowl timeouts - er... thats not possible since the commercials are the best during the Super Bowl! Smile

Tim
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Mon Feb 02, 2015 10:49 am    Post subject: Reply with quote

MWS:

Quote:
A RxBuffer(10,64) consists of (see my 00 example) 10 linear connected bytes, then the next 10, a.s.o., till 64.

IMO RxBuffer(10,64) contains 10 arrays, each array is 64 bytes.
In the memory it is 64 linear connected bytes, then the next 64, a.s.o., till 10.

My demo Bascom hates multidim arrays so I am attaching an example from Avrstudio.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5915
Location: Holland

blank.gif
PostPosted: Mon Feb 02, 2015 11:21 am    Post subject: Reply with quote

I dont have a clue what avr studio has to do with this? i did not check if avr studio use the same order but i do know that one can lay out in different form. that is why it is not a good idea to include info about studio.

In bascom ofset calculation used : (index1-base) + ((index2-base) * elements1 ) + ((index3-base) * elements1*elements2) * dataLength
Thus ar(10,64) index1 is 10 and index2 is 64.
To calculate 1,1, the formula is 9 + 63*10, and that is what MWS explained. While the total memory used is the same, it makes a difference.
Since memcopy has a built in check in order to prevent memory corruption, it will not copy since the compiler finds 10 bytes maximum.
While i changed it, it is much better to dim 64,10 since that will give linear memory.

I will add this to the help since this is very important in practice.

_________________
Mark
Back to top
View user's profile Visit poster's website
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Mon Feb 02, 2015 11:34 am    Post subject: Reply with quote

Agreed.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Feb 02, 2015 2:55 pm    Post subject: Reply with quote

Visovian wrote:
IMO RxBuffer(10,64) contains 10 arrays, each array is 64 bytes.

Your definition is too unsharp, and further it is wrong in regards of Bascom.
You can address it like 10 * 64 or 64 * 10, and however you do, it will lead you always to the same certain memory cell.
The difference is internal organization, as soon you leave well paved roads, you need to know your example is 10 linear connected bytes and this 64 times.
At least that's the way Mark decided it to be this way in Bascom.

In this matter I do hard to understand, what a C-example debugged with AVR-Studio should tell me in context.
To learn from your snippet, the dimensioning is just the other way around compared to Bascom.

I further do not understand, why you don't compare Bascom's internal structure with a closer relationship, for example Visual Basic, or any another PC based Basic dialect.
Quote:
My demo Bascom hates multidim arrays so I am attaching an example from Avrstudio.

Yeah, as said, funny, an example in C-language.
I fully understand, if Bascom did not work, in case you have tried to feed it with C. Smile
Btw., the comparison you drew is like if your dentist is located on the 4th floor in a certain street and you expect to find dentists on 4th floors all over in other streets too. Very Happy
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Mon Feb 02, 2015 5:26 pm    Post subject: Reply with quote

Hello all,

The explanation MWS provided was very good and gave me, at least, the clarity needed. I had originally thought that 10,64 was correct, to arrays of 64 bytes, but that as I was shown was backwards. Really makes no difference which is which so long as it is known and works. This is why I changed my code to 64,10 yesterday.

Once this kinks are worked out of this new feature it will be very very nice indeed!
For me, my immediate needs are to use multi index for spiin commands and print commands without any code in between. That would be the cats meow!

Thanks again all - great job Mark, keep it up!
Tim
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5915
Location: Holland

blank.gif
PostPosted: Mon Feb 02, 2015 11:15 pm    Post subject: Reply with quote

About SPIIN, that does work.

Dim RxBuffer(10,64) As Byte
spiin Rxbuffer(idx, 1), Dta_len 'Rx_FIFO_Bytes

you pass the address of the first element you want to fill. But as the memory is filled in memory at a certain way as explained before, the result will be different as one might expect.
So the simplest way is to change the order of the indexes.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum