IEEE 754 32bit floating point value.


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79

    Default IEEE 754 32bit floating point value.

    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

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default

    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
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  3. #3
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by shawn View Post
    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
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    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.



    Code:
    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)

  7. #7
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    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!!!
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  8. #8
    Join Date
    Sep 2008
    Posts
    27


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Cobra_Phil; - 6th December 2010 at 14:23.

  9. #9
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    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

  10. #10
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by shawn View Post
    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
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts