Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '****************************************************************
- '* Name : Thermostat unit.BAS *
- '* Author : Mike Mummert *
- '* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
- '* : All Rights Reserved *
- '* Date : 10/3/2011 *
- '* Version : 1.0 PBP *
- '* Notes : 18F1320 / DS1620 Thermostat chip *
- '* : Modern Device 4X20 Lcd *
- '****************************************************************
- INCLUDE "modedefs.bas"
- include "ALLDIGITAL.pbp"
- INCLUDE "DT_INTS-18.bas" ; BASE Interrupt SYSTEM
- INCLUDE "ReEnterPBP-18.bas" ; Include IF USING PBP interrupts
- 'INCLUDE "ReEnterPBP-18LP.bas" ; Include if using Low Pr. PBP INTS
- Define PULSIN_MAX 1000
- DEFINE HSER_RCSTA 90h 'Hser receive status init
- DEFINE HSER_TXSTA 24h 'Hser transmit status init
- DEFINE HSER_BAUD 9600 'Hser baud rate
- define HSER_SPBRG 25
- Define HSER_CLROERR 1
- DEFINE DEBUG_REG PORTA
- DEFINE DEBUG_BIT 0
- DEFINE DEBUG_BAUD 9600
- ' Set Debug mode: 0 = true, 1 = inverted
- DEFINE DEBUG_MODE 1
- DEFINE OSC 4
- OSCCON =$60 'clock speed
- '_______ports
- 'TX VAR portB.1
- ' RX VAR PortB.4
- lcd var PortA.6
- DQ var PortA.0
- Clock var PortA.1
- RESET var PortA.2
- relay var PortB.2
- up_button var PortA.3
- Down_button var PortA.4
- '_______Cons
- Baud CON 84 ' 9600 baud, non-inverted
- BaudMode CON 84 ' 9600 baud, non-inverted
- RdTmp CON $AA ' read temperature
- WrHi CON $01 ' write TH (high temp)
- WrLo CON $02 ' write TL (low temp)
- RdHi CON $A1 ' read TH
- RdLo CON $A2 ' read TL
- RdCntr CON $A0 ' read counter
- RdSlope CON $A9 ' read slope
- StartC CON $EE ' start conversion
- StopC CON $22 ' stop conversion
- WrCfg CON $0C ' write config register
- RdCfg CON $AC ' read config register
- DegSym CON 186 ' degrees symbol
- ' -----[ Variables ]-------------------------------------------------------
- tempIn VAR Word ' raw temperature
- sign VAR tempIn.BIT8 ' 1 = negative temperature
- tC VAR Word ' Celsius
- tF VAR Word ' Fahrenheit
- program VAR Byte
- tempup VAR Byte
- tempdown VAR Byte
- temp VAR Word
- timelmt VAR Word
- result var word
- myTemp var byte
- tempAdj_bit var byte
- tempAdj_bit =0
- hour var byte
- Minute var byte
- Nb_var02 var byte
- Attn var byte
- time_bit var bit
- hyst VAR BYTE
- hyst =8
- mode var byte
- Day var byte
- backlight_bit var bit
- mem var byte
- mem1 var byte
- mem2 var byte
- mem3 var byte
- mem4 var byte
- mem5 var byte
- mem6 var byte
- Nb_var93 var byte
- 'System Heat / AC 0 = AC 1 = heat
- x var word
- myTemp = 70
- '--------[ ASM Interrups ]---------------------
- ASM
- INT_LIST macro ; IntSource, Label, TYPE, ResetFlag?
- INT_Handler RX_INT, _read_serial, PBP, yes
- endm
- INT_CREATE ; Creates the interrupt processor
- ENDASM
- T0CON = %10010010
- @ INT_ENABLE RX_INT ; enable external (RX) interrupts
- ' -----[ Initialization ]--------------------------------------------------
- Setup:
- pause 1000
- serout2 lcd,baud,["?B64"]'set backlight intensity
- Pause 500
- serout2 lcd,baud,["?f"]'clear screen
- pause 200
- serout2 lcd,baud,["?c0"] 'no cursor
- 'serout2 lcd,baud,["?C1 Web-Thermostat"]
- 'pause 1500
- serout2 lcd,baud,["?G420"]
- HIGH RESET ' alert the DS1620
- SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run
- LOW RESET
- PAUSE 10
- HIGH RESET
- SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions
- LOW RESET
- 'lcd setup
- pause 200
- serout2 lcd,baud,["?f"]
- ' -----[ Program Code ]----------------------------------------------------
- Main:
- 'debug "here",cr
- IF mem = 1 THEN 'turn up the lcd backlight
- IF mem <> backlight_bit THEN
- backlight_bit = mem
- serout2 lcd,baud,["?B96"]
- endif
- endif
- IF mem = 0 THEN 'turn up the lcd backlight
- IF mem <> backlight_bit THEN
- backlight_bit = mem
- serout2 lcd,baud,["?B11"]
- endif
- endif
- timelmt=0 'button time out zero-ing
- FOR x = 1 TO 2420
- pause 1
- NEXT
- GOSUB Read_DS1620 ' get the temperature
- debug dec backlight_bit,cr
- 'serout2 lcd,baud,["?x14?y3"," "] 'Blink :
- 'pause 500
- IF Nb_var93 = 0 THEN LOW relay
- IF Nb_var93 = 1 THEN GOSUB Check_temp 'compare set temp with actual
- @ INT_DISABLE RX_INT
- IF Down_button=0 OR up_button =0 THEN
- GOSUB inpress
- 'serout2 lcd,baud,["?x14?y3"," "] 'Blink :
- ENDIF
- IF time_bit = 1 THEN 'only go through this routin if you have new data (and did not time out)
- IF mode = 1 THEN
- serout2 lcd,baud,["?x14?y0","Wake "]
- endif
- IF mode = 2 THEN
- serout2 lcd,baud,["?x14?y0","Leave "]
- endif
- IF mode = 3 THEN
- serout2 lcd,baud,["?x14?y0","Return"]
- endif
- IF mode = 4 THEN
- serout2 lcd,baud,["?x14?y0","Sleep "]
- endif
- IF hour < 12 THEN
- serout2 lcd,baud,["?x18?y3","AM"]
- endif
- IF hour = 12 THEN
- serout2 lcd,baud,["?x18?y3","PM"]
- endif
- IF hour > 12 THEN
- hour = hour -12
- serout2 lcd,baud,["?x18?y3","PM"]
- endif
- IF hour =00 THEN
- hour = 12
- serout2 lcd,baud,["?x18?y3","AM"]
- endif
- IF day =1 THEN serout2 lcd,baud,["?x12?y2","Mon "]
- IF day =2 THEN serout2 lcd,baud,["?x12?y2","Tues"]
- IF day =3 THEN serout2 lcd,baud,["?x12?y2","Wed "]
- IF day =4 THEN serout2 lcd,baud,["?x12?y2","Thrs"]
- IF day =5 THEN serout2 lcd,baud,["?x12?y2","Fri "]
- IF day =6 THEN serout2 lcd,baud,["?x12?y2","Sat "]
- IF day =7 THEN serout2 lcd,baud,["?x12?y2","Sun "]
- time_bit =0
- endif
- serout2 lcd,baud,["?x12?y3",dec2 hour,":",dec2 minute,"?x11?y0",dec myTemp,"?x11?y1"]
- @ INT_ENABLE RX_INT
- GOTO main
- ' -----[ Subroutines ]-----------------------------------------------------
- Read_DS1620:
- HIGH RESET ' alert the DS1620
- SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp
- SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in
- ''tempin = tempin -2 'calibration attempt..unit running 2 degrees on the warm side, attempting -2
- LOW RESET ' release the DS1620
- tempIn.BYTE1 = -sign ' extend sign bit
- tC = tempIn * 5 ' convert to tenths
- IF (tC.BIT15 = 0) THEN ' temp C is positive
- tF = tC */ $01CC + 320 ' convert to F
- ELSE ' temp C is negative
- tF = 320 - ((ABS tC) */ $01CC) ' convert to F
- ENDIF
- '_____Display Temp on LCD
- @ INT_DISABLE RX_INT
- serout2 lcd,baud,["?x01?y1?>3"] 'set up lcd to display big numbers
- @ INT_ENABLE RX_INT
- FOR x = 1 TO 220
- pause 1
- NEXT
- @ INT_DISABLE RX_INT
- serout2 lcd,baud, [DEC (ABS tF / 10),"?<"] 'display temp on lcd and exit big number mode
- Pause 75
- RCSTA.4=0
- RCSTA.4=1
- @ INT_ENABLE RX_INT
- RETURN
- Check_temp:
- IF mytemp > (ABS tF / 10) THEN
- hyst = hyst +1
- IF hyst => 15 AND relay =0 THEN
- HIGH relay
- ENDIF
- IF hyst> 15 THEN hyst =15
- ENDIF
- IF mytemp < (ABS tF / 10)THEN
- hyst = hyst -1
- IF hyst =<5 AND relay = 1 THEN
- LOW relay
- ENDIF
- IF hyst <5 THEN hyst =5
- ENDIF
- RETURN
- read_serial:
- hserin 250,read_serial2,[ x] 'look for bootloader activity
- debug dec x,cr
- IF x = 193 THEN 'reset the pic to enter bootloader mode
- @ RESET
- endif
- read_serial2:
- HSERIN 1000,No232, [WAIT(":"),Nb_var02,dec2 hour,dec2 minute, mode,Day,mem,Nb_var93] 'get temp setting from Master
- IF tempAdj_bit=0 THEN '___who pressed which button when
- mytemp = Nb_var02
- endif
- IF nb_var02 > 85 OR nb_Var02 < 45 THEN mytemp = 60
- Send_serial:
- hserout [(ABS tF / 10) , myTemp]'send mytemp to Master
- tempAdj_bit = 0
- time_bit =1
- ' debug ".",cr
- @ INT_RETURN
- No232:
- RCSTA.4=0
- RCSTA.4=1
- debug "Timeout",cr
- @ INT_RETURN
- inpress:
- timelmt=timelmt +1
- IF timelmt >5000 THEN
- @ INT_ENABLE RX_INT
- RETURN
- endif
- IF up_button = 0 THEN GOTO plus
- IF down_button =0 THEN GOTO minus
- GOTO inpress
- plus:
- mytemp=mytemp +1
- IF mytemp>85 THEN mytemp =85
- timelmt=0
- serout2 lcd,baud,["?x11?y0",dec myTemp]
- pause 500
- tempAdj_bit = 1
- hyst =15
- GOTO inpress
- minus:
- mytemp=mytemp -1
- IF mytemp <40 THEN mytemp =40
- timelmt=0
- serout2 lcd,baud,["?x11?y0",dec myTemp]
- pause 500
- tempAdj_bit = 1
- hyst = 5
- GOTO inpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement