Advertisement
mike2545

Greenhouse vents

Aug 13th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 9.40 KB | None | 0 0
  1. '****************************************************************
  2. '*  Name    : UNTITLED.BAS                                      *
  3. '*  Author  : Mike Mummert                                      *
  4. '*  Notice  : Copyright (c) 2013 under penelty of death         *
  5. '*          : All Rights Reserved                               *
  6. '*  Date    : 6/30/2013                                         *
  7. '*  Version : 1.0                                               *
  8. '*  Notes   :                                                   *
  9. '*          :                                                   *
  10. '****************************************************************
  11.  
  12.     INCLUDE "modedefs.bas"
  13.     include "ALLDIGITAL.pbp"
  14.  
  15.     DEFINE DEBUG_REG PORTB  
  16.     DEFINE DEBUG_BIT 3
  17.     DEFINE DEBUG_BAUD 9600
  18.     ' Set Debug mode: 0 = true, 1 = inverted
  19.     DEFINE DEBUG_MODE 1
  20.     DEFINE OSC 4
  21.      OSCCON =$60 'clock speed
  22. '_______ports
  23.     'TX VAR portB.1
  24.    ' RX VAR PortB.4
  25.     lcd var PortA.6
  26.     DQ var PortA.0
  27.     Clock var PortA.1
  28.     RESET var PortA.2
  29.     OpenRelay var PortB.2
  30.     CloseRelay Var portB.1
  31.     up_button var PortA.3
  32.     Down_button var PortA.4
  33. '_______Cons
  34.     Baud CON 84 ' 9600 baud, non-inverted
  35.     BaudMode CON 84   ' 9600 baud, non-inverted
  36.     RdTmp CON $AA ' read temperature
  37.     WrHi CON $01 ' write TH (high temp)
  38.     WrLo CON $02 ' write TL (low temp)
  39.     RdHi CON $A1 ' read TH
  40.     RdLo CON $A2 ' read TL
  41.     RdCntr CON $A0 ' read counter
  42.     RdSlope CON $A9 ' read slope
  43.     StartC CON $EE ' start conversion
  44.     StopC CON $22 ' stop conversion
  45.     WrCfg CON $0C ' write config register
  46.     RdCfg CON $AC ' read config register
  47.     DegSym CON 186 ' degrees symbol
  48. ' -----[ Variables ]-------------------------------------------------------
  49.     tempIn VAR Word ' raw temperature
  50.     sign VAR tempIn.BIT8 ' 1 = negative temperature
  51.     tC VAR Word ' Celsius
  52.     tF VAR Word ' Fahrenheit
  53.     program VAR Byte
  54.     tempup VAR Byte
  55.     tempdown VAR Byte
  56.     temp VAR Word
  57.     timelmt VAR Word
  58.     result var word
  59.     myTemp var byte
  60.     tempAdj_bit var byte
  61.     tempAdj_bit =0
  62.     Attn var byte
  63.     time_bit var bit
  64.     hyst VAR BYTE
  65.     hyst =8
  66.     mode var byte
  67.     backlight_bit var bit
  68.     mem var byte
  69.     step1 var bit
  70.     step2 var bit
  71.     step3 var bit
  72.     step4 var bit
  73.    
  74.     multiplier_switch var PortB.0
  75.     step1 = 0
  76.     step2 = 0
  77.     step3 = 0
  78.     step4 = 0
  79.     'thermistor vars
  80.        co0 VAR Word
  81. co0 = 162
  82. co1top VAR Word
  83. co1top = 255
  84. co1btm VAR Word
  85. co1btm = 2125
  86. co2bt1 VAR Word
  87. co2bt1 = 25
  88. co2top VAR Word
  89. co2top = 3
  90. co2btm VAR Word
  91. co2btm = 50
  92. thermvar0 var word
  93. thermvar1 var word
  94.  
  95. ' -----[ Initialization ]--------------------------------------------------
  96.  
  97. Setup:  
  98.     pause 1000
  99.     serout2 lcd,baud,["?B64"]'set backlight intensity
  100.        Pause 500
  101.     serout2 lcd,baud,["?f"]'clear screen
  102.     pause 200
  103.     serout2 lcd,baud,["?c0"]  'no cursor
  104.      serout2 lcd,baud,["?G216"]
  105.   HIGH RESET ' alert the DS1620
  106.   SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run
  107.   LOW RESET
  108.   PAUSE 10
  109.   HIGH RESET
  110.   SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions
  111.   LOW RESET
  112.   'lcd setup
  113.  
  114.    
  115.   pause 200                            
  116.   serout2 lcd,baud,["?f"]
  117.   READ 1, mytemp 'read target from mem
  118.   IF mytemp > 75 THEN mytemp = 73
  119. ' -----[ Program Code ]----------------------------------------------------
  120. Main:
  121.    debug dec step1,dec step2,dec step3,dec step4, cr
  122. timelmt=0  'button time out zero-ing
  123.  serout2 lcd,baud,["?x00?y1Target ?x08?y1",dec myTemp]
  124.  pause 1000
  125.  
  126.  GOSUB Read_DS1620 ' get the temperature
  127.  
  128.  serout2 lcd,baud,["?x08?y1","  "] 'Blink :
  129.  pause 500
  130.  GOSUB Check_temp 'compare set temp with actual
  131.  
  132.  
  133. IF Down_button=0 OR up_button =0 THEN
  134.   GOSUB inpress
  135.   IF tempadj_bit = 1 THEN
  136.   WRITE 1,mytemp  'write target from mem only if changed
  137.   debug "Writing", cr
  138.   tempadj_bit = 0
  139.   endif
  140. ENDIF
  141.    
  142. GOTO main
  143. ' -----[ Subroutines ]-----------------------------------------------------
  144. Read_DS1620:
  145.         HIGH RESET ' alert the DS1620
  146.   SHIFTOUT DQ, Clock, LSBFIRST, [WrCfg, %10] ' use with CPU; free-run
  147.   LOW RESET
  148.   PAUSE 10
  149.   HIGH RESET
  150.   SHIFTOUT DQ, Clock, LSBFIRST, [StartC] ' start conversions
  151.   LOW RESET
  152.  
  153.       HIGH RESET ' alert the DS1620
  154.       SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp
  155.       SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in
  156.      
  157.       IF tempIn =0 THEN GOTO read_ds1620
  158.       ''tempin = tempin -2  'calibration attempt..unit running 2 degrees on the warm side, attempting -2
  159.       LOW RESET ' release the DS1620
  160.       tempIn.BYTE1 = -sign ' extend sign bit
  161.       tC = tempIn * 5 ' convert to tenths
  162.       IF (tC.BIT15 = 0) THEN ' temp C is positive
  163.         tF = tC */ $01CC + 320 ' convert to F
  164.       ELSE ' temp C is negative
  165.         tF = 320 - ((ABS tC) */ $01CC) ' convert to F
  166.       ENDIF
  167.     '-----------------thermistor code
  168.      high portA.7
  169.      pause 1
  170.       RCTIME portA.7,1,thermvar0 ' 46 is the scale factor.
  171.  
  172. DEBUG DEC thermvar0, cr, cr
  173.    
  174. '_____Display Temp on LCD
  175.  
  176.  
  177.      serout2 lcd,baud, ["?x00?y0Temperature ",DEC (ABS tF / 10)," "] 'display temp on lcd
  178.     RCSTA.4=0
  179.     RCSTA.4=1  
  180.  
  181. RETURN
  182.  
  183. Check_temp:
  184. IF multiplier_switch = 1 THEN
  185.     IF  (ABS tF / 10) => mytemp+2 THEN  
  186.     debug "in high",cr
  187.        IF  step1 = 0  THEN
  188.        low closerelay
  189.        high OpenRelay
  190.           pause 10000
  191.           low openrelay
  192.           step1=1
  193.           endif
  194.      endif    
  195.       IF  (ABS tF / 10) =< mytemp THEN    
  196.            IF step1 =1 THEN
  197.            low openrelay
  198.            high CloseRelay
  199.            step1 = 0
  200.            step2 = 0
  201.            step3 = 0
  202.            step4 = 0
  203.           endif
  204.     endif
  205.     IF   (ABS tF / 10) => mytemp +4 THEN
  206.        IF  step2 = 0  THEN
  207.        low closerelay
  208.        high OpenRelay
  209.           pause 10000
  210.           low openrelay
  211.           step2=1
  212.           endif
  213.       endif
  214.        IF (ABS tF / 10) =< mytemp +2 THEN  
  215.            IF step2 =1 THEN
  216.            low openrelay
  217.            high CloseRelay
  218.            pause 8750
  219.            low closerelay
  220.            step2=0
  221.           endif
  222.     endif
  223.     IF  (ABS tF / 10) => mytemp +6 THEN
  224.        IF  step3 = 0  THEN
  225.        low closerelay
  226.        high OpenRelay
  227.           pause 10000
  228.           low openrelay
  229.           step3=1
  230.           endif
  231.      endif
  232.      IF  (ABS tF / 10) =< mytemp +4 THEN    
  233.            IF step3 =1 THEN
  234.            low openrelay
  235.            high CloseRelay
  236.            pause 8750
  237.            low closerelay
  238.            step3=0
  239.           endif
  240.     endif
  241.     IF (ABS tF / 10) => mytemp +8 THEN
  242.       IF  step4 = 0  THEN
  243.        low closerelay
  244.        high OpenRelay
  245.           step4=1
  246.           endif
  247.      endif
  248.       IF (ABS tF / 10) =< mytemp +6 THEN    
  249.            IF step4=1 THEN
  250.            low openrelay
  251.            high CloseRelay
  252.            pause 8750
  253.            low closerelay
  254.            step4=0
  255.           endif
  256.     endif
  257.   endif
  258.   IF multiplier_switch = 0 THEN
  259.    IF  (ABS tF / 10) => mytemp+2 THEN  
  260.     debug "in low",cr
  261.        IF  step1 = 0  THEN
  262.        low closerelay
  263.        high OpenRelay
  264.           pause 7500
  265.           low openrelay
  266.           step1=1
  267.           endif
  268.      endif    
  269.       IF  (ABS tF / 10) =< mytemp THEN    
  270.            IF step1 =1 THEN
  271.            low openrelay
  272.            high CloseRelay
  273.            step1 = 0
  274.            step2 = 0
  275.            step3 = 0
  276.            step4 = 0
  277.           endif
  278.     endif
  279.     IF   (ABS tF / 10) => mytemp +4 THEN
  280.        IF  step2 = 0  THEN
  281.        low closerelay
  282.        high OpenRelay
  283.           pause 5000
  284.           low openrelay
  285.           step2=1
  286.           endif
  287.       endif
  288.        IF (ABS tF / 10) =< mytemp +2 THEN  
  289.            IF step2 =1 THEN
  290.            low openrelay
  291.            high CloseRelay
  292.            pause 4375
  293.            low closerelay
  294.            step2=0
  295.           endif
  296.     endif
  297.     IF  (ABS tF / 10) => mytemp +6 THEN
  298.        IF  step3 = 0  THEN
  299.        low closerelay
  300.        high OpenRelay
  301.           pause 5000
  302.           low openrelay
  303.           step3=1
  304.           endif
  305.      endif
  306.      IF  (ABS tF / 10) =< mytemp +4 THEN    
  307.            IF step3 =1 THEN
  308.            low openrelay
  309.            high CloseRelay
  310.            pause 4375
  311.            low closerelay
  312.            step3=0
  313.           endif
  314.     endif
  315.     IF (ABS tF / 10) => mytemp +8 THEN
  316.       IF  step4 = 0  THEN
  317.        low closerelay
  318.        high OpenRelay
  319.          
  320.           step4=1
  321.           endif
  322.      endif
  323.       IF (ABS tF / 10) =< mytemp +6 THEN    
  324.            IF step4=1 THEN
  325.            low openrelay
  326.            high CloseRelay
  327.            pause 4375
  328.            low closerelay
  329.            step4=0
  330.           endif
  331.     endif
  332.   endif  
  333. RETURN
  334.  
  335. inpress:
  336.       timelmt=timelmt +1
  337.       IF timelmt >5000 THEN
  338.       RETURN
  339.        endif
  340.        IF up_button = 0 THEN GOTO plus
  341.        IF down_button =0 THEN GOTO minus
  342.        GOTO inpress
  343. plus:  
  344.       mytemp=mytemp +1
  345.       IF mytemp>85 THEN mytemp =85
  346.       timelmt=0
  347.       serout2 lcd,baud,["?x08?y1",dec myTemp]
  348.       pause 500
  349.       tempAdj_bit = 1
  350.       hyst =15
  351.      GOTO inpress
  352. minus:
  353.     mytemp=mytemp -1
  354.     IF mytemp <40 THEN mytemp =40
  355.      timelmt=0
  356.       serout2 lcd,baud,["?x08?y1",dec myTemp]
  357.       pause 500
  358.       tempAdj_bit = 1
  359.       hyst = 5
  360.      GOTO inpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement