DS18B20 question

rq3

Senior Member
The DS18B20 measures in centigrade. Assuming that the lowest temperature I need to measure is -20 C, can I do something like:

readtemp B.0, b0
b0=b0+50

to internally shift the readtemp so that it will always be a positive value? In other words, will the picaxe deal with the negative sign bit on the readtemp byte without further code manipulation (twos complement,etc.), or will it "roll over" as soon as it sees a negative temperature measurement?

Thanks!
Rip
 

hippy

Technical Support
Staff member
If you do want the READTEMP result as two's complement value ( $FE=-2, $FF=-1, $00=0, $01=1 etc ), use 'b0' and ...

Code:
ReadTemp DS18B20_PIN, b0
b0 = bit7 * $7F ^ b0 + bit7
 

rq3

Senior Member
Hi Hippy and Neil. I've read the BASIC command and the examples, but it's not clear to me whether the picaxe can simply add 50 to a measured negative temperature to translate the measurement into a positive range. I don't want to display the result, just use it for further code manipulation. I've seen other code examples that imply that this is OK, but...?
 

hippy

Technical Support
Staff member
it's not clear to me whether the picaxe can simply add 50 to a measured negative temperature to translate the measurement into a positive range.
No, not possible with the raw value returned - The result from READTEMP is $81 (129) for -1C, $81+50 = $B3 (179), not the desired $31 (49).

Converted to two's complement it will work ...

Code:
ReadTemp DS18B20_PIN, b0
b0 = bit7 * $7F ^ b0 + bit7 + 50
If the temperature were -1C then b0 will end up being 49. If it were +1C the result would be 51.
 

rq3

Senior Member
Bit question

Hippy, per my earlier question regarding DS18B20 conversion, is this OK for three sensors:

Code:
    Readtemp B.0, b0                            ;read the freezer probe temperature
        pause 1000                                ;1 second delay for data conversion
    Readtemp B.1, b1                            ;read the refrigerator evaporator temperature
        pause 1000
    Readtemp B.2, b2                            ;read the refrigerator compartment temperature
        pause 1000
b0 = bit7 * $7F ^ b0 + bit7 + 30            ;offset the temperature readings by +30C so all temps are positive
b1 = bit15 * $7F ^ b1 + bit15 + 30
b2 = bit23 * $7F ^ b2 + bit23 + 30
I'm confused on the bits within each byte. Should they all just be the bit7 for each byte, or is the above correct?

Many thanks!
Rip
 

hippy

Technical Support
Staff member
Yes that should be okay. It's the most significant bit in each byte which is used as a sign indicator and used to adjust each byte.
 

MPep

Senior Member
The example given by RIP in #8 to me looks wrong.

B0 uses 8 bits, so does B1 and B2.
So why use bit15 and bit23?
 

hippy

Technical Support
Staff member
Code:
    msb                                 lsb
   .----.----.----.----.----.----.----.----.
b0 |  7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
   |----|----|----|----|----|----|----|----|
b1 | 15 | 14 | 13 | 12 | 11 | 10 |  9 |  8 |
   |----|----|----|----|----|----|----|----|
b2 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
   |----|----|----|----|----|----|----|----|
b3 | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
   `----^----^----^----^----^----^----^----'
 
Top