PDA

View Full Version : Help with 18b20 Stuck with busy bit



xapmanis
- 7th October 2012, 17:50
Hello there, im kind new with pics but i have some knowledge about basic schematics and programming.
- I use pic6F88
- Oscilator 20Mhz
-And for testing reasons because i dont have an lcd screen i use TX/RX pseudo-RS232

I have started a project that i need to read 18b20's temp and do something else... but i stuck in busy bit 1 month now, and can't find any solution :'(.

Im kinda desperate because i dont know what else to check....

Here is my code :

include "modedefs.bas"
'---------------------------------------------------------------
' DEFINES
'---------------------------------------------------------------
TRISA=%00000010
TRISB=%00000100
CMCON=0
ANSEL=0
ADCON0=0
DEFINE OSC 20
'---------------------------------------------------------------
' Variables
'---------------------------------------------------------------

present var bit
count_remain var byte
count_per_c var byte
sign1 var byte
tempout var byte
temp var byte
t05 var byte
t05out var byte
tempb20 VAR WORD
DQ VAR PORTA.1
RX VAR PORTB.2
TX VAR PORTB.5

'--------------------------------------------------------------
' Initialisation
'--------------------------------------------------------------
CLEAR
serout tx,n9600,["TEST OK",13,10]
pause 1500
'-------------------------------------------------------------
' Main prog
'-------------------------------------------------------------
main:
while 1
gosub sensor_ch1
IF present=1 then
gosub mainloop1
tempout=temp
t05out=t05
else
tempout=99
t05out=9
endif
Serout2 tx,16468,["T=",dec2 tempout,",",t05out,"C",13,10]
pause 250

wend
'================================================= =
' Subroutines
'================================================= =

'------------------------------------------------------------------------------------------
' Presence
'------------------------------------------------------------------------------------------

sensor_ch1:
Low DQ 'Set the data pin low to initialize
PauseUs 500 'Wait for more than 480us
TRISA.1=1 'Release the data pin (set to input for high)
PauseUs 100 'Wait for more than 60us
IF DQ=1 Then 'Is there a DS1820 detected Present=1
present=0 'If not, then clear DS_VALID flag Presebt=0
Else
present=1
EndIF
Return 'And return with DS_Valid holding 0
'---------------------------------------------------------------------------------------------------
' Read 18B20 Sensor
'---------------------------------------------------------------------------------------------------
mainloop1:
TRISA.1=0 'read temp ch0 connect it to RA.1
OWOut DQ, 1, [$CC, $44] 'Start temperature conversion
waitloop1:
pauseus 200
OWOut DQ, 1, [$CC, $BE] 'Read the temperature
TRISA.1=1
OWIn DQ, 0, [tempb20.LOWBYTE, tempb20.HIGHBYTE, Skip 4, count_remain, count_per_c]
IF tempb20.15=1 Then
sign1=1
if tempb20.3=1 and tempb20.2=0 and tempb20.1=0 and tempb20.0=0 then
t05="5"
else
t05="0"
endif
tempb20=tempb20 >> 4
tempb20=tempb20.lowbyte ^ 255'Reverse the bits of the lowbyte of TEMP because negative
temp=tempb20.lowbyte
Else
sign1=0
if tempb20.3=1 then t05="5"
if tempb20.3=0 then t05="0"
tempb20=tempb20 >> 4
temp=tempb20.lowbyte
EndIF
Return
eND

Heckler
- 8th October 2012, 00:01
you might take a look at this example... (located in the wiki section under projects/temperature)

http://www.picbasic.co.uk/forum/content.php?r=374-How-to-read-a-one-wire-DS18B20-temperature-sensor-or-nine-of-them

you can also search on "ds18b20" for other examples.
I would suggest you go through your code and add lots of comments to help you trouble shoot and for others to follow what you are trying to do.

xapmanis
- 8th October 2012, 04:11
Ty Heckler for the reply, but the link that you gave me my friend doesn't work. Stuck in loading... I deleted comments for posting on site i have done them properly :P :P

My problem is the presence pulse, i can't understand how it works even i have read the datasheet hundrend of times.

I do low for initialise, i do get a low and going for the converting procedure. But i never get the busy bit back, even i did it with 2 diffrent ways.

I suspect that the problem is the presence pulse but i dont know how to check it and that's why i came with a post (i also have read a couple of other threads but still no luck) .

fratello
- 8th October 2012, 06:31
Take a look here, for a verry good example : http://www.rentron.com/PicBasic/one-wire3.htm

xapmanis
- 9th October 2012, 14:52
I think i found out the problem.... I haven't disabled the A/D inputs nor the Comparators and OPTION_REG . I'll try it out when i have time and ill let you know :) !!!