Shop OBEX P1 Docs P2 Docs Learn Events
Parsing recommendations — Parallax Forums

Parsing recommendations

Farmer ScottFarmer Scott Posts: 30
edited 2008-05-29 16:52 in Propeller 1
Guys...

I need some advice.· Spent about 8 hours last night trying to figure out a reasonable way to convert·the result from a DS1631 I2C temperature sensor into a float and pass the resulting long to the calling function.

Any ideas?· At this point, I'm really open for anything...

The return from DS1631 is 2 bytes, MSB is xyyyyyyy, LSB is zzzz0000, where x is a sign bit, y is the integer portion and z is the fractional portion.

I'm about to the point of change platforms, so any help would certainly be appreciated...

Thanks,

Scott

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-29 15:01
    What you have is a 2's complement number with 4 binary places. You need to convert the two bytes into an integer with the implied scale factor of 16, then convert the integer to floating point, then divide by the scale factor to get your final floating point value. To convert to a signed integer do:

    value := (((MSB << 8) | LSB) << 16) ~> 20 ' Position the value and extend the sign

    If the name of your floating point object is "f", you'd then do

    value := f.FDiv(f.FFloat(value) ,16.0) ' Convert to floating point and scale value
  • Kaos KiddKaos Kidd Posts: 614
    edited 2008-05-29 15:08
    Mike,
    Do you live and breath propeller syntax?
    WOW... It would have taken me QUITE some time for that little bit of code you "poofed" in minutes...

    Nice... simple... and sweet...

    I like...

    [noparse]:)[/noparse]

    KK

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Propeller + Hardware - extra bits for the bit bucket =· 1 Coffeeless KaosKidd

    ·
  • Farmer ScottFarmer Scott Posts: 30
    edited 2008-05-29 15:43
    Mike,

    'f' object...I've been attempting to use FloatMath.spin from Chips Float Demo on the object exchange.· Is this the object you're referring to?

    Scott
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-29 15:49
    Yes. Any of the float objects will do, either the all-Spin version or the mixed Spin/assembly one.

    No, I've programmed in enough languages and with enough different computers over the years, that Spin and the Propeller are not that different from other things I've seen. I do have a Propeller Manual handy and it opens easily to certain sections like the one describing the different Spin operators.

    Post Edited (Mike Green) : 5/29/2008 3:55:25 PM GMT
  • Farmer ScottFarmer Scott Posts: 30
    edited 2008-05-29 16:26
    Let me see if I understand all the stuff that's going on here...

    (MSB << 8) | LSB) - This instructions takes MSB, shifts it left 8 bits (7 becomes 15, 6 becomes 14...), and logical OR's it with LSB, in essence making MSB word 1, and LSB word 0?

    << 16) - This instruction shifts the result of the first instruction to the left 16 bits (15 becomes 31, 14 becomes 30...). This is necessary to get the sign bit into the proper position.

    ~> 20 - This instruction shifts everything back to the right 20 bits (31 becomes 11, 30 becomes 10), and maintains the sign value in bits 12 through 31 (so if it was 1, bits 12 through 31 will be 1, same for 0).

    The result would be something like %0001100011010000 becoming %0000000110001101 (16 least significant bits only).

    Am I even remotely close to correct so far?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-29 16:31
    Yes, it's pretty straightforward.
  • Farmer ScottFarmer Scott Posts: 30
    edited 2008-05-29 16:45
    The only thing I'm still struggling with is scaling...you're dividing my 16...why 16?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-29 16:49
    The fractional part of the value is 4 bits. 2 to the 4th power is 16.
  • Farmer ScottFarmer Scott Posts: 30
    edited 2008-05-29 16:52
    Okay, I see that...I feel a little dumb for that one. How much do I owe you?
Sign In or Register to comment.