store 16bit values in 8bit eeprom

Discussion in 'C Programming' started by cerr, Feb 17, 2011.

  1. cerr

    cerr Guest

    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
     
    cerr, Feb 17, 2011
    #1
    1. Advertising

  2. cerr <> 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->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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Feb 18, 2011
    #2
    1. Advertising

  3. cerr

    BartC Guest

    "cerr" <> wrote in message
    news:...
    > 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.

    --
    Bartc
     
    BartC, Feb 18, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramon

    Accessing PIC's EEPROM

    Ramon, May 4, 2004, in forum: C Programming
    Replies:
    3
    Views:
    1,459
    Mark A. Odell
    May 5, 2004
  2. humble
    Replies:
    0
    Views:
    909
    humble
    Oct 28, 2006
  3. george jetson

    Embeddable Pcode compiler/interpreter Serial EEPROM

    george jetson, Jul 7, 2004, in forum: C Programming
    Replies:
    1
    Views:
    472
    Grumble
    Jul 9, 2004
  4. Daniele M.
    Replies:
    2
    Views:
    424
    Daniele M.
    Mar 6, 2008
  5. cerr

    16bit values into 8bit eeprom

    cerr, Feb 17, 2011, in forum: C Programming
    Replies:
    1
    Views:
    388
    Ike Naar
    Feb 17, 2011
Loading...

Share This Page