Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   store 16bit values in 8bit eeprom (http://www.velocityreviews.com/forums/t743748-store-16bit-values-in-8bit-eeprom.html)

 cerr 02-17-2011 06:39 PM

store 16bit values in 8bit eeprom

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[i]->value)));
write_eeprom((i*2)+1,*((int8*)&(data[i]->value) + 1));
}
}
//------------------------------------------------------------------------------

{
int8 i;

for (i = 0; i<VARNUM; i++){
data[i]->value=0x0000;
}
}
May anyone point me into the right direction? We can assume that
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

 Jens Thoms Toerring 02-18-2011 12:16 AM

Re: store 16bit values in 8bit eeprom

cerr <ron.eggler@gmail.com> wrote:
> 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[i]->value)));
> write_eeprom((i*2)+1,*((int8*)&(data[i]->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[i]->value=0x0000;
> }
> }

And that I would change to something like

void
{
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
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

 BartC 02-18-2011 01:42 AM

Re: store 16bit values in 8bit eeprom

"cerr" <ron.eggler@gmail.com> wrote in message
> 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[i]->value)));
> write_eeprom((i*2)+1,*((int8*)&(data[i]->value) + 1));
> }
> }
> //------------------------------------------------------------------------------
>
> {
> int8 i;
>
> for (i = 0; i<VARNUM; i++){
> data[i]->value=0x0000;
> }
> }
> May anyone point me into the right direction? We can assume that
> 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

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];

}

}

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