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

Float variable

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



Joined: 02 Feb 2010
Posts: 346

View user's profile Send private message

Float variable
PostPosted: Tue Oct 11, 2011 4:07 am     Reply with quote

I have this bytes [8F 80 48 40] which represent "197182.000000" floating point number in "Modbus Poll" tool. BUT When I use make32 ()
Code:
 

MFR=MAKE32(Buffer[3],Buffer[4],Buffer[5],Buffer[6]);
               lcd_putc("\fMFR:");
               printf(lcd_putc,"%f",MFR);

I see: "2367324.30".
My compiler ver.4.104 ,PIC18F2525.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: Float variable
PostPosted: Tue Oct 11, 2011 4:40 am     Reply with quote

kmp84 wrote:
I have this bytes [8F 80 48 40] which represent "197182.000000" floating point number in "Modbus Poll" tool. BUT When I use make32 ()
Code:
 

MFR=MAKE32(Buffer[3],Buffer[4],Buffer[5],Buffer[6]);
               lcd_putc("\fMFR:");
               printf(lcd_putc,"%f",MFR);

I see: "2367324.30".
My compiler ver.4.104 ,PIC18F2525.


Yes, that is odd. There shouldn't be any fractional part of the result.

...

That's not what you are expecting however. You are getting very confused about floating point numbers and integers. The bytes you give, 8F 80 48 40, appear to be the big-endian ordering of the IEEE-754 32 bit floating point representation of 197182.0. CCS's numerical convertor gives the 32 bit hex as 0x48408F80. So, for a start you have a byte ordering issue to sort out. Next you have to consider that CCS C uses the Microchip floating point format, which is NOT IEEE-754 compatible. The Microchip float32 hex equivalent for 197182.0 is 0x808F4090, which is -1.3155637E-38 in IEEE format. So you have a pretty big number format conversion issue to deal with. You perhaps can see some connection between the two, but its not straightforward.

Then you use an integer routine to put the bytes back into 32 bits. The result of that should be either 1212190592 or -1887418304 depending on endianness. This integer should then, assuming you've declared MFR as a float, be converted to the nearest float representation of that number, which shouldn't have any decimal places, but may well have due to loss of precision.

The biggest problem you have is that you need to convert IEEE-754 format float to Microchip float format. That's not easy. Instead learn a basic rule: don't send floats across networks/comms links. Use ASCII, use scaled integers, use anything other than floating point.

RF Developer
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Oct 11, 2011 4:47 am     Reply with quote

Take a look at:

http://ww1.microchip.com/downloads/en/AppNotes/00575.pdf
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Tue Oct 11, 2011 6:54 am     Reply with quote

Though I agree wholeheartedly with avoiding sending floats in a format like this, if you must, look at ieeefloat.c in the CCS drivers directory, which contains the routines to convert to/from ieee format.....

Best Wishes
kmp84



Joined: 02 Feb 2010
Posts: 346

View user's profile Send private message

PostPosted: Tue Oct 11, 2011 7:27 am     Reply with quote

I saw your post Ttelmah and try this(using ieeefloat.c) ,but result didn't true.
True value is :"[8F 80 48 40] ---->"197182.000000""(float).
Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Tue Oct 11, 2011 8:30 am     Reply with quote

You have a second problem.
Your pairs of bytes are being swapped.
The PIC is receiving 48408F80

You first and second byte pairs are swapped. You need to fix this in your make32, and then convert the value with f_IEEEtoPIC.

Best Wishes
kmp84



Joined: 02 Feb 2010
Posts: 346

View user's profile Send private message

PostPosted: Wed Oct 12, 2011 3:35 am     Reply with quote

It's work. Thank you very much! This is the MODBUS floating point data if any other have a same problem. Smile
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Oct 12, 2011 3:48 am     Reply with quote

kmp84 wrote:
It's work. Thank you very much! This is the MODBUS floating point data if any other have a same problem. Smile


Right, so you did look at this first didn't you... http://www.chipkin.com/articles/how-real-floating-point-and-32-bit-data-is-encoded-in-modbus-rtu-messages

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