Code:
'****************************************************************
'* Name : *
'* Author : Bruce Reynolds modified By Dwight Merkley *
'* Notice : *
'* : *
'* Date : 7/28/2003 *
'* Version : 1.0 *
'* Notes : PIC16f690 *
'****************************************************************
Reset:
TrisA = %00011000 'Port A3,A4 inupts rest outputs (A3 cannot output)
TrisB = %00000000 'Port B all outputs
TrisC = %00000000 'Port C all outputs
ansel = 0 'and turn off analog
AnselH = 0 'turn of rest of analogs
CM1CON0 = 0 'turn off comparators
CM2CON0 = 0 'turn off comparators
SSPCON.bit5 = 0 ' disable serial port, pins are I/O
OPTION_REG = %10000000 '1 turn off weak pull ups
INTCON = %00000000
'===========================================
DQ var PortA.4 ' One-wire Data-Pin "DQ" on PortA.4
Busy VAR BIT ' Busy Status-Bit
ID VAR BYTE[8] ' Array storage variable for 64-bit ROM code
DEFINE DEBUG_REGG PORTA 'set debug port to porta
DEFINE DEBUG_BIT 0 'use pin a0 of porta for debug
DEFINE DEBUG_BAUD 2400 'set baud rate to 2400
DEFINE DEBUG_MODE 0 'communicate in true mode
'=====================================================================
portC=0 'turn off LED's on breadboard
Begin:
PAUSE 500 ' Wait .5 second
Start_Convert
OWOUT DQ, 1, [$33] ' Issue Read ROM command
'///////////////////////////////////////////////////////////////////////////
' ' READ ROM [$33] [(from Maxim documentation)]
' 'This command can only be used when there is one slave on the bus.
' 'It allows the bus master to read the slave’s 64-bit ROM code without
' 'using the Search ROM procedure. If this command is used when there
' 'is more than one slave present on the bus, a data collision will
' 'occur when all the slaves attempt to respond at the same time.
'///////////////////////////////////////////////////////////////////////////
ID_Loop:
OWIN DQ, 0, [STR ID\8]' Read 64-bit device data into the 8-byte array "ID"
debug $0D,$0A 'do a linefeed and carrage return
DEBUG "64bit = ",HEX2 ID[0],HEX2 ID[1],HEX2 ID[2],HEX2 ID[3],HEX2 ID[4],HEX2 ID[5],HEX2 ID[6],HEX2 ID[7],"h",$0D,$0A
DEBUG "Family Code = ",HEX2 ID[0],"h",$0D,$0A
DEBUG "Ser# = ",HEX2 ID[1],HEX2 ID[2],HEX2 ID[3],HEX2 ID[4],HEX2 ID[5],HEX2 ID[6],"h",$0D,$0A
DEBUG "CRC Value = ",HEX2 ID[7],"h",$0D,$0A
debug ">>> the following is complete ID of device <<<",$0D,$0A
debug "[$",HEX2 ID[0],",$",HEX2 ID[1],",$",HEX2 ID[2],",$",HEX2 ID[3],",$",_
HEX2 ID[4],",$",HEX2 ID[5],",$",HEX2 ID[6],",$",HEX2 ID[7],"]",$0D,$0A
PAUSE 5000 ' 5-second pause, replace sensor for next read
GOTO Start_Convert
END
Code:
'****************************************************************
'* Name : Temp.BAS *
'* Author : Bruce Reynolds modified by Dwight Merkley *
'* Notice : Copyright (c) 2003 Reynolds Electronics *
'* : All Rights Reserved *
'* Date : 7/28/2003 *
'* Version : 1.0 *
'* Notes : code originates from www.rentron.com *
'* : PIC 16F690 *
'****************************************************************
Reset:
TrisA = %00011000 'Port A3,A4 inupts rest outputs (A3 cannot output)
TrisB = %00000000 'Port B all outputs
TrisC = %00000000 'Port C all outputs
ansel = 0 'and turn off analog
AnselH = 0 'turn of rest of analogs
CM1CON0 = 0 'turn off comparators
CM2CON0 = 0 'turn off comparators
SSPCON.bit5 = 0 ' disable serial port, pins are I/O
OPTION_REG = %10000000 '1 turn off weak pull ups
INTCON = %00000000
'===========================================
DQ var PortA.4 ' One-wire Data-Pin "DQ" on PortA.4
Busy VAR BIT ' Busy Status-Bit
Raw 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 Raw.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Sign VAR BYTE ' +/- sign for temp display
Dummy VAR BYTE ' Dummy for Div32
ID VAR BYTE[8] ' Array storage variable for 64-bit ROM code
sensor var byte
hexbyte var byte
col var byte
DEFINE DEBUG_REGG PORTA 'set debug port to porta
DEFINE DEBUG_BIT 0 'use pin a0 of porta for debug
DEFINE DEBUG_BAUD 2400 'set baud rate to 2400
DEFINE DEBUG_MODE 0 'communicate in true mode
'-------------------------------------------------------------------------------
'these constants are available to program the sensor for 9,10,11,12 bit resolution
'DS18B20_9bit CON %00011111 ; 93.75ms, 0.5°C
'DS18B20_10bit CON %00111111 ; 187.5ms, 0.25°C <-- My favorite
'DS18B20_11bit CON %01011111 ; 375ms, 0.125°C
'DS18B20_12bit CON %01111111 ; 750ms, 0.0625°C (default)
' use the statement below in your program to set the resolution
'OWOUT comm_pin, 1, [$CC, $4E, 0, 0, DS18B20_9bit] 'set resolution of sensor
'=================================================================
'============ Main Program loop ==================================
'=================================================================
portC=0 'turn off LED's on breadboard
'-----------------------------------------------------------------
Begin:
PAUSE 500 ' Wait .5 [1/2] seconds
Main
for sensor=1 to 9 'I am reading 9 DS18b20 sensors... change this to match how many sensors you are reading
portC=sensor 'use the 4 breadboard led's to count binary as each sensor is read
for hexbyte=0 to 7 'each sensor address is 8 bytes
gosub getid 'go look up each sensors address
id[hexbyte]=col 'load the ID array with the retrieved address byte
next hexbyte 'go get the rest of the address bytes
gosub readsensor 'now go read the current sensor
gosub DisplayTemp 'now display the temp of the current sensor
next sensor 'now go read another sensor
portC=0 'done with all 9 sensors... turn off led's
debug $0D,$0A 'reset terminal cursor to home, upper left
end 'end here... use reset button to read them again.
'============= subroutines ========================
ReadSensor:
OWOUT DQ, 1, [$55,str id\8,$44] 'instructs sensors to match[$55] this[ID] rom code and
'initiates[$44] temperature conversion on matching sensor
CkAgn:
OWIN DQ, 4, [busy] ' Check for still busy converting
IF busy = 0 THEN ckagn ' Still busy?, then loop
owout dq,1,[$55,str id\8,$BE] 'instructs sensors to match[$55] this[ID] and start sending back scratchpad[$BE]
OWIN DQ, 2, [Raw.LOWBYTE,Raw.HIGHBYTE]' Read two temperature bytes, then end communications
return
'-------------------------------------------------------------
DisplayTemp: ' +32.0 to +257 F
IF Cold_Bit = 1 THEN Cold ' If Cold_Bit = 1, it's below "0" deg C
Sign = "+"
Dummy = 625 * Raw ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
TempC = (Raw & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((Raw.Lowbyte & $0F) * 625) ' Lower 4-bits of result * 625
debug dec sensor," TempC = ",Sign,DEC TempC,".",DEC Float," C ",$0D,$0A
' debug "Raw ", IBIN16 Raw," Dec=",dec (Raw>>3),$0D,$0A 'display the raw value
RETURN
''------------------------------------------------------------
Cold: ' arrive here if temp is below zero C
Sign = "-" ' Display - symbol for negative temp
Dummy = 625 * ~Raw+1' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
debug dec sensor," TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3_
,".",DEC3 TempC," C ",$0D,$0A
' debug "Raw", IBIN16 Raw," Dec ",(Raw>>3),$0D,$0A
RETURN
'=============================================================
'below is a list of the addresses of each of the 9 sensors I am reading
'these must match the sensors you are trying to read...
'use "Read Rom" program to determine the addresses of each of these sensors
GetID:
Select Case sensor
Case 1 :LOOKUP hexbyte,[$28,$1E,$F5,$24,$03,$00,$00,$B8], col
Case 2 :LOOKUP hexbyte,[$28,$4E,$F4,$24,$03,$00,$00,$5B], col
Case 3 :LOOKUP hexbyte,[$28,$35,$FC,$24,$03,$00,$00,$05], col
Case 4 :LOOKUP hexbyte,[$28,$4D,$2C,$25,$03,$00,$00,$ED], col
Case 5 :LOOKUP hexbyte,[$28,$45,$F8,$24,$03,$00,$00,$82], col
Case 6 :LOOKUP hexbyte,[$28,$FC,$FC,$24,$03,$00,$00,$0C], col
Case 7: LOOKUP hexbyte,[$28,$BB,$02,$25,$03,$00,$00,$77], col
Case 8 :LOOKUP hexbyte,[$28,$32,$24,$25,$03,$00,$00,$6F], col
Case 9 :LOOKUP hexbyte,[$28,$4C,$A5,$24,$03,$00,$00,$6D], col
end select
return
Please note... the basis for the above programs came from Bruce Reynolds' website
Bookmarks