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 CCS Technical Support

How can I store and use an array in EEPROM ?

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



Joined: 30 Sep 2004
Posts: 14

View user's profile Send private message Send e-mail

How can I store and use an array in EEPROM ?
PostPosted: Tue May 24, 2005 5:47 am     Reply with quote

I've been using arrays which I have been storing RAM.

Below is an example of how I initialised my arrays in RAM...

Code:

int ShiftSchedCold_1to2[2][4] = {{20,20,52,52},
                                             {0,25,127,255}};

int ShiftSchedCold_2to3[2][5] = {{68,68,92,180,180},
                                         {0,66,120,120,255}};

int ShiftSchedCold_3to4[2][3] = {{12,12,28},
                                     {0,132,132}};


However, I need to store more arrays but I am running out of RAM.

The arrays are always a 2 by x, where x would be no more than 10.

How can I store and use these arrays in EEPROM ?

I tried storing the arrays in ROM (program memory) by initialising them as shown below...

Code:

const int ShiftSchedCold_1to2[2][4] = {{20,20,52,52},
                                             {0,25,127,255}};

const int ShiftSchedCold_2to3[2][5] = {{68,68,92,180,180},
                                         {0,66,120,120,255}};

const int ShiftSchedCold_3to4[2][3] = {{12,12,28},
                                     {0,132,132}};


However, this caused problems because of the function I use to point to the arrays.

unfortunately you cannot use pointers to an array in ROM (because ROM is 14-bit).

The arrays are used in the following function...

Code:

// Call to the function
CalcSchedule(ShiftSchedCold_1to2[0],sizeof(ShiftSchedCold_1to2)/2);


Code:

// Function CalcSchedule
int CalcSchedule(int *SchedPointer, int Size)
{
int16 x;
float grad;
int TPSMIN,TPSMAX,RPMMIN,RPMMAX;
for (x=0;x<(Size);x++)         
{
TPSMIN=*(SchedPointer+Size+x);
TPSMAX=*(SchedPointer+Size+x+1);   
RPMMIN=*(SchedPointer+x);
RPMMAX=*(SchedPointer+x+1);      
if ((TPS>=TPSMIN)&&(TPS<=TPSMAX))
{
if (RPMMIN==RPMMAX)
{
return RPMMIN;
}
else
{
grad= ((float)TPS-(float)TPSMIN) / ((float)TPSMAX-(float)TPSMIN);
return ((RPMMAX-RPMMIN)*grad) + RPMMIN;      
}
}
}
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue May 24, 2005 6:22 am     Reply with quote

Store the arrays in ROM.
Copy an array into RAM buffer and pass a pointer to your function.
John_Lintern



Joined: 30 Sep 2004
Posts: 14

View user's profile Send private message Send e-mail

PostPosted: Tue May 24, 2005 8:05 am     Reply with quote

Mark wrote:
Store the arrays in ROM.
Copy an array into RAM buffer and pass a pointer to your function.


Thanks Mark,

I'm trying to do what you suggested.

I've now stored all my arrays of data in ROM (program memory).

I want to then copy the appropriate array into RAM.

But this gives me a problem because the array sizes are all different.

I've put the arrays into ROM using the following code....

Code:

// ShiftSched when temperature is Cold
BYTE CONST ShiftSchedCold_1to2[2][4] = {{20,20,52,52},
                                             {0,25,127,255}};

BYTE CONST ShiftSchedCold_2to3[2][5] = {{68,68,92,180,180},
                                         {0,66,120,120,255}};

BYTE CONST ShiftSchedCold_3to4[2][3] = {{12,12,28},
                                     {0,132,132}};

// ShiftSched when temperature is Hot
BYTE CONST ShiftSchedHot_1to2[2][6] = {{68,68,92,180,180,210},
                               {0,66,120,120,185,255}};

BYTE CONST ShiftSchedHot_2to3[2][2] = {{16,60},
                              {0,255}};

BYTE CONST ShiftSchedHot_3to4[2][4] = {{12,12,28,28},
                             {0,132,132,255}};



In the example above, there are 3 arrays for when the temperature is cold and 3 arrays for when the temperature is hot, but the size of the arrays vary.

The size is always a 2 by x array.

In the example above, x could be 2, 3, 4, 5 or 6.

I want to store the approproate arrays in RAM using the variables...

Code:

int ShiftSched_1to2[2][];
int ShiftSched_2to3[2][];
int ShiftSched_3to4[2][];


For example, if the temperature is cold, then...

ShiftSched_1to2 = ShiftSchedCold_1to2
ShiftSched_2to3 = ShiftSchedCold_2to3
ShiftSched_3to4 = ShiftSchedCold_3to4

If the temperature is hot, then...

ShiftSched_1to2 = ShiftSchedHot_1to2
ShiftSched_2to3 = ShiftSchedHot_2to3
ShiftSched_3to4 = ShiftSchedHot_3to4

I was going to use a for loop to copy the array from ROM into RAM, using code something like this...

Code:

for (x=0;x<2;x++)
{
   for (y=0;y<sizeof(ShiftSched_1to2Cold);y++)
   {
   ShiftSched_1to2[x][y]=ShiftSched_1to2Cold[x][y]
   }
}


The problem is, that the size of the arrays in RAM depend on the size of the array in ROM.

And the size of the arrays in ROM all vary.

The syntax to initialise the arrays in RAM...

Code:

int ShiftSched_1to2[2][];


gives an error when compiling, because it needs to know the exact size of the array.

But the size varies, so how can I get around this ?


Last edited by John_Lintern on Tue May 24, 2005 8:08 am; edited 1 time in total
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue May 24, 2005 11:01 am     Reply with quote

Make the array big enough to hold the largest or use malloc() function.

Depending on what you are trying to do, you could just pass in an ID to use instead of a pointer. Inside the function, you would use a switch statement. One case for the hot values and the other for the cold values.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Tue May 24, 2005 12:59 pm     Reply with quote

I wonder what that function does, is it for an EFI system?
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