>I want to construct a 2d array. My earlier post received no response.
The info
>in Programming and Customizing the PIC micro is limited. Any examples?
>
>Jon
Jon,
The following should allow access to a 16x16 2D constant array of bytes.
If you need words you can implement it using two byte arrays. You would
set RowIndex to a value between 0 and 15 (0x0F) and ColumnIndex to a
valid between 0 and 15 and then call this routine. The code assumes the
high nibble of both indexes are clear. You must completely fill in the
retlw table to have all 256 elements. The following is NOT tested.
Get2DTable
movlw high Array ; preset PCLATH with array index.
movwf PCLATH ; to allow indirect access to correct page.
swapf RowIndex,w ; get row 0..15 into high nibble.
iorwf ColumnIndex,w ; get column 0..15 into low nibble.
addlw low Array ; compute low PC address.
skpnc ; if no carry on above
incf PCLATH,f ; else update high address.
movwf PCL ; set PC, does indirect jump into our table.
Array ; (no boundry requirements)
retlw 0 ; define first row, 16 column elements.
retlw 1 ; place byte data to return in this locations.
retlw 2
retlw 3
...
retlw 15
retlw 16 ; define second row, 16 column elements.
...
Continue until all array is filled, 256 elements for 16x16 2D array.
Assuming you're using assembly, just make a macro to contruct a 1D array,
then use the macro to construct each row of your array. Remember that a 2D
array is nothing more than an array of 1D arrays. Here's a little 3 x 4
array made with MPASM:
> -----Original Message-----
> From: pic microcontroller discussion list
> [.....PICLISTKILLspam@spam@MITVMA.MIT.EDU]On Behalf Of Jon Petty
> Sent: Wednesday, October 21, 1998 1:53 PM
> To: PICLISTKILLspamMITVMA.MIT.EDU
> Subject: Help with arrays
>
>
> I want to construct a 2d array. My earlier post received no
> response. The info
> in Programming and Customizing the PIC micro is limited. Any examples?
>
> Jon
>
The following should allow access to a 16x16 2D constant array of bytes.
If you need words you can implement it using two byte arrays. You would
set RowIndex to a value between 0 and 15 (0x0F) and ColumnIndex to a
valid between 0 and 15 and then call this routine. The code assumes the
high nibble of both indexes are clear. You must completely fill in the
retlw table to have all 256 elements. The following is NOT tested. >>
On Wed, 21 Oct 1998 16:53:10 EDT Jon Petty <EraseMEPHXSYSspam_OUTTakeThisOuTAOL.COM> writes:
>I want to construct a 2d array. My earlier post received no response.
>The info
>in Programming and Customizing the PIC micro is limited. Any examples?
Since memories are a linear arrangement of bytes, it is necessary to
store 2D and higher arrays internally like 1D arrays. For example if you
have an array A[16,16] and want to find A[x,y], the processor treats the
access like an A[256] array and computes the index for each access as
x+y*16. It is necessary to know one of the dimensions beforehand in
order to find the proper value in memory.
As a further example here's two ways to look at a 4 x 4 array:
which is stored in memory in a straight line order (0,1,2,3...14,15). It
is necessary to remember it is a n x 4 array in order to make an access
in a 2D form.
In the case of a 16x16 array on a PIC, the swapf instruction is very
useful to compute x*16. Others have already posted some suitable code
but without much explanation of the underlying concept.
___________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
or call Juno at (800) 654-JUNO [654-5866]
On Wed, 21 Oct 1998 16:53:10 EDT Jon Petty <PHXSYSspam_OUTAOL.COM> writes:
>I want to construct a 2d array. My earlier post received no response.
>The info
>in Programming and Customizing the PIC micro is limited. Any examples?
>
For tables that are not power-of-two dimensions, I use a 1-dimension table
that points to the start or each row of the 2-D table, to avoid multiplies
when accessing the 2-D table.
----------------------------------
Rob Wentworth, Software Developer
612/757-5829 fax 612/757-4792
What we attend to becomes reality.
> Van: Jon Petty <@spam@PHXSYSKILLspamAOL.COM>
> Aan: KILLspamPICLISTKILLspamMITVMA.MIT.EDU
> Onderwerp: Help with arrays
> Datum: woensdag 21 oktober 1998 22:53
>
> I want to construct a 2d array. My earlier post received no response. The
info
> in Programming and Customizing the PIC micro is limited. Any examples?
Hello Jon,
I take you do know how to implement a 1d array. Now take two 1d array's
behind each other. The first array is called Array0, the second is called
Array1. To acces the cell's of Array0 you just index them. first cell is at
offset 0, the second cell is at offset 1, and so on. Now access the first
cell of Array1. Where is it in relation to the first cell of array0 ?.
well ... :-) Ok, you've found the answer ! The index to the first cell of
Array1 is equal to the _length_ of Array0 ! If we agree on making all 1d
array's the same length, the answer for array2 is : (Length of 1d array)
times (array number). Now to add the index to an individual cell within
an unknown 1d array : (Length of 1d array) times (array number) + (offset
into a 1d array).
Now call (array number) "Y" and (index into a in array) "X". Call the
number of aray's the "height" and (length of 1d array) "width". The
formula now changes to :
Cell-Index = Width * Y + X
That's all ! What we have done here is to convert a 2d representation to
a 1d representation.
Another way to look at it:
Just think of it of having a lot of little boxes, for example 6 wide, 8
high. Take the second row of boxes, and place them to the right of the
first line. keep on doing that untill all 8 rows are placed behind each
other. Now you dont have 6 * 8 2D array, but a single 48 cells 1D array.
The first box of the second row is now at position 7, the first of the
third row at position 13, and so on ... The 3th box of the 5th row is at
offset 5 * 6 + 3 is 33. Due to the fact that humans call the first item in
a row item number One, it's called the 34th box :-)
By the way, this works for any number of dimentions.
Cell-Index = (size of 2d array) * Z + (size of 1d array) * Y + X