store 16bit values in 8bit eeprom

C

cerr

Hi There,

I have an array of 16 16bit values that i need to store in an 8bit
eeprom.
It seems to be fine but I read back all weird values.
I tried to do it this way:
void WriteToEEPROM(eevalues* data)
{
int8 i=0;
for (i=0; i<VARNUM; i++){
write_eeprom((i*2),*((int8*)&(data->value)));
write_eeprom((i*2)+1,*((int8*)&(data->value) + 1));
}
}
//------------------------------------------------------------------------------

void ReadFromEEPROM(eevalues* data)
{
int8 i;

for (i = 0; i<VARNUM; i++){
data->value=0x0000;
*((int8*)&(data->value)) = read_eeprom((i*2));
*((int8*)&(data->value)+1) = read_eeprom((i*2)+1);
}
}
May anyone point me into the right direction? We can assume that
write_eeprom() and read_eeprom() work just fine and expect the address
to write to/read from (starting at 0) and the value resp. it returns
the value as a int8.

Thanks for hints or suggestions!
Ron
 
J

Jens Thoms Toerring

cerr said:
I have an array of 16 16bit values

You seem to have, at least if your code is to be be-
lieved, an array of pointers to what may be 16-bit
values.
that i need to store in an 8bit eeprom.
It seems to be fine but I read back all weird values.
I tried to do it this way:
void WriteToEEPROM(eevalues* data)
{
int8 i=0;
for (i=0; i<VARNUM; i++){
write_eeprom((i*2),*((int8*)&(data->value)));
write_eeprom((i*2)+1,*((int8*)&(data->value) + 1));
}
}


What type has 'data[ i ]->value'? Without knowing that it's
imposible to even start guessing. If it's int16 then things
look more or less ok, but otherwise...

On a different note: I would write that more like

void
WriteToEEPROM( const eevalues * data )
{
size_t i;
for ( i = 0; i < VARNUM; i++ )
{
write_eeprom( 2 * i, data[ i ]->value & 0xFF );
write_eeprom( 2 * i + 1, data[ i ]->value >> 8 );
}
}

(assuming that the lower byte is to be written to the lower
addess of the EEPROM and the upper byte to the higher address
- just switch the second arguments of the two write_eeprom()
calls otherwise). That way you aren't dependent on the endi-
aness of the machine this is running on (unless, of course,
if that was exactly what you were intending).
void ReadFromEEPROM( eevalues* data )
{
int8 i;
for (i = 0; i<VARNUM; i++){
data->value=0x0000;
*((int8*)&(data->value)) = read_eeprom((i*2));
*((int8*)&(data->value)+1) = read_eeprom((i*2)+1);
}
}


And that I would change to something like

void
ReadFromEEPROM( eevalues * data )
{
size_t i;
for ( i = 0; i < VARNUM; i++ )
data[ i ]->value = read_eeprom( 2 * i )
| ( read_eeprom( 2 * i + 1 ) << 8 );
}

for the same reason. Beside, I personally find it that
way a bit easier to understand.
May anyone point me into the right direction? We can assume that
write_eeprom() and read_eeprom() work just fine and expect the address
to write to/read from (starting at 0) and the value resp. it returns
the value as a int8.

If that's the case then I don't see any real reason why it
shouldn't work in principle. But then hardware can be more
demanding than what you're doing. Perhaps after writing
to the EEPROM you need to send another "signal" to tell
it that "I meant it" and it is then to "burn" the data
in? I haven't dealt with EEPROMs but other hardware, and
sometime things are a bit more complicated than it looks
at a first glance, so carefully re-reading the specs may
be required when things don't work as expected.

Regards, Jens
 
B

BartC

cerr said:
Hi There,

I have an array of 16 16bit values that i need to store in an 8bit
eeprom.
It seems to be fine but I read back all weird values.
I tried to do it this way:
void WriteToEEPROM(eevalues* data)
{
int8 i=0;
for (i=0; i<VARNUM; i++){
write_eeprom((i*2),*((int8*)&(data->value)));
write_eeprom((i*2)+1,*((int8*)&(data->value) + 1));
}
}
//------------------------------------------------------------------------------

void ReadFromEEPROM(eevalues* data)
{
int8 i;

for (i = 0; i<VARNUM; i++){
data->value=0x0000;
*((int8*)&(data->value)) = read_eeprom((i*2));
*((int8*)&(data->value)+1) = read_eeprom((i*2)+1);
}
}
May anyone point me into the right direction? We can assume that
write_eeprom() and read_eeprom() work just fine and expect the address
to write to/read from (starting at 0) and the value resp. it returns
the value as a int8.


Have you tried just write_eeprom()/read_eeprom() to see whether it properly
reads and writes bytes?

Eg. read_eeprom(10) and display the result.

Now write_eeprom(10,0x0) (or other test values), and see whether
read_eeprom() returns that same value. Then see perhaps whether a simple
sequence of such bytes can be read and written.

Your 8-bit/16-bit code looks messy. Perhaps temporarily replace
read/write_eeprom() with a couple of test functions that store a byte into
an array, and see what happens:

unsigned char dataarray[1000];

void write_byte(int address,int value){
dataarray[address]=value;
}

int read_byte(int address){
return dataarray[address];
}

Then this will separate the problems of eeprom access, from any errors in
your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top