Actually, the solution to your problem is easier than I thought. DS18S20's resolution is 0.5 C. So, you can use the following algorithm for negative temps -
Lets say RAWTEMP is where you stored the raw data out of the DS18S20. If bit#8 of RAWTEMP is 1, then you know that the temp is negative. In that case, invert RAWTEMP and add 1 to it (RAWTEMP = ~RAWTEMP + 1). If you multiply this number by 5 and store it in a variable, say TempC, you'll see that TempC will be the actual temperature, only multiplied by 10. For example, when TempC reads 450, then your actual temperature is -45 C. Let's try to do this with a real raw binary reading out of the DS18S20 -
Let's say that the raw binary reading out of your DS18S20 is
1111 1111 0101 1101
Since bit#8 is 1, we know that this is some negative temperature reading. Now we invert and then add 1 to the reading-
Code:
RAWTEMP = 1111 1111 0101 1101
~RAWTEMP = 0000 0000 1010 0010
+1 = 0000 0000 1010 0011
"0000 0000 1010 0011" represents a decimal value of 163. Multiply this number by 5 and we get 815. So, this really is -81.5C (of course this is out of the range of your sensor, but you get the idea). For example, take one of the readings from DS18S20's datasheet. -55C is represented by the raw binary value of "1111 1111 1001 0010". Inverting and then adding 1 reselts in "0000 0000 0110 1110" whis is 110 in decimal. Multiply this number by 5 and you get 550, which is -55C.
Hope this helps.
Bookmarks