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 support@ccsinfo.com

Reading float32 from EEPROM produces garbage

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



Joined: 25 Apr 2014
Posts: 3

View user's profile Send private message

Reading float32 from EEPROM produces garbage
PostPosted: Fri Apr 25, 2014 2:15 am     Reply with quote

Hi. I've followed CCS advice on how to write four byte values into EEPROM. This works as expected. I receive uint32 over UART and write that into EEPROM. When I read back the EEPROM via PICkit3, it is as expected. I convert the float value to uint32 in the sending end to be able to use ASCII protocol with simple parsing using atoi32.

However, when I read EEPROM in my program to a float32, I get garbage. For example; I write 0x412925A0 into EEPROM. This is around 10.57 in float32. When I do printf("%f", eepromRead32(0)) I get basically garbage. I don't have the actual garbage values on hand but it's way off and I also checked that it's no bit reversal or anything simple like that either. When I read it back to %Lu (uint32) the value is exactly what I expect. So there is definitely something fishy going on with float variable in the compiler... Anyone got any hints?

I'm using a PIC18F26K20 with PCWHD 4.133.

Here's my EEPROM handling functions (just tidied up CCS ones):

Code:

void eepromWrite32(unsigned int16 address, unsigned int32 data)
{
   int i;

   for (i = 0; i < 4; i++)
      eepromWrite8(*((int8*)&data + i), address + i);
}


float eepromRead32(unsigned int16 address)
{
   int i;
   float data;

   for (i = 0; i < 4; i++)
      *((int8*)&data + i) = eepromRead8(address + i);

   return(data);
}


eepromRead8 and eepromWrite8 are just 1:1 wrappers for write_eeprom and read_eeprom.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Apr 25, 2014 3:13 am     Reply with quote

Sounds like floating point format confusion. CCS on most PICs do not use IEEE754 floating point format. They use a modified version of of defined by Microchip. That means that when you send bytes from outside, which are most probably IEE754, the CCS compiler's float routines cant understand them.

There are a some ways round this. You can send the bytes as you are doing and use the routines found in ieeefloat.c to convert to/from Microchip float to IEEE754 float32. Note CCS has no support/equivalent for doubles or extended on most PICs, just 32 bit floats. Beware, the PIC routines don't support the full range of NaNs and related stuff. Even then, byte order may cause trouble, so watch out!

Another alternative is to go the traditional route from the days when every compiler on every computer had its own floating point format (IEEE754 is comparatively new) and so floats were never sent in their binary form. THey were either converted to a scaled integer, or more commonly sent in human-readable ASCII.


Last edited by RF_Developer on Fri Apr 25, 2014 3:17 am; edited 2 times in total
Entropia



Joined: 25 Apr 2014
Posts: 3

View user's profile Send private message

PostPosted: Fri Apr 25, 2014 3:15 am     Reply with quote

Thanks for the reply. I am indeed feeding it IEEE-754 single floats. Actually the system that produces these floats is a PIC24 but I assume it can handle those.

I will see what I can do about this...
Entropia



Joined: 25 Apr 2014
Posts: 3

View user's profile Send private message

PostPosted: Fri Apr 25, 2014 3:30 am     Reply with quote

ieeefloat.c's f_IEEEtoPIC provided the necessary solution for me. Fourtunately it is only 68 bytes when compiled! Thanks again RF_Developer!
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