PDA

View Full Version : Ds18b20 + 16f628a



Max Power
- 14th July 2009, 20:29
Hey everybody,

Ok, so I was encouraged to see working programs for the DS18B20 here on the fourm. Now I'm trying to make one work on a 16F628A, and I'm getting stuck. I grabbed the code at the bottom of this thread called 'DS18B20' by Ahmed salah (the most recent 18B20 post). I also tried the bit of code from fratello in that same post. I've got pbp running in MPlab, pickit2, connected to a olimex pic-18 development board. I started with a character lcd connected, but now I'd just like to get temperature data out over RS-232..

What I'm seeing is that when the first owout command is executed, you see data on the one-wire that never stops... I never get to the 'waiting for presence pulse' debug message. It's like I'm in an infinite loop sending data.

also, what is modedefs.bas? I don't have any file named modedefs, but it compiles Ok.

Thanks a lot,
Max Power



here is the code



'PIC16F628A Configuration
' ================
@ __CONFIG _HS_OSC & _MCLRE_ON & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF

DEFINE OSC 20 ' external oscillator

DEFINE DEBUG_REG PORTB 'RS-232 output on PORTB.4
DEFINE DEBUG_BIT 4
DEFINE DEBUG_BAUD 19200
DEFINE DEBUG_MODE 0 'not inverted. using RS-232 module


DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 100


CMCON=7

TRISB = %00000100
TRISA = %00000000

DEBUG REP $00\8,13,10,"Power Up"


INCLUDE "modedefs.bas"

Temperature1 Var Word ' Temperature storage
TempC Var Word
Float Var Word
Sign Var Byte ' +/- sign
Mode Var Byte ' 0=Temp. display, 1=Set Temp, 2=Set Hysteresis
DQ Var PORTB.0 ' One-wire data pin
Serial Var PORTB.1
Twist Var Bit
Dummy Var Byte
V Var Word
Busy Var BIT

DS18B20_1_12bit CON %01111111 ; 750ms, 0.0625°C (default)

PushB var PORTB.2
REDLED var PORTB.3

REDLED = 1
pause 1000

REDLED = 0

Twist = 0

MainLoop:

DEBUG REP $00\8,13,10,"Starting 'MainLoop'"




DEBUG REP $00\8,13,10,"push button to run owout"
gosub waitfor_b_press


'''''''''''''OWOut DQ, 1, [$CC, $4E,DS18B20_1_12bit]

OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_1_12bit]
Output DQ ' Make Pin Output
DQ=0 ' OneWire line Low
PauseUs 480 ' Keep down for 480 µS
Input DQ ' Make Pin Input
PauseUs 70 ' Wait 70 µS


DEBUG REP $00\8,13,10,"waiting for presence pulse"



If DQ=1 then ' No presence pulse from DS1820
DEBUG REP $00\8,13,10,"No sensor" ' Show message
Pause 1000 ' Wait 1 Sec.
Goto MainLoop ' Check again
EndIf

DEBUG REP $00\8,13,10,"sensor is good"
gosub waitfor_b_press




'Main :
Init1:
''''''''''''''''''''OWOut DQ, 1, [$CC,$4E,$FF,$FF,$7F]
''''''''''''''''''''OWOut DQ, 1, [$CC,$4E,DS18B20_1_12bit]
OWOUT DQ, 1, [$CC,$4E, 0, 0, DS18B20_1_12bit]
OWOut DQ, 1, [$CC,$48]
OWOut DQ, 1, [$CC,$B8]
OWOut DQ, 1, [$CC,$BE]
Pause 1000
'''''''''''''''''''''OWin DQ, 0, [Temperature1.Byte0, Temperature1.Byte1]
OWIn DQ, 2, [temperature1.byte0, temperature1.byte1]
DEBUG REP $00\8,13,10," sensor OK"
'================================================= ================================================== ===============

Main :
Part1:
OWOut DQ, 1, [$CC,$44] ' Start temperature conversion Sensor 1

WaitLoop:
OWIn DQ, 4, [Busy] ' Check for still busy converting

REDLED = 1 ' Show waiting for conv complete

If Busy = 0 Then

GOTO waitloop

ENDIF

REDLED = 0

OWOut DQ, 1, [$CC,$BE]
Pause 500
'''''''''''''''''''''''OWIn DQ, 0, [Temperature1.Byte0, Temperature1.Byte1]
OWIn DQ, 2, [temperature1.byte0, temperature1.byte1]


'''''''''''''''''''''''
Twist = 0
'''''''''''''''''''''''

If Temperature1.15 then
Temperature1= ~Temperature1 +1
Twist = 1
Endif

Dummy = 625 * Temperature1
TempC = DIV32 10
TempC = (Temperature1 & $7FF) >> 4
Float = ((Temperature1.Lowbyte & $0F ) * 25 )>>2
Temperature1 = TempC*100 + Float
If Twist then
V= 10000 - Temperature1 ' 25 C=12500 0 C=10000 -10 C=9000
Twist = 0
else
V= 10000 + Temperature1
EndIf
If V >= 10000 then ' Above 0 C.
Temperature1=V-10000
Else
Temperature1=10000-V ' Below 0 C.
EndIf
GoSub SelectSign
DEBUG REP $00\8,13,10, "IN ", Sign, DEC (Temperature1 / 100), ".", DEC2 Temperature1, " ",223,"C "
DEBUG REP $00\8,13,10, BIN16 Temperature1

END

'================================================= ================================================== ================
'GoSub SelectSign ' +/blank/- Sign
'Goto Main ' Oare merge pus asta aici ?
'Goto MainLoop ' Do it forever

'================================================= ================================================== =======================
SelectSign:
If v = 10000 then ' Temperature = 0 C.
Sign=" " ' No sign
Else
If v < 10000 then ' <> 0
Sign="-" ' Temperature below 0 C.
Else
Sign="+" ' Temperature above 0 C.
EndIf
EndIf
Return


waitfor_b_press:

REDLED = 1
WHILE PushB = 1
WEND

REDLED = 0

RETURN

Darrel Taylor
- 14th July 2009, 20:48
Ehhh, I don't think so ...

I see some of my code in there, and some of bruce's code in there, but it doesn't end up working right.

You should start off with something that we know works ...

http://www.rentron.com/PicBasic/one-wire3.htm

hth,

Acetronics2
- 14th July 2009, 20:52
Hi,

I think only very, very , very quick reading able eyes could see your message ...

try to place a " line feed " past your messages ...

that will help you to understand what's going on.

Alain

Max Power
- 14th July 2009, 22:16
So quick..thanks guys

DT: yeah, this is a mess, thanks for pointing me to the rentron site. I've been there before, but I forgot that bruce had this for us. and thanks for all that you do here. I'm just starting to play with instant interrupts.

Ace: Thanks, but I'm seeing my debug messages just fine.

Ok, I trashed that old program and brought in the new one from bruce at rentron. I don't have a serial LCd, so I changed the outputs to debug outputs. sure, there is some lcd formatting characters in there, but I'll worry about that later.

Now it's the same thing. It does not look like I get past the first owout command. I never see my '*******' message. I do see the ones before it though. I power the board, then I see 'Power Up' and 'start convert' and then nothing...but there is a never ending stream of data on the one wire...


here is the new code:


' PIC16F628A Configuration
' ================
@ __CONFIG _HS_OSC & _MCLRE_ON & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_OFF

DEFINE DEBUG_REG PORTB 'RS-232 output on PORTB.4
DEFINE DEBUG_BIT 4
DEFINE DEBUG_BAUD 19200
DEFINE DEBUG_MODE 0 'not inverted. using RS-232 module


DEFINE OSC 20 ' external oscillator

CMCON=7

TRISB = %00000100
TRISA = %00000000

DEBUG REP $00\8,13,10,"Power Up"


'************************************************* ***************
'* Name : Temp.BAS *
'* Author : Bruce Reynolds *
'* Notice : Copyright (c) 2003 Reynolds Electronics *
'* : All Rights Reserved *
'* Date : 7/28/2003 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************


Comm_Pin VAR PortB.0 ' One-wire Data-Pin "DQ" on PortB.0
Busy VAR BIT ' Busy Status-Bit
R_Temp VAR WORD ' RAW Temperature readings
TempC VAR WORD ' Temp in deg C
TempF VAR WORD ' Temp in deg F
Float VAR WORD ' Holds remainder for + temp C display
Cold_Bit VAR R_Temp.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Real_Cold CON 1 ' Define Real_Cold = 1

Deg CON 223 ' Data to display Deg ° symbol
CLR CON 1 ' CLR LCD command

Sign VAR BYTE ' +/- sign for temp display
Dummy VAR BYTE ' Dummy for Div32



DEBUG REP $00\8,13,10,"start convert"

Start_Convert:
OWOUT Comm_Pin, 1, [$CC, $44]' Skip ROM search & do temp conversion

DEBUG REP $00\8,13,10,"****************"

Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!
OWOUT Comm_Pin, 1, [$CC, $BE]' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [R_Temp.Lowbyte, R_Temp.Highbyte]' Read two bytes / end comms
GOSUB Convert_Temp
GOTO Start_Convert

Convert_Temp: ' +32.0 to +257 F
IF Cold_Bit = Real_Cold THEN Yikes ' If Cold_Bit = 1, it's below "0" deg C
Sign = "+"
Dummy = 625 * R_Temp ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
Dummy = 1125 * R_Temp
TempF = DIV32 100
IF TempF >6795 THEN ' Over 99.5 deg F..?
TempF = TempF + 3200

DEBUG REP $00\8,13,10," TempF = ",Sign,DEC TempF DIG 4,DEC TempF DIG 3,DEC TempF DIG 2,".",DEC2 TempF,Deg,"F "
ELSE
TempF = TempF + 3200
DEBUG REP $00\8,13,10," TempF = ",Sign,DEC TempF DIG 3,DEC TempF DIG 2,".",DEC2 TempF,Deg,"F "
ENDIF
TempC = (R_Temp & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((R_Temp.Lowbyte & $0F) * 625) ' Lower 4-bits of result * 625
DEBUG REP $00\8,13,10, " TempC = ",Sign,DEC TempC,".",DEC Float,Deg,"C "
DEBUG REP $00\8,13,10, "Raw", IBIN16 R_Temp
RETURN

Yikes: ' Display full range -C to -F conversion
Sign = "-" ' Display - symbol for negative temp
Dummy = 625 * ~R_Temp+1' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
TempF = ~R_Temp / 16 ' Begin conversion from -C to deg +/-F
IF TempF >=18 THEN ' Check for -degrees F "-18 C = -0.4 F"
TempF = ((((TempF + 50) * 9) /5) -122) ' -C to -F below -17 deg C
DEBUG REP $00\8,13,10, " TempF = ",Sign, DEC TempF,Deg,"F "
DEBUG REP $00\8,13,10, " TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3,".",DEC3 TempC,Deg,"C "
DEBUG REP $00\8,13,10, "Raw", IBIN16 R_Temp
ELSE ' Else result = +deg F
TempF = ((((-TempF + 50) * 9) /5) -58)' -C to +F below 32.0 deg F to -17 deg C
DEBUG REP $00\8,13,10, " TempF = ","+",DEC TempF,Deg,"F "
DEBUG REP $00\8,13,10, " TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3,".",DEC3 TempC,Deg,"C "
DEBUG REP $00\8,13,10, "Raw", IBIN16 R_Temp
ENDIF
RETURN

END

Bruce
- 15th July 2009, 00:14
Are you 100% sure you have your sensor wired right?

Max Power
- 15th July 2009, 15:53
I checked and re-checked, and I think I have it hooked up right. I have the DS18B20Z, with the SOIC package, so I have GND on pin 5, the PIC's 5V on pin3, and the one wire line on pin 4. I have a 4.7k ohm res pulling the one wire line up to 5V. The one wire line is connected to PIC portB.0. I bought 2 sensors from digikey, and I tried both of them, the same thing happens with each one. I power up the board, then in hyperterminal I see:

Power Up
start convert

and then nothing else, but there is lots of activity on the one wire line.

Bruce
- 15th July 2009, 18:01
I found a bug a while back where OWOUT would sit in a nasty loop when any osc > 8MHz
was defined. It's been fixed since, but it sounds like you have a version that has this bug.

Download & install the patch. Should fix you right up....;o}
http://www.melabs.com/support/patches.htm

Max Power
- 15th July 2009, 18:44
That sounds exactly like what is happening.... and lo and behold, I installed the patch and it works now, using that same code.

Bruce, your website has such a wealth of info. Thanks for keeping it available.

and yes, I figured out what modedefs.bas is for... (I asked in my first post)

and now I just need to decide what to do with this little gem!

Thank you all for your help. you got a beer waiting for you in Milwaukee ... but don't worry, the beer wouldn't be from Milwaukee...hahaha.

Bruce
- 16th July 2009, 00:10
Glad you got it working...;o}


I just need to decide what to do with this little gem!

Make a bunch & sell them. You would be really surprised at how many folks are out
there that pay good $ for embedded control solutions.

P.S. Be careful mentioning BEER here. Darrel has a beer radar....;o}

Darrel Taylor
- 16th July 2009, 11:29
I don't think he has anything to worry about ... :D

<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="509" HEIGHT="421" ><PARAM NAME="MOVIE" VALUE="http://www.pbpgroup.com/files/Flash/Milwaukee.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="true"><PARAM NAME="QUALITY" VALUE="high"><EMBED SRC="http://www.pbpgroup.com/files/Flash/Milwaukee.swf" WIDTH="509" HEIGHT="421" PLAY="true" LOOP="true" WMODE="opaque" QUALITY="high" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>