DS18B20 negative values

hax

New Member
I'm trying to get a temperature sensor to read out in increments of 0.1 degree, and I also need it to display negative numbers.

I have done a search for example code but the inverse line below next to the ***does not work.... positive numbers come out ok but the negative readings just show 0.0

I'm using an 08M

What is a good way to do an inverse function on a lowly 08M?




Code:
main:

symbol pinx = 4
symbol temp = w1
symbol whole = b4
symbol deci = b5
symbol signr = 0


readtemp12 pinx,temp
b6 = 43 ; ="+"
IF temp> 2048 THEN ; below zero...
b6 = 45 ; ="-"
temp = 1/ temp ; ****************this is the problem
temp = temp AND 4095 + 1 ;mask off top 4 bits,add 1 for correction
ENDIF 



temp = temp*10 / 16 ; x10 = 1 decimal(0.1) resolution
whole = temp / 10
deci = temp // 10


serout 0,n2400,(b6,#b4,".",#b5,13,10)


goto main
 

hax

New Member
Ah, that's what happens when you blindly (lazily) use someone elses code without looking into the nitty gritty of the datasheet....

*****ten virtual self imposed lashes to haxby, and ten thousand virtual dollars to hippy*****
 

westaust55

Moderator
Looks like some of my past code for DS18B20

Shooting from the hip (ie unchecked) but another potential way to extract the negative values:

temp = 65535 - temp ; two's compliment
temp = temp AND 4095 + 1 ; mask off the top 4 bits and +1 to adjust/correct
 
Top