PDA

View Full Version : Adapt MELABS DS18B20 example to show negative temperatures?



CuriousOne
- 28th September 2022, 06:25
Hello.
I'm using the melabs sample code for DS18B20 and it works fine.
However, as it states:





OWOut DQ, 1, [$CC, $44] ' Start temperature conversion


waitloop:
OWIn DQ, 4, [count_remain] ' Check for still busy converting
If count_remain = 0 Then waitloop


OWOut DQ, 1, [$CC, $BE] ' Read the temperature
OWIn DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE, Skip 4, count_remain, count_per_c]


' Calculate temperature in degrees C to 2 decimal places (not valid for negative temperature)
temperature = (((temperature >> 1) * 100) - 25) + (((count_per_c - count_remain) * 100) / count_per_c)


What should be fixed there to display negative temperature properly?
As I've read DS18B20 datasheet, negative temperature output will always have 4 MSB bits set to 1111. But what's wrong with the above conversion formula?

richard
- 28th September 2022, 07:30
what part of this have you failed to understand
http://www.picbasic.co.uk/forum/showthread.php?t=24650

CuriousOne
- 28th September 2022, 19:04
I just had no idea same can be applied to DS18B20 readings :D

richard
- 29th September 2022, 03:50
What should be fixed there to display negative temperature properly?
As I've read DS18B20 datasheet, negative temperature output will always have 4 MSB bits set to 1111. But what's wrong with the above conversion formula?


you may have read the DS18B20 datasheet but you are using code for a DS18S20 so the first step is work out what you really have and then use the correct method

CuriousOne
- 29th September 2022, 05:37
Well I have it running with DS18B20 and it displays positive temperatures just fine...

CuriousOne
- 29th September 2022, 05:38
I also used this:

https://melabs.com/samples/LABX1-16F877A/temp_DS1822.htm

But it is much slower.
And I don't need high precision, this is just room temperature thermometer, 1C precision is ok.