PDA

View Full Version : IEEE 754 32bit floating point value.



shawn
- 4th December 2010, 03:16
Hello,
I've been looking for a while now and cant find answer. At least not an answer I can understand. What I'm trying to do is take an ad reading from a lm34 and pack it into a 32bit floating point variable to send to a modbus rtu master. I have the mod bus comunication working but I am not sure on how to pack the variable. For instance, with my LM34 temp sensor if I get a reading of 751, this equals 75.1 degrees but I cant for the life of me figure out how to pack this up into a 32bit floating point variable. The method of modbus rtu I am using relies on the IEEE 754 Big Endian format. I have found lots of info but nothing I can understand. Can someone please point me in the right direction.
Thanks
nick

rsocor01
- 5th December 2010, 09:23
Shawn,

Like you said, there is a lot of info out there about floating point numbers. However, working with floating point numbers is not an easy task :(. Fortunately for you, you only want to convert a decimal number into a 32-bit floating point. That part is not too hard :).

A 32-bit floating point number is divided into three parts, the sign, the exponent, and the mantissa. You should make yourself familiar with these concepts. After that, you should look at the methods for converting decimal numbers into floating points. There are many examples out there. Probably the hardest part of doing this is that you need to convert decimal fractions to binary numbers. For example: 6.25(base 10) = 110.01(base 2). Most decimal fractions are not as obvious and easy to convert to binary as the example I gave you. But again, fortunately for you there are many tutorials in the web on how to convert decimal fractions to binary numbers.

I hope that this post helped you and guide you in the right direction.

Robert

shawn
- 5th December 2010, 22:20
Thanks for the answer Robert. I found some examples on the web and I understand it now. Has anyone ever done this on a PIC. It does look complicated, is there any other vriable type that would handle a decimal point in modbus rtu. It didnt look like it. Robert have you ever written a 32 bit floating point routine to pack a variable with pic basic pro. I would love some tips on how to get started. Or some links to posts in the forum
Thanks
Shawn

mackrackit
- 6th December 2010, 00:02
This might help
http://www.picbasic.co.uk/forum/content.php?r=153-N-Bit_MATH

rsocor01
- 6th December 2010, 03:13
Robert have you ever written a 32 bit floating point routine to pack a variable with pic basic pro.

Shawn,

No, I have never written a PBP program to convert a decimal number into a floating point. Do you know C language? In C18 you can declare a variable as a 32-bit float. However, it takes a lot of programming memory when you use float variables in C18.

If you insist in writing this program in PBP, I would suggest to use Long vars in PBP for the 32-bit floating point numbers. It can be done, but it is something that won't be done in a single afternoon. I found some examples in the web to convert decimals to 32-bit floating point. The following link has some examples. Disregard the 8-bit floating examples, but you can study them as well since the idea is the same.

http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html

Robert

Jerson
- 6th December 2010, 13:14
I have worked on the floating point library for the 8051. This is a converter I wrote in QuickBasic 4.5 to convert a floating number entered on the PC to floating point representation that can be understood by the micro controller. See if it will help you move ahead in your quest.

To the best of my recollection, the format is
8 bits biased exponent followed by 24 bits signed mantissa.

The idea is to move the mantissa till you get a 1 bit in the highest position of the mantissa and accordingly change the exponent by the amount you moved the mantissa. Then remove the highest bit (it is implicit) and put in the sign bit for the mantissa. A 1 bit here means it is a negative number.

That's all I can offer right now. I am a bit hazy on the details at the moment as it has been more than 10 years ago that I did this. If you need any help, let me know and I'll try to help.





DIM Mant AS SINGLE, Expo AS INTEGER
DIM bits AS INTEGER, Sign AS INTEGER

PRINT "CFLT converter for text to float51 format"
INPUT "Please enter the number : ", num

IF num < 0 THEN
Sign = 1
num = -num
END IF

Mant = num
Expo = &H80 + 24
FOR bits = 1 TO 24
IF (Mant AND &H800000) THEN EXIT FOR
Mant = Mant * 2
Expo = Expo - 1
NEXT

'remove the MSB if +ve
IF Sign = 0 THEN
Mant = Mant AND &H7FFFFF
END IF

PRINT "The floating point representation is Exp, Mantissa"
PRINT HEX$(Expo); ", "; HEX$(Mant)

cncmachineguy
- 6th December 2010, 13:39
Adding to the confusion to try and shead some light. You say the number you get back could be 751, which would equal 75.1. how is this sent? is it :
00000001 01110111, or is it
00000111 01010001?

The first being stright binary and the second being packed BCD I will assume the first. Now when you say you need a 32 bit, floating point answer, seems to me there must be some format to this. for instance, does the device you are sending it to expect the decimal to be in any special place? is it good with a 5 place decimal or does it assume 2 place? how does the receiving end expect to see the number? I mean 32 bit for tempreture is a bit much I think, could be 32767.32767 to -32767.32767 degrees! or something like that. Either way I don't want to live there!!!

Cobra_Phil
- 6th December 2010, 14:14
I used this information to read floats from an AB plc and it worked fine. There is a subroutine for Int to Float.
http://melabs.com/resources/fp.htm

shawn
- 18th December 2010, 01:06
thanks for the replys everyone,
Ive been gone for a while and havent had time to suddy it all yet. Ill get back to it.
Thanks
Nick

rsocor01
- 18th December 2010, 12:32
thanks for the replys everyone,
Ive been gone for a while and havent had time to suddy it all yet. Ill get back to it.
Thanks
Nick

Shawn,

It looks like we were trying to reinvent the wheel. Everything that you need is provided in the melabs.com link that Cobra_Phil mentions above.

Robert