srob - try going through the following program. It worked for me.
Code:'Program: TsensDS18B20.bas 'Notes: Temperature sensing with DS18B20 chip. 'Hardware Used: PIC18F2620 and DS18B20 Temp sensor 'Connections: 'Using default pin connections between LCD and PIC 'D4 -> RA0, D5 -> RA1, D6 -> RA2, D7 ->RA3 'E -> RB3, RS -> RA4, Vee (Pin 3) to GND '**************************************************************** 'DS18B20 connected to RB4 (pin 25) and Vcc with a 4.7k pull-up resistor. 'PIC18F2620 programmed with the "INTRC" switch for clock setting. 'MUST SET OSCCON to %01100000 to set the internal clock to 4-MHz '**************************************************************** Comm_Pin VAR PortB.4 ' One-wire Data-Pin on PortB.4 SignC VAR BYTE ' +/- sign for Celcius temp display SignF VAR BYTE ' +/- sign for F temp display RAWTEMP VAR WORD 'Read raw temperature from DS18B20 SignBit VAR RAWTEMP.Bit11 'Read sign bit. 0=positive, 1=negative TempC VAR WORD 'Converted temp in C TempF var word 'Converted temp in F Busy VAR BIT 'Busy bit dummy var word OSCCON = %01100000 'Internal clock set to 4-MHz (MUST DO THIS!!!) CMCON = 7 ' RA0-RA3 are digital I/O. Turn off camparators TRISA = 0 'PORTA is output TRISB = 0 'PORTB is output ADCON1 = 15 'All PORTB pins are digital PAUSE 500 ' Wait 0.5 second to initialize LCD LCDOUT $FE,1 ' Clear LCD Loop: gosub ReadDS18B20 'Initiate talk with & read DS18B20 gosub CalcC 'Calculate Celsius from RAWTEMP gosub CalcF 'Calculate Fahrenheit from Celsius LCDOUT $FE,2 'Home cursor LCDOUT "Temp=",SignC,dec TempC/100,".",DEC2 TempC," C" LCDOUT $FE,$C0 'Go to beginning of 2nd row lcdout "Temp=",SignF,dec TempF/100,".",DEC2 TempF," F" goto Loop ReadDS18B20: OWOUT Comm_Pin, 1, [$CC, $44] ' Skip ROM search & do temp conversion ReadBusy: OWIN Comm_Pin, 4, [Busy] ' Read busy-bit IF Busy = 0 THEN ReadBusy ' Loop until Busy=1 (Temp conversion complete) OWOUT Comm_Pin, 1, [$CC, $BE] ' Skip ROM search & read scratchpad memory OWIN Comm_Pin, 2, [RAWTEMP.LowByte, RAWTEMP.HighByte] ' Read the raw temperature from DS18B20 return CalcC: if SignBit=0 then SignC="+" else SignC="-" rawtemp = ~rawtemp+1 'Negative temp stored in 2's complement format endif dummy = rawtemp*625 'DS18B20's resolution is 0.0625 C TempC=DIV32 100 return CalcF: If SignC="-" then If TempC>1777 then 'When Tc is <(-17.77), Tf is finally negative SignF="-" Dummy=TempC*18 TempF= DIV32 10 TempF=TempF-3200 Else SignF="+" 'Tf is still positive when Tc>= (-17.77) Dummy=TempC*18 TempF=DIV32 10 TempF=3200-TempF Endif Else SignF="+" 'Positive Tc always gives positive Tf Dummy=TempC*18 TempF=DIV32 10 TempF=TempF+3200 Endif return END




Bookmarks