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
Bookmarks