tutorial: handling two dimensional arrays in C18 Description: In previous tutorials, it was explained how to use pointers to handle arrays or variables, and specifically for transferring an array as a function parameter. In the project of Bolt system controlling RFID tags, we developed a practical application of handling one dimensional arrays in C. In this tutorial you will see how to handle two dimensional arrays using C18 language. The examples were developed with C18 v.3.40 Compiler and MPLAB IDE v.8.63. We will use the MPLAB IDE simulator to facilitate the undersanding of this topic, and finally an application program using the Bolt 18F2550 system with an LCD will be explained to show the management of two dimensional arrays. Suppose you want to create an array with 35 data, organized in 5 rows of 7 data each. Consider the example in the table below, where data are defined in ASCII, decimal and hexadecimal formats. The matrix would take the following form: matriz[i,j] where i is the row number and j the column number. For example: matriz[4,1]=0XA4
Handling two dimensional arrays in C: In C, it is essential the use of pointers to reference all array elements. The figure below shows the data structure for the proposed matrix in the previous example. One dimensional arrays s0[ ] ... s4 [ ] each contain 7 values that makes finally 35 items of the matrix in the proposed example. The general structure of the matrix is shown below:
There is an array of pointers in this example call "masterarray[ ]". To define this array in the program we proceed as follows: const rom char *masterarray[ ]={s0,s1,s2,s3,s4}; S0...S4 pointers are 16 bit values that are stored in RAM memory locations. But its content is pointing to addresses in ROM memory (code) which is where the elements of the matrix are stored. When performing the simulation test program, shown in the window below, can be seen the values of these pointers in RAM. To access individual array elements of the matrix, indirect addressing should be used, ie with subscripts added to the value of the pointers. TEST PROGRAMS FOR SIMULATION IN MPLAB IDE: Based on the structure shown above, the following program defines first data of the matrix of 5 rows and 7 columns that are stored in memory and later uses a function that retrieves the element [4,1] of the matrix. The function used in the program to read the indicated item is: z=matriz(i,j); Where the element value is assigned to the variable z. This function can be used in any other program for handling two dimensional arrays, having as input parameters values [i, j] (row, column) of the matrix. Test program for simulation is the following: C18-BOLT-TWO-DIMENSIONAL-ARRAY-2.c To make the simulation, you must first compile the program, and assign a breakpoint on the line shown in the window below (red circle). Reset the simulator program and then click the option "run". The simulation will stop at the breakpoint (green arrow). The variable z and pointers S0 ... S4 can be seen in the watch window of variables and RAM. In this test example, variable z takes the value of the matrix element [4,1], which is 0xA4.
|