CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How do I #locate a 2-D array

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 11, 2004 12:40 pm     Reply with quote

Here is one example that I did in the past. It possibly could be
re-written in a more efficient way, but this example will give you
the main idea.
Code:

// bigarray.c
// This program shows how to make "wrapper" functions
// to allow access to an array larger than one ram bank.

//------------------------------------------------------
// INCLUDE FILES

#include "16F877.h"
#device *=16

//-------------------------------------------------------
// COMPILER AND CHIP CONFIGURATION DIRECTIVES

#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use Delay(Clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#zero_ram

//-------------------------------------------------------
// DEFINITIONS

#define ARRAY_1_SIZE  80
#define ARRAY_2_SIZE  80

#define TOTAL_ARRAY_SIZE (ARRAY_1_SIZE + ARRAY_2_SIZE)

//-------------------------------------------------------
// GLOBALS

char array_1[ARRAY_1_SIZE];
char array_2[ARRAY_2_SIZE];

//-------------------------------------------------------
// FUNCTION PROTOTYPES

char read_big_array(char index);
void write_big_array(char index, char value);


//================================
main()
{
char i;
char temp;

printf("Start test\n\r");

// To test the functions, we will fill the big array
// with numbers equal to the index.

for(i = 0; i < TOTAL_ARRAY_SIZE; i++)
   {
    write_big_array(i, i);
   }

//----------------------------
// Now read them back and check the results.

for(i = 0; i < TOTAL_ARRAY_SIZE; i++)
   {
    temp = read_big_array(i);

    if(temp != i)
       printf("Error:  Address %02x, Wrote %02x, Read %02x\n\r", i, i, temp);
   }

//----------------------------


printf("Test done");

while(1);
}

//=================================
// This function reads a byte from the specified index.

char read_big_array(char index)
{
char retval;

if(index < ARRAY_1_SIZE)   // Is index within 1st array ?
  {
   retval = array_1[index]; 
  }     
else if(index < TOTAL_ARRAY_SIZE)  // Is index within 2nd array ?
  {
   index -= ARRAY_1_SIZE;  // Then adjust index so it's within array 2.
   retval = array_2[index];
  }
else  // If not in either array, then return zero.  (We have to return something).
  {
   retval = 0;
  }

return(retval);
}

//------------------------------------------
// This function writes a byte to the specified index.

void write_big_array(char index, char value)
{

if(index >= TOTAL_ARRAY_SIZE)  // Don't allow writing outside of the array limits
   return;

if(index < ARRAY_1_SIZE)    // Is the index within array 1 ?
   {
   array_1[index] = value;
  }     
else         // If not, it must be within array 2. 
  {
   index -= ARRAY_1_SIZE;   // Adjust the index so it's within array 2.
   array_2[index] = value;
  }

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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


Powered by phpBB © 2001, 2005 phpBB Group