Float value in eeprom

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need to store and recover a float value from eeprom. I'm looking for any good suggestions on how that should be done.

Thanks,

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you try to use the search function of the forum? If I search for "float eeprom" I find some posts, but I didn't look into them.

Volkmar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use the block read and write functions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
double * EEPROM_read_double(uint16_t uiAddress) 
{ 
   char * temp="  "; 
   double * retr = temp; 
   for(int i=0;i<=3;i++){ 
      *(temp+i) = EEPROM_read(uiAddress+i); 
   } 
   return(retr); 
} 

void EEPROM_write_double(uint16_t uiAddress, volatile double * ucData) 
{ 
   char * temp = ucData; 
   for(int i=0;i<=3;i++){ 
      EEPROM_write(uiAddress+i,*(temp+i)); 
   } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nysan wrote:

...
void EEPROM_write_double(uint16_t uiAddress, volatile double * ucData) 
{ 
   char * temp = ucData; 
   for(int i=0;i<=3;i++){ 
      EEPROM_write(uiAddress+i,*(temp+i)); 
   } 
}


Who told you that double is 4 bytes long and EEPROM address 16 bit integer? :shock:
This is true only by accidence and this kind of assumptions should be avoided at all.
It is not only theoretical problem - there is discussion about implementing "true" double operations for AVR GCC/libc and double type wil not be longer 4 bytes long..

Hint: sizeof operator.

Hint 2: block operations are usually more faster and compact than writing/reading byte after byte.

Regards,