Thought the following code might interest someone?
It's based on the code for the DS18B20 by Bruce on the Reynolds/Renton web site.
The chip I used is the DS18B20-PAR ( the device is marked 'DS18B20P') and will only work with parasitic power. Achieved by using an 2N7000 MOSFET (gate on PORTC.1, source on the DQ line and the drain on +5 supply).
The DQ line must be driven high within 10uS of the 'convert' command and maintained for the duration of the conversion (750mS for 12 bit conversion).
The extra code to do this follows the conver command.
Code:
' -----[ Title ]-----------------------------------------------------
'
' File...... WVL Water Remote
' Purpose... Remote station for control of water heater
' Date...... June 2010
'
' 7. DS18B20P measures tank temp. Parasitic power only with DQ pulled hard
' high after 'read temp' command by MOSFET 2N7000 with gate on PORTC.1
' 8. The DS18B20P range +125C to -55C
' 9. The DQ line must be high for the conversion period. Periods and resolution:
' 9 bit 93.7mS 0.5C 1/2 degree
' 10 bit 187.5mS 0.25C 1/4 degree
' 11 bit 375.0mS 0.125C 1/8 degree
' 12 bit 750.0mS 0.0625C 1/16 degree
' 12 bit resolution used so conversion delay is 750mS and conversion factor is
' 625 that gives temperature in 1/10000 degrees C
' 10. The code is based on the DS18B20 pages at web site REYNOLDS/RENTON
'
' ----[ Includes / Defines ]-------------------------------------------
'
clear
define OSC 20 ' Use HSPLL during compilation
DEFINE LCD_DREG PORTB ' Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 4 ' Define first pin of connected to LCD
DEFINE LCD_RSREG PORTB ' Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 2 ' Define pin used for RS connection
DEFINE LCD_EREG PORTB ' Define PIC port used for E line of LCD
DEFINE LCD_EBIT 1 ' Define pin used for E connection
define LCD_RWREG PORTB ' Define PIC port used for RW
DEFINE LCD_RWBIT 3 ' Define pin used for RW
DEFINE LCD_BITS 4 ' Define 4 bit communication mode to LCD
DEFINE LCD_LINES 4 ' Define using a X line LCD
DEFINE LCD_COMMANDUS 1500 ' Define delay time between sending LCD commands
DEFINE LCD_DATAUS 44 ' Define delay time between data sent
FLAGS=0
include "modedefs.bas" ' Include serout defines
'
' ----[ Variables ]----------------------------------------------------
'
TempR VAR word ' DS18B20P reading. raw data
TempC VAR WORD ' Temp in deg C
Float VAR WORD ' Holds remainder for + temp C display
Heartbeat var PORTA.0 ' Heartbeat each cycle
DQ var PORTC.0 ' DS18B20P. Comms
Pullup var PORTC.1 ' DS18B20P
'
' -----[ Initialization - PIC ]--------------------------------------
'
ADCON1 = %00000110 ' All ports digital
CMCON = %00000111 ' Comparators off. Essential or PORTF won't work
'
' -----[ Identify ]--------------------------------------------------
'
LCDOUT $fe,$80,"Water Heater"
pause 1000
LCDOUT $fe,1
'
' *****************************************************************************
' * *
' * M A I N P R O G R A M *
' * *
' *****************************************************************************
Main:
GOSUB Get_temp
LCDOUT $fe,$c0,"Temp : ",DEC TempC,".",DEC Float
toggle Heartbeat
pause 1000
goto Main
' *****************************************************************************
' * *
' * S U B R O U T I N E S *
' * *
' *****************************************************************************
Get_temp:
OWOUT DQ,1,[$CC, $44] ' Skip ROM search & do temp conversion
high Pullup ' Turns MOSFET on to make DQ line high
pause 750 ' Conversion time for 12 bit reading
low Pullup ' Turn MOSFET off after read temp command
OWOUT DQ,1,[$CC, $BE] ' Skip ROM search & read scratchpad memory
OWIN DQ,2,[TempR.Lowbyte,TempR.Highbyte]' Read two bytes / end comms
Convert_Temp:
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
TempC = (TempR & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((TempR.Lowbyte & $0F) * 625) ' Lower 4-bits of result * 625
RETURN
END
The MCU is an PIC16F877A running at 20MHz.
Regards Bill Legge
Bookmarks