READTEMP Negative maths DS18B20

premelec

Senior Member
It's getting cold... I see the DS18B20 readings from READTEMP are supposed to be negative if over 127 [manual 2] - however is a reading of 130 five degrees lower temperature or 5 degrees higher temperature than a reading of 135? I guess my
brain is frozen or perhaps trying to make the math too comlicated - but assistance would be appreciated! Thanks...
 

MORA99

Senior Member
If regular 2-complement then 129 vill be -1, 130 -2, etc.

The first bit of a byte is used as sign bit, so the real temperature is 0 - (readout - 128)
 

hippy

Technical Support
Staff member
@ MORA99 : Not quite; that's one representation but not two's complement. For a byte, -1 would be 255 ($FF), -2 is 254 ($FE) down to -128 as 128 ($80).

In my DS18B20 datasheet, "TEMPERATURE/DATA RELATIONSHIP Table 2" is what shows how it's done, although it would have been sensible to have given 1'C and -1'C as examples ;-)

- ReadTemp IO_PIN, b0
- If b0 >= $80 Then
--- SerTxd( "-" )
--- b0 = -b0
- Else
--- SerTxd( "+" )
- End If
- SerTxd( #b0 )
 

premelec

Senior Member
OK I'm going to take it as the bigger the number above 127 the lower the temperature is- could be the basis for a riddle like when is 1 greater than 50... in reference to microwave oven timers :) Thanks...
 
Last edited:

MORA99

Senior Member
Right, sorry for the confusion :)

I used the DS recently on a regular pic board, and it used that representation so I presumed that was the correct way.
 

andrew_qld

Senior Member
THis one had me going for a whlie too with the TCN75A temp sensor which uses the same 1 bit "negative" flag.

readi2c $00,(b0, b1) ' Reads registers 0 and 1 into B0 and B1
if b0 > 128 then let b3 = "-" endif 'the 8th bit is the "sign" bit shows + or -
if b0 < 128 then let b3 = "+" endif
if b0=128 then let b0=0 endif
if b0 > 128 then let b0=256-b0 endif ' displays negative temperatures accurately
 

hippy

Technical Support
Staff member
OK I'm going to take it as the bigger the number above 127 the lower the temperature is- could be the basis for a riddle like when is 1 greater than 50... in reference to microwave oven
Not for two's complement ...

$00 / 0 = 0
:
$7E / 126 = 126
$7F / 127 = 127
$80 / 128 = -128
$81 / 129 = -127
$82 / 130 = -126
:
$FE / 254 = -2
$FF / 255 = -1


To quickly determine the value part of a two's complement negative number, invert all the bits ( including the msb ) then add one. eg $82 = %10000010, inverted = %01111101, add one = %01111110 = 126, ie, -126.

You'll note an interesting situation with $80 :)
 
Last edited:

hippy

Technical Support
Staff member
One problem with visualising how two's complement works is because it's often shown ( as I did above ), running from $00 to $FF. The jump from $7F/+127 to $80/-128 and then decreasing doesn't always make immediate sense. If that list is drawn differently, it's much easier to see that every hex number is 'add 1' above the number below it ...

$80 = -128
$81 = -127
:
$FE = -2
$FF = -1
$00 = 0
$01 = 1
$02 = 2
:
$7E = 126
$7F = 127
 

Technical

Technical Support
Staff member
readtemp does not use twos complement
bit 7 is simply a flag, 1 for negative, 0 for positive

e.g to display using sertxd
Code:
readtemp 0,b1
 
if b1 > 127
sertxd ("-")
  b1 = b1 - 128
else
  sertxd ("+")
end if
 
sertxd (#b1)
 

premelec

Senior Member
Thanks techinical - that's pretty much what I'd concluded - however the rollover from +0 to -0 is a bit of a conundrum... if positive values go from 0 to 125DegC I would suppose that an indication of '128' [>127!] is then indicative of -1DegC - yet you have - 128 in your code... I'm going to have to get empirical with a bit of salted ice solution... I'm an empiricist but it's nice to start with accurate ideas! :)
 

Technical

Technical Support
Staff member
You should never actually get 128. The firmware checks for a number less than 0 and then does the correction (setting bit7, to add 128). Therefore 0 degrees will always give the value 0 rather than 128!

-1 degrees will be 129, ie %10000001
 

premelec

Senior Member
Hi Technical - thanks for the additional information that 128 is excluded from READTEMP and 129 is -1DegC. I'll correct my code accordingly... perhaps you could ask the manual guys to put in a note about this... it doesn't seem obvious and 0DegC is a magic point since the French decided it.... one we're often concerned with... I guess many codes would say "if > 127 turn on the heaters now" - the water pipes are almost split!

Upon further reflection I'd like to ask that a subsequent implementation of READTEMP put out a monotonic linear value - say 0 to 180 to cover the -55 to +125 range which avoids making a computation right at the freezing point - which discontinuity could result in considerable damage in a control system if off by a slight amount.

My use is in DegF so I'd be happy to see READTEMP_F with a built in conversion also implemented in directives... Thanks again for a wonderful product in any case!
 
Last edited:

Technical

Technical Support
Staff member
The 'readtemp' command in manual part 2 does already say:

"The temperature is read back in whole degree steps, and the sensor operates from-55 to + 125 degrees celsius. Note that bit 7 is 0 for positive temperature values and 1 for negative values (ie negative values will appear as 128 + numeric value)."​
 
Top