Help Needed Converting Binary to Decimal
Juliet Oscar Echo
Posts: 31
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)
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
800 in decemal to binary == %00100000
it would also help if you explained more of what your main goal is, or you project.
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.
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.
new := old << 20 ~> 20
...to convert a 12 bit value.
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,
Edit your original post. Choose "Go Advanced", there should be a pull down menu with "solved" as an option.