473,480 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Saving type float number to EEPROM

8 New Member
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
6 5401
kreagan
153 New Member
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
Toe001
8 New Member
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
463 Recognized Expert Contributor
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
9,065 Recognized Expert Moderator Expert
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
9,065 Recognized Expert Moderator Expert
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
7,435 Recognized Expert Expert
Blech! Everything should be done in assembly.
Sep 16 '07 #7

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

Similar topics

4
4234
by: CodeRazor | last post by:
I'm unfamiliar with image manipulation using c#. How can i resize a jpg that currently exists in a file and save it resized as a new file. The examples i've found have been a bit misleading for...
5
2382
by: Donkey | last post by:
Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below: #include <stdio.h> const long double...
5
1887
by: Keith G | last post by:
VS2003 using VB.Net I want to be able to save drawing path data in a database. If I store the Points array for each GraphicsPath I can then reproduce (in this case) filled polygons at run time....
3
5396
by: Neena Paul | last post by:
Hi, I ve a below structured object TYPE in Oracle to calculate the Simple and Compound interest for an amount.. CREATE TYPE pnr_typ AS OBJECT ( principle NUMBER, ...
8
4147
by: Yu SONG | last post by:
Hi all, What would be the most efficient way to save an array of floats to a file (in text format)? At the moment, my code looks like: /* * Saving an array of floats to a file
10
2066
by: JoeC | last post by:
I am writing a game and all my game pieces are stored in a single vector of piece handles. I have the basics I can read and write char and number files but I am trying to do comthing more...
2
16106
by: cr55 | last post by:
I was wondering if anyone can help me with this programming code as i keep getting errors and am not sure how to fix them. The error code displayed now is error: C2228: left of '.rent' must have...
9
1640
by: =?Utf-8?B?TWlrZTk5MDA=?= | last post by:
I save a number in the table and want to get that number again, but the number I get has lower precision than I expect. For example, when I divide 10/3 I get 3.3333333333333335 if the variable is...
3
4069
by: phoenix1990 | last post by:
so i started learning avr-c programming a few weeks ago and i'm a little confused about using the eeprom so save values. i've been working a snake game for my project, and i need to save everything...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.