#### premelec

##### Senior Member

brain is frozen or perhaps trying to make the math too comlicated - but assistance would be appreciated! Thanks...

- Thread starter premelec
- Start date

brain is frozen or perhaps trying to make the math too comlicated - but assistance would be appreciated! Thanks...

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 )

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

Not for two's complement ...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

$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:

$80 = -128

$81 = -127

:

$FE = -2

$FF = -1

$00 = 0

$01 = 1

$02 = 2

:

$7E = 126

$7F = 127

That will teach me to look at the DS18B20 datasheet and not consider any firmware processingreadtemp does not use twos complement

bit 7 is simply a flag, 1 for negative, 0 for positive

What about Readtemp12?

Is it the same but 2048 for -1/16C?

Is it the same but 2048 for -1/16C?

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!

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:

"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)."