How to read/write a float from EEPROM?

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

This is probably more to do with my (lack of) understanding of C rather than the microprocessor (ATMega32) but:

Has anyone got any code examples of how to read/write a float to EEPROM?

I've got the basic read/writing to EEPROM for 8 bits working but my C knowledge is quite basic and I'm sure there are a series of conversions for converting a float into constituent bytes that can then be applied to the EEPROM 8 bits at a time.

I would be very appreciative if anyone can help or just point in the right direction.

John

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

Its compiler specific, but all the c compilers have eeprom routines. Its in the compiler manual fer sure...

Imagecraft compiler user

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

Depends on the compiler but doesn't something like:

float f = 3.141592;

eeprom_write_block(31, &f, 4);
eeprom_read_block(&f, 31, 4);

do the trick to store and then retrieve the 4 bytes of the float to EEPROM location 31 ?

Cliff

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

Hi Cliff, wouldn't be better to use sizeof(), for that matter?

float f = 3.141592; 

eeprom_write_block(31, &f, sizeof(f)); 
eeprom_read_block(&f, 31, sizeof(f)); 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all for your help - I will give this a go. I later realised that it ought to involve just copying memory blocks whereas originally I'd anticipated something much more complex.

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

This worked fine.
To get the compiler to allocate a place in EEPROM for a float I had to use:

#include 
// EEPROM variables
uint8_t  EEMEM eefloat[4];

I don't think there is an equivalent float_t type so I just made an array of 4 bytes.
Then a test function:

{
	float f1 = 1.234;
	float f2 = 0;
	float fr;
	eeprom_write_block(&f1, &eefloat, sizeof(f1)); 
	eeprom_read_block(&f2, &eefloat, sizeof(f2)); 
	fr = f1-f2;
}

Which showed that f1 was first written into EEPROM then used to update f2. Note that I had to look in eeprom.h to get the correct orientation of the arguments. I believe there is some discrepancy depending on what compiler/library you are using.

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

gingerbloke wrote:
I don't think there is an equivalent float_t type so I just made an array of 4 bytes.

Just use float instead

float  EEMEM eefloat;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Note that I had to look in eeprom.h to get the correct orientation of the arguments.

Why is there something wrong with the user manual?

http://www.gnu.org/savannah-chec...

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

clawson wrote:
Depends on the compiler but doesn't something like:

float f = 3.141592;

eeprom_write_block(31, &f, 4);
eeprom_read_block(&f, 31, 4);

do the trick to store and then retrieve the 4 bytes of the float to EEPROM location 31 ?

dear clawson,

I am not able read exact floating value store in EEPROM of ATmega16 @ 8MHz int.osc.
I am using,

eeprom_update_float.
eeprom_read_float.

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

Commonly, you put all the variables, strings, etc., you need to save to EEPROM into a C structure. They write the structure. Read-back into same type of structure.

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

Quote:
I am not able read exact floating value store in EEPROM of ATmega16 @ 8MHz int.osc.
I am using,
eeprom_update_float.
eeprom_read_float.
??
The folowing works well in the simulator.

#include 

float EEMEM flo;
float flo1;

//-------------------------------------

int main(void)
{

   eeprom_update_float(&flo, 3.14159);
   flo1 = eeprom_read_float(&flo);

   eeprom_update_float(&flo, 1.2345);
   flo1 = eeprom_read_float(&flo);

   while(1)
   {
   }
}