Hi all,

why is it so hard to get the DS18B20 sensor to work ?
I'm using a 16F88 pic with internal osc.

The code i used most of CocaColaKid's ( thanks ) posted here:
http://www.picbasic.co.uk/forum/showthread.php?t=7533

I'm getting only a fixed value ( tempd = 1360 and tempc = 8500 ).

It is not a bad sensor nor a bad pic ( as i tryed a second one ).

Here is the code i used:

;************************************************* ******************************

;------------------------ Port Initialization -------------------------------

SSPCON.5 = 0 ; Disable SSP Module
TXSTA.5 = 0 ; Disable AUSART Tx
RCSTA.7 = 0 ; Disable Serial Port
CMCON = %00000111
cmd con 254 ; Control byte

'OSCCON= %01101110
'OSCCON= %01100000
OSCCON= %01100100

INTCON=0
'PIE1 = 0
'PIE2 = 0
'CMCON=7
CVRCON=0
CCP1CON=0 ; Disable CCP Module
OPTION_REG.6 = 0

'************************************************* ****************************
'DEFINEs
'************************************************* ****************************
'internal OSC used
DEFINE osc 4
'************************************************* ****************************
'ADC

DEFINE ADC_BITS 10 ' Set number of bits in result ( 8 or 10 bits )
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

'************************************************* ****************************
'ADC parameters

'ADCON0=%11000001 ' clock source
ADCON1=%10000000 ' set VREF Off, porta.0 and .1 analog and left justify result
ANSEL=%00000001 ' Porta.0 and .1 used by ADC

'************************************************* ****************************
' DEFINE LCD pins
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4

DEFINE LCD_COMMANDUS 2000
'************************************************* ****************************

INCLUDE "modedefs.bas"

'settings for serial comm off for now

'DEFINE debug_reg PORTA
'DEFINE debug_bit 2
'DEFINE debug_baud 9600
'DEFINE debug_mode 1

'************************************************* ****************************
' variables

i var byte
x var byte
dummy var word
databit var bit
databyte var byte
va1 var word
va2 var word
tempa var word
tempd var word
tempc var word
store var word
DQ var byte[9]
crccalc var byte
negbit var tempd.bit11
signc var byte
signf var byte
crcerror var byte

'tempin con 0

CLEAR

'************************************************* ****************************
'I/O's

PORTA=0
PORTB=0
TRISA=%00000101
TRISB=%00000001

'************************************************* ****************************
'PINS
analogin var PORTA.0
a1 var PORTA.1
digitalin var PORTA.2
a3 var PORTA.3
a4 var PORTA.4
MCLR var PORTA.5
led1 var PORTA.6
led2 var PORTA.7

but var PORTB.0 '
memo var PORTB.1 '
ebit var PORTB.2 '
rsbit var PORTB.3
lcd1 var PORTB.4 '
lcd2 var PORTB.5 '
lcd3 var PORTB.6 '
lcd4 var PORTB.7

'************************************************* ****************************

start:
pause 200 'init LCD
lcdout $fe,1 'clear LCD
led1=0
led2=0

'************************************************* *

mease:


if but=1 then
gosub rec
endif

analog:

ADCIN 0, tempa

va2=tempa
va1=va2
va1 = (va1 */ 5000 ) >> 2


digital:
OWOUT digitalin,1,[$CC,$44] 'TELL SENSOR TO START A TEMPERATURE CONVERSION
OWOUT digitalin,1,[$CC,$BE] 'TELL SENSOR YOU WANT TO READ THE TEMPERATURE
OWIN digitalin,0,[STR DQ\9] 'RECEIVE TEMPERATURE FROM SENSOR

tempd.byte0=DQ[0]
tempd.byte1=DQ[1]

gosub GetCRC

gosub convert

gosub show

goto mease

GetCRC:

for x = 0 to 7 ; Get CRC for each of the 8 bytes
DataByte = DQ[x] ; Assign array pointer using value of x
gosub CalcCRC ; Get CRC value
next x ; Repeat until all bytes are done
if DQ[8] <> CRCCalc then ; Do the CRC values match?
CRCError = 1 ; Set flag indicating a problem with this sensor
else
CRCError = 0 ; Set flag indicating no problem with this sensor
endif
CRCCalc = 0 ; Reset CRC calculation variable
return

CalcCRC:
for i = 0 to 7 ; Do for all 8 bits in DataByte
DataBit = CRCCalc.0 ^ DataByte.0 ; XOR bit0 of DataByte and CRC
DataByte = DataByte >> 1 ; Position DataByte for next bit test
if DataBit = 0 then Shift ; If test bit not set, just shift CRCCalc
CRCCalc = CRCCalc ^ $18 ; If set, account for EXOR feedback

shift:

CRCCalc = CRCCalc >> 1 ; Shift right the CRCCalc byte
CRCCalc.7 = DataBit ; CRC bit 0 to bit bucket
next i ; Data bit rotates into CRC bit 7
return

convert:
if negbit = 1 then
SignC = "-"
else
SignC = "+"
endif
if SignC = "-" then
dummy = 0
tempd.Byte0 = tempd.Byte0 ^ 255
tempd.Byte1 = tempd.Byte1 ^ 255
dummy = 625 * tempd + 1
TempC = DIV32 100
else
dummy = 625 * tempd
TempC = DIV32 100
endif
return

show:

'lcdout $fe,2,"Analog ",dec va1," "
lcdout $fe,2,"tempd ",dec tempd," "
lcdout $fe,$c0,"tempc ",dec tempc," "
return

rec:
led1=1
return

end

******************************


Thanks