By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,207 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,207 IT Pros & Developers. It's quick & easy.

Saving type float number to EEPROM

P: 8
In a program for use with a micro, I have a variable of type float (real number) and wish to save it to EEPROM. I have a routine which writes a byte at a time to EEPROM at the location (address) I designate.

The problem comes in that I do not know how the variable (type float) is stored. I do know that a variable of type floats is a 32 bit value in the compiler I am using.

Is it as simple as writing each of the 4 bytes of the float (real), and later reading them back in? I feel I am missing something in this:

What I am imagining is something along these lines:

float ScaleFctr; // Real number
unsigned int i; // Start address in EEPROM where to save the real number

Write real to EEPROM:
I2CWrByte (EEPROM, i, st_ptr->ScaleFctr >> 24);
I2CWrByte (EEPROM, i+1, st_ptr->ScaleFctr >> 16);
I2CWrByte (EEPROM, i+2, st_ptr->ScaleFctr >> 8);
I2CWrByte (EEPROM, i+3, st_ptr->ScaleFctr & 0x00FF);

Read real from EEPROM:
I2CRdByte (EEPROM, i, &EEByteH);
I2CRdByte (EEPROM, i+1, &EEByteM);
I2CRdByte (EEPROM, i+2, &EEByteM2);
I2CRdByte (EEPROM, i+3, &EEByteL);
CalData.ScaleFctr = (EEByteH<<24) + (EEByteM<<16) + (EEByteM2<<8) + EEByteL;

Thanks .
Sep 5 '07 #1
Share this Question
Share on Google+
6 Replies


kreagan
100+
P: 153
In a program for use with a micro, I have a variable of type float (real number) and wish to save it to EEPROM.

The problem comes in that I do not know how the variable (type float) is stored. I do know that a variable of type floats is a 32 bit value in the compiler I am using.
Beware, you might need to use a compiler specific to your chip.
[QUOTE = Toe001]
Is it as simple as writing each of the 4 bytes of the float (real), and later reading them back in? I feel I am missing something in this:

What I am imagining is something along these lines:

float ScaleFctr; // Real number
unsigned int i; // Start address in EEPROM where to save the real number

Write real to EEPROM:
[/quote]

You can't write to EEPROM. EEPROM is Electroniclly Erasable Programmable Read Only Memory. This this the storage location for your code. You don't treat it like an accessable memory location.

Also, I suggest reading the chips manual because a lot information is chip dependent. I programmed (in assembly though) 2 different chips and each were very different. Their manuals both contained: example code, chip architecture, i/o port informtion, special registers, and so forth.

Just wondering, is this for a real time system?
Sep 5 '07 #2

P: 8
Thanks you for the reply.

I wrote the routine to write and read bytes from EEPROM using I2C, so know this works. Now I just want to save a real number, which is a 32 bit type on my compiler.

So I am asking if I can just write each of the four bytes (using shift etc to get at each in sequence) to EEPROM, and then later restore them to a variable of type float and expect to have recovered the original decimal value?

Thanks.
Sep 6 '07 #3

RRick
Expert 100+
P: 463
It should work as long as your compiler creates a float that your chip can understand. This is what Kreagan talked about.

If you compile this on a intel chip and try to run it on a risc chip, then it definitely won't work. This is in the realm of cross-platform compilation where you tell the compiler on machine A to create machine code for machine B. There are a lot of things to work out.
Sep 6 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
You can't write to EEPROM. EEPROM is Electroniclly Erasable Programmable Read Only Memory. This this the storage location for your code. You don't treat it like an accessable memory location.
Yes you can. Many many embedded applications have a chunk of EEPROM on board (often but not always on an I2C bus) for storing program configuration that needs to persist through a power down. The whole point is because it is EEPROM (electrically eraseable) you can erase it from the program.

It is EPROM and PROM that you can not write to but they are rather old technologies with many of todays embedded applications using FLASH memory to hold the program (and sometimes the configuration data too). You can even reprogram FLASH from with-in your program however you often have the constraint that you can not read and write to the same chip at the same time and FLASH generally does not support as many write cycles as EEPROM.
Sep 6 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
So I am asking if I can just write each of the four bytes (using shift etc to get at each in sequence) to EEPROM, and then later restore them to a variable of type float and expect to have recovered the original decimal value?
You will have trouble using shift to get the bytes. You will probably need to do something a bit sneaky, a bit bodgy and a bit non portable like take a pointer to your floating point variable (i.e. find it's address in ram) and cast it to a char pointer and then use that as if it was an array of 4 characters.

But you will have to be sure that you platform supports the cast of float * to char * which is not guaranteed by the C/C++ standards.

Another option is to declare a union of an array of 4 char with a float then you can assign the float and read the char.
Sep 6 '07 #6

drhowarddrfine
Expert 5K+
P: 7,435
Blech! Everything should be done in assembly.
Sep 16 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.