CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

store double in eeprom

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

store double in eeprom
PostPosted: Thu Feb 17, 2011 11:36 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 1:48 pm     Reply with quote

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: 19520

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 3:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 3:50 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 4:55 pm     Reply with quote

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: 19520

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 2:57 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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