# Help with DS18S20 and 16F628

Status
Not open for further replies.

#### ChriX

##### Member
Hey all, i'm trying to interface the Dallas DS18S20 with a PIC 16F628 and having some problems. I can't even get the PIC to detect the presense pulse generated by the sensor. Here's my code, i've got the data line of the sensor on bit 0 on porta.

Macros
Code:
TEMP_OUTPUT macro
BANK1							;Change to BANK1
bcf TRISA,0					;Set PORTA,0 to an output
BANK0							;Back to BANK0
bcf PORTA,0					;Set output pin LOW
endm

TEMP_INPUT macro
BANK1							;To BANK1 again
bsf TRISA,0					;Set PORTA,0 to an input
BANK0							;Back to BANK0
endm

Subroutine
Code:
OW_INIT
TEMP_OUTPUT
DELAY_MICRO 250
DELAY_MICRO 250
TEMP_INPUT
DELAY_MICRO 60
btfsc PORTA,0
call NO_OW_DETECT
TEMP_OUTPUT
return

The NO_OW_DETECT routine is just sending something to an attached LCD to tell me the sensor has not been found, I haven't been able to get it to skip that at all yet, even though the sensor should be pulling that pin low.

Hopefully someone with some experience with these can help me out.

#### kinjalgp

##### Active Member
Making 1-wire devices work is very difficult if you are doing it for the first time. I have a very bad experience with them. It takes a lot of trial and error cycles to write your own routines to communicate with them because timings are very critical over here. Make sure that your delay 250uS routines are much accurate. Also count the over-head that it takes in calling a delay routine and suctract it from the delay count because all timings are in micro-seconds so over-head makes a lot of difference. In 8051 calling a subroutine takes 24uS@12MHz and if you don't consider that it doesn't work.

#### ChriX

##### Member

Do you think i'm waiting too long and missing the pulse then? I learnt the delay routines i'm using here from veys.com, he explains them very well and they have worked great for me in the past.

Did you get yours going in the end? It says in the datasheet to hold the output pin low for 480uS minimum, but i'm holding for 500uS so that shouldn't be a problem. Then it says the sensor waits 15-60uS before pulling the line low after detecting the rising edge, so i've waited 60 there. Only other thing it says is to keep the master rx for 480uS minimum, which I haven't done because I didn't think it would matter, i've just switched it right back to an output after checking the pin to see if it's clear.

Will have a look at that link and another play.

#### kinjalgp

##### Active Member
Yes I got my setup running when I counted the over-head delay and subtracted it from delay count. My delay routine rounds off to 488uS for first step & 72uS for the second step.
My routine goes like this
DQ = 0
delay 480 ; ~488uS
DQ = 1
delay 60 ; ~72uS
present = DQ
delay 400 ; ~424uS
Stop

Present variable = 0 if DS1820 is present.

#### ChriX

##### Member
Present variable = 1?

Shouldn't that be 0 because the DS is pulling low? Or have we just found the reason why mine isn't working :lol:

#### kinjalgp

##### Active Member
Oh sorry! That should be 0.

#### ChriX

##### Member
Damn, thought i'd found something then. Oh well, time to check all my timings again then, it must be those that are at fault. I have checked them with ISIS simulating my code, and I graphed the output and it all seems ok, if only there was a 1-wire library for it.

#### motion

##### New Member
Hi, you can try the ff. code which was adapted from the 1-wire routines I have successfully used.

Code:
OW_INIT:
TEMP_OUTPUT
DELAY_MICRO 250
DELAY_MICRO 250
DELAY_MICRO 250
CLRWDT
TEMP_INPUT
WAIT4HI:
btfss PORTA,0
goto WAIT4HI
WAIT4LO:
btfsc PORTA,0
goto WAIT4LO
TEMP_OUTPUT
return

This sample code waits indefinitely for the hi-lo transistions. You should insert a timeout counter in the WAIT4HI and WAIT4LO loops.

Status
Not open for further replies.

Replies
12
Views
1K
Replies
90
Views
4K
Replies
16
Views
624
Replies
3
Views
532
Replies
25
Views
2K