View previous topic :: View next topic |
Author |
Message |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
store double in eeprom |
Posted: Thu Feb 17, 2011 11:36 am |
|
|
Hi There,
I'm having troubles storing an array of 16 16bit values to the eeprom and read it back.
I read all weird values :(. My functions look like this:
It's one value per iteration, VARNUM is set to 16.
Code: |
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));
}
}
//------------------------------------------------------------------------------
void ReadFromEEPROM(eevalues* data)
{
int8 i;
for (i = 0; i<VARNUM; i++) {
*((int8*)&data[i]->value) = read_eeprom((i*2));
*((int8*)&(data[i]->value)+1) = read_eeprom((i*2)+1);
}
}
|
What am I doing wrong here?
Thank you for assistance!
Ron |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Feb 17, 2011 1:48 pm |
|
|
Honestly, instead of fluffing through all that,
I usually build myself a structure with all my stuff in it that I want to save.
Like:
Code: |
struct {
unsigned int a;
unsigned long b;
unsigned int32 c;
} config;
and then I basically loop around a pointer to config and save every byte to EEPROM....
As long as you load and save config, byte by byte in the same order, your structure is golden.
|
_________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Thu Feb 17, 2011 3:34 pm |
|
|
Key fault though is in the pointer/array handling.
If 'data' is a pointer, then 'data[i]', is the 'i'th value pointed _to_, not a pointer.
To access the address of the element inside the array, would need:
&(data[i].value)
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Feb 17, 2011 3:50 pm |
|
|
Ttelmah wrote: | Key fault though is in the pointer/array handling.
If 'data' is a pointer, then 'data[i]', is the 'i'th value pointed _to_, not a pointer.
To access the address of the element inside the array, would need:
&(data[i].value)
|
Is that for me or him? (I don't access the array quite like that when saving).
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Thu Feb 17, 2011 4:55 pm |
|
|
Well I get it going by just taking every double apart and saving byte by byte which works fine.
Thanks for your hints & suggestions! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Fri Feb 18, 2011 2:57 am |
|
|
bkamen wrote: | Ttelmah wrote: | Key fault though is in the pointer/array handling.
If 'data' is a pointer, then 'data[i]', is the 'i'th value pointed _to_, not a pointer.
To access the address of the element inside the array, would need:
&(data[i].value)
|
Is that for me or him? (I don't access the array quite like that when saving).
-Ben |
That's for the original poster, who is using 'data[i]', as if it is a pointer, by using the -> construct. No wonder it doesn't work.....
Best Wishes |
|
|
|