PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 11, 2004 12:40 pm |
|
|
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;
}
} |
|
|