PDA

View Full Version : 18B20 Thermometer Question



Travin77
- 5th April 2006, 00:33
Hello all again. I just have a newbie question here. I am using the 18b20 dallas one wire and all is well except one thing. I am trying to cause a port pin to high at 40 degrees f. In celcius this is 4.444444444444 you get the idea. The binary string is something like this:
0000 0000 0100 0111 which is about 4.4375 degrees celcius which is close enough. This is the code I am using which is complements of Reynolds Electronics:

OWIN D, 4, [Stat]' Check for still busy converting
IF Stat = 0 THEN W1' Still busy?, then loop
OWOUT D, 1,[$55,$28,$7A,$4A,$66,$00,$00,$00,$84,$BE]
OWIN D, 2, [Temp.LOWBYTE,Temp.HIGHBYTE]' Read two bytes, then end communications
IF Temp.bit15 = 1 THEN Below_32 ' Check for temp below 0°C
Sign = "+"
Temp = ((((Temp >> 4) + 50) * 9) /5) -58
LCDout $fe,$94,"Temp #1 =","+", DEC Temp,".", DEC2 Temp,"F"

I am just unsure how to mess with the code necessary for the temp conversion with a 16 bit binary number. I read a couple of strings that are close to what I needed but I couldn't get them to work. I appreciate any help.

Travin

Bruce
- 5th April 2006, 01:18
The factory default resolution the DS18B20 is 12-bit. If you're just testing for
over 4.4 deg C, then after reading the temp, try this;

TEMP = TEMP >> 4 ' Middle 8-bits into low byte. (see data sheet)
IF TEMP.LowByte >= 4 THEN HIGH Whatever_PIN

The lower 4-bits of TEMP is the remainder in 0.0625 deg C increments. By
shifting TEMP >> 4 you're just shifting out the lower 4-bits, and using the
whole temp result which is the middle 8-bits of 16-bit word TEMP.

If you need better resolution, then test the lower 4-bits of TEMP first, then
shift >> 4 to get the whole number.

Remainder VAR BYTE
Whole VAR BYTE

Remainder = (TEMP.LowByte & $0F) ' Mask & keep lower 4-bits in TEMP
TEMP = TEMP >> 4 ' Shift middle 8-bits into low byte
Whole = TEMP.LowByte ' Middle 8-bits of TEMP in Whole

IF (Whole > 4) AND (Remainder = 7) THEN HIGH PIN ' IF > 4.4 deg C THEN

7 * 0.0625 = 0.4375 deg C.