View previous topic :: View next topic |
Author |
Message |
FFT
Joined: 07 Jul 2010 Posts: 92
|
Float in eeprom using #rom getenv("EEPROM_ADDRESS" |
Posted: Thu Nov 18, 2010 10:01 am |
|
|
Hi All,
My question can be defined as following:
Code: | #define eeoffset 0x10
#define ResistanceHigherCurrent 1.8
#rom getenv("EEPROM_ADDRESS")+eeoffset = {"string"} |
In the code above, how to write into eeprom the 1.8 float value as 32-bit float instead of the "string"?
Best wishes. |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
Re: Float in eeprom using #rom getenv("EEPROM_ADDRESS&q |
Posted: Thu Nov 18, 2010 11:33 am |
|
|
FFT wrote: |
In the code above, how to write into eeprom the 1.8 float value as 32-bit float instead of the "string"? |
the EEPROM internal construction is 8 bits (1 byte); in these 8 bits you can store a char or a int8 value. and obviously, many char's can be concatenated, and the result is a string. you have already seen how this works.
but to store a float in EEPROM you must break it down into 8bit "chunks". there are many ways to approach this, and it is somewhat application dependent. one simplistic (and brute force) option is in fact to continue to store the string, and convert the string to a float as needed in your program. look into the C language's atof() and sscanf() functions.
otherwise, you will have to determine a method to take your float value (bounded, for many reasons) and represent it as a structure of 8bit values. there are several practical ways to do this.
jds-pic |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Nov 18, 2010 12:51 pm |
|
|
(apparently useless post deleted) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D
Last edited by bkamen on Thu Nov 18, 2010 3:43 pm; edited 1 time in total |
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Thu Nov 18, 2010 3:38 pm |
|
|
Hi and thanks for the answers but these are not the solution.
I think you misunderstood the question.
I ask how to initialize float data in eeprom (embedded in hex file) for the programming moment. I already can read and write float values at run-time. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Nov 18, 2010 3:45 pm |
|
|
you don't have to.
When you initialize the variable, there will be code to do it for you.
so when you say:
float var=1.8;
there will be code to initialize it to the desired value.
If that's not what you want, then you are not fully explaining what/why you are trying to accomplish and should consider elaborating your situation/goal a little more.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
FFT
Joined: 07 Jul 2010 Posts: 92
|
|
Posted: Fri Nov 19, 2010 8:28 am |
|
|
@PCM_Programmer,
Thanks so much, this is what I want.
But, what is the difference between 32-bit float and int32?
Code: | #define putLong(value) {value,(value>>8),(value>>16),(value>>24)}
#rom 0x0f000 = putLong(0x12345678) |
Why that code cannot be used for floats too?
Actually we don't care about the mantissa, sing... , just we have 4 bytes to store. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 19, 2010 4:00 pm |
|
|
It doesn't work. A method is needed that can extract the 4 bytes that
make up a floating point number, and do it at compile time. I don't
immediately know how to do this.
If a method doesn't exist, CCS would have to be requested to add the
feature. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Fri Nov 19, 2010 10:03 pm |
|
|
Why not just let the program initialize eeprom when it is first run?
You can read a 'flag' byte of eeprom at powerup. If it is not 0x00, for instance, then you write your initialization values to eeprom and write 0x00 to your flag byte. Future power ups will not re-write it.
Just a thought |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Sat Nov 20, 2010 4:08 am |
|
|
The problem is that on small chips, this is a waste of space.
It is what I do.
I declare my eeprom data as a structure, containing all the stuff I want to store, and with an extra 'marker' byte at the start, initialised with something like 0xA5. The structure is declared in the main code with initialisation values. Then on boot up, I simply test if the 'marker' matches the marker in ROM. If not, I copy the entire structure into the ROM, while if it does match (implying the data has been used before), I instead read it back.
It is a silly weakness really of the compiler, with it happily accepting #ROM int16, or #ROM int8 declarations, but not accepting #ROM float, or #ROM int32.
Best Wishes |
|
|
|