Shop OBEX P1 Docs P2 Docs Learn Events
Help Needed Converting Binary to Decimal — Parallax Forums

Help Needed Converting Binary to Decimal

Juliet Oscar EchoJuliet Oscar Echo Posts: 31
edited 2013-04-10 16:24 in Propeller 1
I have a project where I want to convert a 12 bit binary variable to decimal. Only problem is I'm getting results that don't make sense when the binary value is negative (project uses two's complement). Any suggestions / help / example code would be greatly appreciated.

I inserted some code below where I'm experimenting with just using the "FullDuplexSerial" Object to display the decimal value of four different binary variables and getting erroneous results when the binary value is negative.

Thanks in advance for the help.

CON''


_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


VAR''


Long Var1, Var2, Var3, Var4

OBJ''

Debug : "FullDuplexSerial"


PUB Main''


Debug.start(31, 30, 0, 57600)


repeat


Var1 := %001100100000 'Dec value = 800
Var2 := %000110010000 'Dec value = 400
Var3 := %111001110000 'Dec value = -400
Var4 := %110011100000 'Dec value = -800


debug.tx(1) 'Home

debug.dec (Var1) 'Displayed value is 800
debug.str(string(13))
debug.dec (Var2) 'Displayed value is 400
debug.str(string(13))
debug.dec (Var3) 'Displayed value is 3696, should be -400
debug.str(string(13))
debug.dec (Var4) 'Displayed value is 3296, should be -800


waitcnt(clkfreq/10 + cnt)

Comments

  • teganburnsteganburns Posts: 134
    edited 2013-04-10 14:20
    So i wanna just say im not super experienced. But here is my guess first off binary can't go negative, and converting your binary i got the first line to equal a 50 then a 0 using http://www.asciitohex.com/

    800 in decemal to binary == %00100000

    it would also help if you explained more of what your main goal is, or you project.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2013-04-10 14:48
    You are working with a 32-bit processor, so -400 is not 111001110000, it's 11111111111111111111111001110000 and -800 is 11111111111111111111110011100000.

    Signed values must account for all 32 bits. The propeller effectively uses 111001110000 as 00000000000000000000111001110000 and the highest bit which designates negative is not set, so it views it as a positive number. As a positive number, this binary value is 3696 as you saw.
    For this same reason, values that are just a BYTE or WORD cannot (as the Propeller sees it) be a negative number, because it is missing the high bit that defines the numbers as negative as well as the rest of the definition of the 32-bit number.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-10 15:28
    All that explains what's going on. You have 12-bit signed 2's complement values. How to convert to 32-bit signed 2's complement. The easiest way is the following:

    new := old << 24 ~> 24

    What this does is shift your 12 bit value left to where the sign bit of the 12-bit value is in the sign bit position of the 32-bit word. It then shifts right using a sign extending shift to put the value back into the right part of the 32-bit value, but now with the sign bit propagated through the whole 32-bit word. Try it.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2013-04-10 15:38
    You mean:
    new := old << 20 ~> 20
    ...to convert a 12 bit value.
  • Juliet Oscar EchoJuliet Oscar Echo Posts: 31
    edited 2013-04-10 16:08
    Bobb, thanks for reminding me that the Propeller is a 32 bit processor.

    Mike, thanks for responding to my question. I tried adding your line of code to my project, and still no joy. I'm trying to read the temperature off a Texas Instrument TMP102 chip. The Propeller receives the temperature value from the TMP102 in two bytes. The last four bits of the least significant byte always read 0, thus by shifting the least significant byte and adding it to the most significant byte gets you a 12-bit singed 2's complement value.

    Here's the code:

    msb := receivePacket(true) ' Read the Most Significant Byte
    lsb := receivePacket(false) ' Read the Least Significant Byte
    stopDataTransfer


    msb1 := msb
    lsb1 := lsb

    msb <<= 4 ' Assemble the shifted MSB and LSB
    lsb >>= 4


    msb2 := msb
    lsb2 := lsb

    tempRaw := msb | lsb ' Update the stored temperature to the most recent reading.


    tempRaw := tempRaw << 24 ~> 24

    I'm testing the sensor in my deep freezer as I type, The code works great all the way down to -80 (-8.0 deg C), However, instead of reading -81, the sensor reads + 79 and the value continues to decrement (back down to 0). Any explanation for this and fix would be greatly appreciated.

    Thanks,
  • Juliet Oscar EchoJuliet Oscar Echo Posts: 31
    edited 2013-04-10 16:11
    Good Point Bobb, I've updated the code and the sensor is back in the deep freezer. How do I change this thread from unsolved to solved?
  • Juliet Oscar EchoJuliet Oscar Echo Posts: 31
    edited 2013-04-10 16:17
    Well, the deep freezer only goes down to -12C, but it appears to be working perfect. Thanks so much Bobb and Mike for your great help.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-04-10 16:24
    How do I change this thread from unsolved to solved?

    Edit your original post. Choose "Go Advanced", there should be a pull down menu with "solved" as an option.
Sign In or Register to comment.