I’m a complete newbie at this. I started programming PICs last week and so far I have gone through the basic “LED Blink” and “LCD – Hello World” programs. As my 3rd project, I wanted to interface a DS18B20 temperature sensor to a PIC18F2620 and read the temperature.

I wanted to keep the programming simple and right now I’m only interested in reading a positive temperature (since my setup is inside my house and at about 70 deg. F) and not worry about the fractional value. Thinking it will be easy, I created the below program (with help from a few source codes I found on the net). But when I run the program, my LCD reads “+15 C”, which is not the correct temp because 70 deg F is slightly above 21 deg C. I also printed the raw DQ data on my LCD and it always reads %0000 0000 1111 0000. Can anyone go through my program and let me know what is going wrong? Thanks in advance.

-- Jack

'========== Begin Program ==================
'1) DS18B20 connected to RB4 and Vcc with a 4.7k pull-up resistor.
'2) Just reading positive temp for now, without reading decimal values
'3) DS18B20's temp bit looks like: % ssss-s???-????-???? (s= sign bit)
'4) Using PIC18F2620 with default parallel LCD connections (LCD is 16x2)

Comm_Pin VAR PortB.4 ' One-wire Data-Pin on PortB.4
Sign VAR BYTE ' +/- sign for temp display
RAWTEMP VAR WORD 'Read raw temperature from DS18B20
SignBit VAR RAWTEMP.Bit11 'read sign bit. 0=positive, 1=negative
TempC VAR WORD
Busy VAR BIT

CMCON = 7 ' RA0-RA3 are digital I/O. Turn off camparators

TRISA = 0 'PORTA is output
TRISB = 0 ' PORTB is output

PAUSE 500 ' Wait 0.5 second to initialize LCD
LCDOUT $FE,1 ' Clear LCD

Start_Convert:
LCDOUT $FE,2 'Home cursor
OWOUT Comm_Pin, 1, [$CC, $44] ' Skip ROM search & do temp conversion

Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!

OWOUT Comm_Pin, 1, [$CC, $BE] ' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [RAWTEMP] ' Read the raw temperature
GOSUB Convert_Temp
GOTO Start_Convert

Convert_Temp:
'Using indoors. So, only positive temp is measured for now

Sign = "+"
TempC=(RAWTEMP & 2032)/16

'Explain purpose of above operation
'Decimal 2032 is = %0111 1111 0000. So, ANDing RAWTEMP with 2032
'isolates the temperature value (multiplied by 16, since it's shifted to the left by 4 bits)

LCDOUT "Temp = ",Sign,dec TempC," C" 'output temperature on LCD
RETURN

END

'================== End Program ===============================