View previous topic :: View next topic |
Author |
Message |
richi-d
Joined: 28 Aug 2007 Posts: 106
|
How to save float numbers in EEPROM |
Posted: Fri Oct 08, 2010 6:20 am |
|
|
Hello,
I need to store a float number in EEPROM. I know it´s a 32bit value, so I need 4 Byte. But how can I do that? Casting the value to int32 will cause that for example 1.2345 (float) is 1 (int32) !
The Bit numbers in float are this:
00011001 00000100 00011110 01111111
This is what I tried, butt not working:
Code: |
STEIGUNG_M[0] = 1.2345;
DUMMY_5 = STEIGUNG_M[0];
DUMMY_0 = DUMMY_5 & 0x000000FF;
write_eeprom(6 , DUMMY_0);
delay_ms(5);
DUMMY_4 = DUMMY_5 & 0x0000FF00;
DUMMY_1 = DUMMY_4 >> 8;
write_eeprom(7 , DUMMY_1);
delay_ms(5);
DUMMY_4 = DUMMY_5 & 0x00FF0000;
DUMMY_2 = DUMMY_4 >> 16;
write_eeprom(8 , DUMMY_2);
delay_ms(5);
DUMMY_4 = DUMMY_5 & 0xFF000000;
DUMMY_3 = DUMMY_4 >> 24;
write_eeprom(9 , DUMMY_3);
delay_ms(5);
delay_ms(5); |
Last edited by richi-d on Fri Oct 08, 2010 6:36 am; edited 1 time in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Oct 08, 2010 6:30 am |
|
|
CCS manual page 311: 'How do I write variables to EEPROM that are not a byte?' |
|
|
richi-d
Joined: 28 Aug 2007 Posts: 106
|
|
Posted: Fri Oct 08, 2010 7:01 am |
|
|
Thank you, I tried this:
Code: | void WRITE_FLOAT_EEPROM( long int n, float data)
{
int p;
for (p = 0; p < 4 ; p++)
{
write_eeprom(p + n, *(((int 8 *)&data) + p));
}
}
float READ_FLOAT_EEPROM( long int n)
{
int q;
float data;
for (q = 0; q < 4; q++)
{
*(((int 8 *)&data) + q) = read_eeprom(q + n);
}
return(data);
} |
But always this error:
*** Error 58 "D:\Controller_Software\Universal_Switch+Kap\Kalibrierung.c" Line 6(29,30): Expecting a close paren
I want to use it in a PIC16F688 |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Oct 08, 2010 7:17 am |
|
|
As you have not posted all the code (or have you ?) it is hard to tell where line6 is, I assume it is:-
write_eeprom(p + n, *(((int 8 *)&data) + p));
Try removing the space between (int 8 *) so you have (int8 *)
Also the same applies to the next function. |
|
|
richi-d
Joined: 28 Aug 2007 Posts: 106
|
|
Posted: Fri Oct 08, 2010 7:20 am |
|
|
Thank you so much! It works. |
|
|
|