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 save float numbers in EEPROM

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



Joined: 28 Aug 2007
Posts: 106

View user's profile Send private message

How to save float numbers in EEPROM
PostPosted: Fri Oct 08, 2010 6:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 08, 2010 6:30 am     Reply with quote

CCS manual page 311: 'How do I write variables to EEPROM that are not a byte?'
richi-d



Joined: 28 Aug 2007
Posts: 106

View user's profile Send private message

PostPosted: Fri Oct 08, 2010 7:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 08, 2010 7:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 08, 2010 7:20 am     Reply with quote

Thank you so much! It works.
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