PDA

View Full Version : Another DS18B20 problem



ruijc
- 31st January 2008, 20:20
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

DynamoBen
- 1st February 2008, 01:29
I ran into this issue when I first used this sensor. Make 100% sure you have the DS18B20 hooked up correctly. I wired up my first and second sensor wrong and fried both of them.

b1arrk5
- 1st February 2008, 02:13
Bruce at Rentron has a great tutorial on using these chips on his website. It's worth a look!

Jerry.

Acetronics2
- 1st February 2008, 08:16
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


missing : the waiting loop for DS ready to send...

Alain

ruijc
- 1st February 2008, 11:02
Thanks all,

Alain, is this what you mean?

digital:

owout digitalin,1,[$CC,$44] 'TELL SENSOR TO START A TEMPERATURE CONVERSION

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

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

.

ruijc
- 1st February 2008, 23:57
I just tested the change but the result is the same :(

I've placed a led and added a : toggle led1 right at the beggining of the label digital.

I can see that if i remove the sensor's data conection the led freezes, so it means that the waiting lines are doing their job.

However i'm still stuck with the same values on my lcd :(

.

Acetronics2
- 2nd February 2008, 08:36
As the title says ... make a little SEARCH on this forum and you'll find tons of WORKING examples about DS 1820 and 18B20 or 18S20 ...

Then just "cut and paste" ... instead of trying to re-invent the wheel !!!

Alain

ruijc
- 2nd February 2008, 18:47
Thanks Alain,

But that's what i did in the first place...that's how i found CocaColaKid's example... and it didnt helped me much

.

skimask
- 2nd February 2008, 19:18
But that's what i did in the first place...that's how i found CocaColaKid's example... and it didnt helped me much

Hate to say it, but it must be a hardware or keyboard problem.