Advertisement
mike2545

Themostat Slave unit

Oct 3rd, 2011
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 9.42 KB | None | 0 0
  1. '****************************************************************
  2. '*  Name    : Thermostat unit.BAS                               *
  3. '*  Author  : Mike Mummert                                      *
  4. '*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
  5. '*          : All Rights Reserved                               *
  6. '*  Date    : 10/3/2011                                         *
  7. '*  Version : 1.0 PBP                                           *
  8. '*  Notes   : 18F1320 / DS1620 Thermostat chip                  *
  9. '*          : Modern Device 4X20 Lcd                            *
  10. '****************************************************************
  11.    
  12.    
  13.    
  14.     INCLUDE "modedefs.bas"
  15.     include "ALLDIGITAL.pbp"
  16.     INCLUDE "DT_INTS-18.bas"     ; BASE Interrupt SYSTEM
  17.     INCLUDE "ReEnterPBP-18.bas"     ; Include IF USING PBP interrupts
  18.     'INCLUDE "ReEnterPBP-18LP.bas"  ; Include if using Low Pr. PBP INTS
  19.  
  20.    
  21.     Define PULSIN_MAX 1000
  22.     DEFINE HSER_RCSTA 90h   'Hser receive status init
  23.     DEFINE HSER_TXSTA 24h   'Hser transmit status init
  24.     DEFINE HSER_BAUD 9600  'Hser baud rate
  25.     define HSER_SPBRG 25
  26.     Define HSER_CLROERR 1
  27.     DEFINE DEBUG_REG PORTA  
  28.     DEFINE DEBUG_BIT 0
  29.     DEFINE DEBUG_BAUD 9600
  30.     ' Set Debug mode: 0 = true, 1 = inverted
  31.     DEFINE DEBUG_MODE 1
  32.     DEFINE OSC 4
  33.      OSCCON =$60 'clock speed
  34. '_______ports
  35.     'TX VAR portB.1
  36.    ' RX VAR PortB.4
  37.     lcd         var PortA.6
  38.     DQ          var PortA.0
  39.     Clock       var PortA.1
  40.     RESET       var PortA.2
  41.     relay       var PortB.2
  42.     up_button   var PortA.3
  43.     Down_button var PortA.4
  44.    
  45. '_______Cons
  46.     Baud CON 84 ' 9600 baud, non-inverted
  47.     BaudMode CON 84   ' 9600 baud, non-inverted
  48.     RdTmp CON $AA ' read temperature
  49.     WrHi CON $01 ' write TH (high temp)
  50.     WrLo CON $02 ' write TL (low temp)
  51.     RdHi CON $A1 ' read TH
  52.     RdLo CON $A2 ' read TL
  53.     RdCntr CON $A0 ' read counter
  54.     RdSlope CON $A9 ' read slope
  55.     StartC CON $EE ' start conversion
  56.     StopC CON $22 ' stop conversion
  57.     WrCfg CON $0C ' write config register
  58.     RdCfg CON $AC ' read config register
  59.     DegSym CON 186 ' degrees symbol
  60. ' -----[ Variables ]-------------------------------------------------------
  61.     tempIn VAR Word ' raw temperature
  62.     sign VAR tempIn.BIT8 ' 1 = negative temperature
  63.     tC VAR Word ' Celsius
  64.     tF VAR Word ' Fahrenheit
  65.     program VAR Byte
  66.     tempup VAR Byte
  67.     tempdown VAR Byte
  68.     temp VAR Word
  69.     timelmt VAR Word
  70.     result var word
  71.     myTemp var byte
  72.     tempAdj_bit var byte
  73.     tempAdj_bit =0
  74.     hour var byte
  75.     Minute var byte
  76.     Nb_var02 var byte
  77.     Attn var byte
  78.     time_bit var bit
  79.     hyst VAR BYTE
  80.     hyst =8
  81.     mode var byte
  82.     Day var byte
  83.     backlight_bit var bit
  84.     mem var byte
  85.     mem1 var byte
  86.     mem2 var byte
  87.     mem3 var byte
  88.     mem4 var byte
  89.     mem5 var byte
  90.     mem6 var byte
  91.     Nb_var93 var byte
  92.     'System Heat / AC    0 = AC 1 = heat
  93.     x var word
  94.    
  95. myTemp = 70
  96. '--------[ ASM Interrups ]---------------------
  97. ASM
  98. INT_LIST  macro    ; IntSource,        Label,  TYPE, ResetFlag?
  99.         INT_Handler    RX_INT,    _read_serial,   PBP,  yes
  100.        
  101.  
  102.    endm
  103.     INT_CREATE             ; Creates the interrupt processor
  104. ENDASM
  105. T0CON = %10010010
  106. @   INT_ENABLE   RX_INT     ; enable external (RX) interrupts
  107.  
  108.  
  109. ' -----[ Initialization ]--------------------------------------------------
  110.  
  111.  
  112.  
  113. Setup:
  114.    
  115.     pause 1000
  116.     serout2 lcd,baud,["?B64"]'set backlight intensity
  117.        Pause 500
  118.     serout2 lcd,baud,["?f"]'clear screen
  119.     pause 200
  120.     serout2 lcd,baud,["?c0"]  'no cursor
  121.    'serout2 lcd,baud,["?C1 Web-Thermostat"]
  122.    'pause 1500
  123.      serout2 lcd,baud,["?G420"]
  124.   HIGH RESET ' alert the DS1620
  125.   SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run
  126.   LOW RESET
  127.   PAUSE 10
  128.   HIGH RESET
  129.   SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions
  130.   LOW RESET
  131.   'lcd setup
  132.  
  133.    
  134.   pause 200                            
  135.   serout2 lcd,baud,["?f"]
  136.  
  137. ' -----[ Program Code ]----------------------------------------------------
  138. Main:
  139. 'debug "here",cr  
  140. IF mem = 1 THEN  'turn up the lcd backlight
  141.  IF mem <> backlight_bit THEN
  142.      backlight_bit = mem
  143.        serout2 lcd,baud,["?B96"]
  144.        
  145.     endif
  146.  endif  
  147.  IF mem = 0 THEN  'turn up the lcd backlight
  148.  IF mem <> backlight_bit THEN
  149.      backlight_bit = mem
  150.        serout2 lcd,baud,["?B11"]
  151.        
  152.     endif
  153.  endif        
  154. timelmt=0  'button time out zero-ing
  155.   FOR x = 1 TO 2420
  156.  pause 1
  157.     NEXT
  158.  GOSUB Read_DS1620 ' get the temperature
  159.   debug dec backlight_bit,cr
  160.  'serout2 lcd,baud,["?x14?y3"," "] 'Blink :
  161.  'pause 500
  162.  IF Nb_var93 = 0 THEN LOW relay
  163.  IF Nb_var93 = 1 THEN GOSUB Check_temp 'compare set temp with actual
  164. @   INT_DISABLE   RX_INT
  165.  
  166. IF Down_button=0 OR up_button =0 THEN
  167.   GOSUB inpress
  168.   'serout2 lcd,baud,["?x14?y3"," "] 'Blink :
  169. ENDIF
  170.   IF time_bit = 1 THEN  'only go through this routin if you have new data (and did not time out)
  171.     IF mode = 1 THEN  
  172.       serout2 lcd,baud,["?x14?y0","Wake  "]
  173.        
  174.     endif
  175.         IF mode = 2 THEN  
  176.           serout2 lcd,baud,["?x14?y0","Leave "]
  177.                        
  178.         endif
  179.             IF mode = 3 THEN  
  180.               serout2 lcd,baud,["?x14?y0","Return"]
  181.              
  182.             endif
  183.                 IF mode = 4 THEN
  184.                   serout2 lcd,baud,["?x14?y0","Sleep "]
  185.                
  186.                 endif
  187.  
  188.             IF hour < 12 THEN
  189.                 serout2 lcd,baud,["?x18?y3","AM"]
  190.              endif
  191.         IF hour = 12 THEN
  192.             serout2 lcd,baud,["?x18?y3","PM"]
  193.         endif
  194.             IF hour > 12 THEN
  195.                 hour = hour -12
  196.                 serout2 lcd,baud,["?x18?y3","PM"]
  197.             endif
  198.     IF hour =00 THEN
  199.         hour = 12
  200.             serout2 lcd,baud,["?x18?y3","AM"]
  201.     endif
  202.  
  203.   IF day =1 THEN serout2 lcd,baud,["?x12?y2","Mon "]
  204.   IF day =2 THEN serout2 lcd,baud,["?x12?y2","Tues"]
  205.   IF day =3 THEN serout2 lcd,baud,["?x12?y2","Wed "]
  206.   IF day =4 THEN serout2 lcd,baud,["?x12?y2","Thrs"]
  207.   IF day =5 THEN serout2 lcd,baud,["?x12?y2","Fri "]
  208.   IF day =6 THEN serout2 lcd,baud,["?x12?y2","Sat "]
  209.   IF day =7 THEN serout2 lcd,baud,["?x12?y2","Sun "]
  210.   time_bit =0
  211. endif
  212.   serout2 lcd,baud,["?x12?y3",dec2 hour,":",dec2 minute,"?x11?y0",dec myTemp,"?x11?y1"]
  213.  
  214. @   INT_ENABLE   RX_INT
  215.    
  216. GOTO main
  217. ' -----[ Subroutines ]-----------------------------------------------------
  218. Read_DS1620:
  219.       HIGH RESET ' alert the DS1620
  220.       SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp
  221.       SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in
  222.       ''tempin = tempin -2  'calibration attempt..unit running 2 degrees on the warm side, attempting -2
  223.       LOW RESET ' release the DS1620
  224.       tempIn.BYTE1 = -sign ' extend sign bit
  225.       tC = tempIn * 5 ' convert to tenths
  226.       IF (tC.BIT15 = 0) THEN ' temp C is positive
  227.         tF = tC */ $01CC + 320 ' convert to F
  228.       ELSE ' temp C is negative
  229.         tF = 320 - ((ABS tC) */ $01CC) ' convert to F
  230.       ENDIF
  231.      
  232. '_____Display Temp on LCD
  233. @   INT_DISABLE   RX_INT
  234.  
  235.       serout2 lcd,baud,["?x01?y1?>3"]  'set up lcd to display big numbers
  236. @   INT_ENABLE   RX_INT      
  237.        FOR x = 1 TO 220
  238.       pause 1
  239.       NEXT
  240. @   INT_DISABLE   RX_INT      
  241.       serout2 lcd,baud, [DEC (ABS tF / 10),"?<"] 'display temp on lcd and exit big number mode
  242.       Pause 75
  243.     RCSTA.4=0
  244.     RCSTA.4=1  
  245. @   INT_ENABLE   RX_INT                                        
  246. RETURN
  247.  
  248. Check_temp:
  249.  
  250.     IF mytemp > (ABS tF / 10) THEN
  251.             hyst = hyst +1
  252.         IF hyst => 15 AND relay =0 THEN  
  253.             HIGH relay
  254.         ENDIF
  255.             IF hyst> 15 THEN hyst =15
  256.             ENDIF
  257.                 IF mytemp < (ABS tF / 10)THEN
  258.                     hyst = hyst -1
  259.                         IF hyst =<5 AND relay = 1 THEN
  260.                             LOW relay
  261.                 ENDIF
  262.                      IF hyst <5 THEN hyst =5
  263.                         ENDIF
  264. RETURN
  265.  
  266. read_serial:
  267.       hserin 250,read_serial2,[ x] 'look for bootloader activity
  268.       debug dec x,cr
  269.       IF x = 193   THEN 'reset the pic to enter bootloader mode
  270. @ RESET
  271. endif
  272. read_serial2:      
  273.       HSERIN 1000,No232, [WAIT(":"),Nb_var02,dec2 hour,dec2 minute, mode,Day,mem,Nb_var93] 'get temp setting from Master
  274.        
  275.       IF tempAdj_bit=0 THEN  '___who pressed which button when
  276.         mytemp = Nb_var02
  277.       endif
  278.      IF nb_var02 > 85 OR nb_Var02 < 45 THEN mytemp = 60
  279.      
  280. Send_serial:
  281.      hserout [(ABS tF / 10) , myTemp]'send mytemp to Master
  282.        tempAdj_bit = 0
  283.        time_bit =1
  284.       ' debug ".",cr
  285. @ INT_RETURN    
  286. No232:
  287. RCSTA.4=0
  288. RCSTA.4=1
  289.   debug "Timeout",cr
  290.  
  291. @ INT_RETURN  
  292.    
  293. inpress:
  294.       timelmt=timelmt +1
  295.       IF timelmt >5000 THEN
  296. @   INT_ENABLE   RX_INT  
  297.       RETURN
  298.        endif
  299.        IF up_button = 0 THEN GOTO plus
  300.        IF down_button =0 THEN GOTO minus
  301.        GOTO inpress
  302. plus:
  303.      
  304.       mytemp=mytemp +1
  305.       IF mytemp>85 THEN mytemp =85
  306.       timelmt=0
  307.       serout2 lcd,baud,["?x11?y0",dec myTemp]
  308.       pause 500
  309.       tempAdj_bit = 1
  310.       hyst =15
  311.      GOTO inpress
  312. minus:
  313.    
  314.     mytemp=mytemp -1
  315.     IF mytemp <40 THEN mytemp =40
  316.      timelmt=0
  317.       serout2 lcd,baud,["?x11?y0",dec myTemp]
  318.       pause 500
  319.       tempAdj_bit = 1
  320.       hyst = 5
  321.      GOTO inpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement