CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to store float in EEPROM??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Falgellus



Joined: 12 Feb 2006
Posts: 8

View user's profile Send private message

How to store float in EEPROM??
PostPosted: Wed Mar 29, 2006 9:08 am     Reply with quote

If you see the ccs help you can find this:

-----------------------------------------------------------------------------
How do I write variables to EEPROM that are not a byte?

The following is an example of how to read and write a floating point number from/to EEPROM. The same concept may be used for structures, arrays or any other type.


· n is an offset into the eeprom.
· For floats you must increment it by 4.
· For example if the first float is at 0 the second
· one should be at 4 and the third at 8.

WRITE_FLOAT_EXT_EEPROM(long int n, float data) {
int i;

for (i = 0; i < 4; i++)
write_eeprom(i + n, *(&data + i) ) ;
}

float READ_FLOAT_EXT_EEPROM(long int n) {
int i;
float data;

for (i = 0; i < 4; i++)
*(&data + i) = read_eeprom(i + n);

return(data);
}
-----------------------------------------------------------------------------

I tried it but seems to have problems in this expression:

*(&data + i) = read_eeprom(i + n);


I tryed doing test for a lot of time but the best I can get is these functions that runs well:


WriteFloatToEEprom( byte Addr, float data )
{
int32 a;
a = (int32) ( data * (float)100 );

Write_EEPROM ( Addr , (byte) ( a & 0x000000FF ) );
Write_EEPROM ( Addr +1, (byte) (( a & ( 0x0000FF00 ) ) >> 8 ) );
Write_EEPROM ( Addr +2, (byte) (( a & ( 0x00FF0000 ) ) >> 16 ) );
Write_EEPROM ( Addr +3, (byte) (( a & ( 0xFF000000 ) ) >> 24 ) );

}


////////////////////////////////////////////////////////////////////////////////
float ReadFloatFromEEprom( byte Addr )
{
float data;
int32 d2;

d2 = (int32) Read_EEPROM ( Addr );
data = (float)d2;
d2 = (int32) Read_EEPROM ( Addr+1 ) ;
data = data + (float)( d2 << 8 );
d2 = (int32) Read_EEPROM ( Addr+2 ) ;
data = data + (float)( d2 << 16 );
d2 = (int32) Read_EEPROM ( Addr+3 ) ;
data = data + (float)( d2 << 24 );
data = (float) (( data / 100.00) + 0.000001 );

return(data);
}


Have anybody a better way?

THANK YOU VERY MUCH


Paolo
Ttelmah
Guest







PostPosted: Wed Mar 29, 2006 9:38 am     Reply with quote

You need to change the way the address arithmetic is done. Use:
Code:

WRITE_FLOAT_EXT_EEPROM(long int n, float data) {
  int i;
 
  for (i = 0; i < 4; i++)
    write_eeprom(i + n, *(((int *)&data) + i) ) ;
}


The same needs to be done to the read functions.
The problem is that the functions were probably written a _long_ time ago. When CCS was a lot younger, it (incorrectly), assumed that a 'pointer', always addressed a byte, so taking the pointer and adding 'i', moved you forward one byte. Now CCS (correctly) implements pointers, so if you take a pointer to a float, and increment it, it moves forward by four bytes...
The change to the code, 'casts' the float pointer, into an integer pointer (int *), and _then_ performs the arithmetic, resulting in the correct result.

Best Wishes
Falgellus



Joined: 12 Feb 2006
Posts: 8

View user's profile Send private message

PostPosted: Wed Mar 29, 2006 11:38 am     Reply with quote

Thank you very much Ttelmah, changing as you said all is ok.

Bye
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group