|
|
View previous topic :: View next topic |
Author |
Message |
John_Lintern
Joined: 30 Sep 2004 Posts: 14
|
How can I store and use an array in EEPROM ? |
Posted: Tue May 24, 2005 5:47 am |
|
|
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
|
|
Posted: Tue May 24, 2005 6:22 am |
|
|
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
|
|
Posted: Tue May 24, 2005 8:05 am |
|
|
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
|
|
Posted: Tue May 24, 2005 11:01 am |
|
|
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
|
|
Posted: Tue May 24, 2005 12:59 pm |
|
|
I wonder what that function does, is it for an EFI system? |
|
|
|
|
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
|