Searching \ for 'Help with arrays' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=help+with+arrays
Search entire site for: 'Help with arrays'.

Truncated match.
PICList Thread
'Help with arrays'
1998\10\21@165615 by Jon Petty

picon face
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

1998\10\21@173155 by Chip Weller

flavicon
face
Jon Petty wrote:


>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.

Good Luck, Chip

1998\10\21@174239 by evan

picon face
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:

_row   macro    byte1, byte2, byte3
    retlw   byte1
    retlw   byte2
    retlw   byte3
    endm

 _row  h'05',  h'ff',  h'7F'
 _row    'd',    'e', d'232'
 _row    'X', d'254',  h'CC'
 _row  h'43',    '!',  d'99',

-Ed V.
Agile Controls
spam_OUTevanTakeThisOuTspamdirect.ca

{Quote hidden}

1998\10\21@194515 by Jon Petty

picon face
In a message dated 10/21/98 2:31:45 PM US Mountain Standard Time,
.....chip_wellerKILLspamspam.....FITCH.COM writes:

<< 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. >>

Thanks alot I'll study your example tonight

Jon

1998\10\21@201637 by Mike Keitz

picon face
On Wed, 21 Oct 1998 16:53:10 EDT Jon Petty <EraseMEPHXSYSspam_OUTspamTakeThisOuTAOL.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:

0,0     1,0     2,0     3,0
0,1     1,1     2,1     3,1
0,2     1,2     2,2     3,2
0,3     1,3     2,3     3,3

or

0       1       2       3
4       5       6       7
8       9       10      11
12      13      14      15

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]

1998\10\22@002205 by Rob Wentworth
flavicon
face
On Wed, 21 Oct 1998 16:53:10 EDT Jon Petty <PHXSYSspamspam_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.

1998\10\23@093848 by Caisson

flavicon
face
> Van: Jon Petty <@spam@PHXSYSKILLspamspamAOL.COM>
> Aan: KILLspamPICLISTKILLspamspamMITVMA.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



Greetings,
  Rudy Wieser

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...