'Authors: Matthew and Tom Fox 'Date: April 18, 2009 'Program name: SmartStat-Plus 'Program version: 1.5.2 eeprom 0,(75) 'Default Air flow eeprom 1,(79) 'Default Fahrenhiet set temperature eeprom 2,(23) 'Default Celsius set temperature symbol SystemFailureTempDiff_F = 6 symbol SystemFailureTempDiff_C = 4 symbol HighTempAlert_F = 96 symbol HighTempAlert_C = 35 symbol FreezeAlert_F = 32 symbol FreezeAlert_C = 0 symbol MaxCycle = 5 symbol AirFlowDelayTime = 120'3 seconds symbol HystersisPosition_0 = 1 symbol HystersisPosition_1 = 2 symbol MaxCycleOverRide_F = 3 symbol MaxCycleOverRide_C = 2 symbol SPI_Out = 2 symbol SPI_Clk = 1 symbol Latch = 0 symbol UpButton = pin6 symbol DownButton = pin5 symbol ACHeat = 7 symbol LED2 = 6 'yellow symbol LED3 = 5 'clear orange symbol LED4 = 4 'red symbol LED5 = 3 'green symbol CycleCount = B9 CycleCount = 0 symbol AirFlowTimer = W8 AirFlowTimer = 0 symbol LoopCounter = B10 LoopCounter = 0 symbol OutByte = B0 symbol NumberOrChrOut = B3 symbol Hystersis = B12 symbol ChrFlag = BIT8 '0=number 1=table look up symbol CelsiusFlag = BIT9 '0=fahrenheit 1=celsius symbol ACLowAirFlowFlag = BIT10 '1=Turn off AC if low air flow 0=Keep on symbol HeatLowAirFlowFlag = BIT11 '1=Turn off Heat if low air flow 0=Keep on symbol ACHeatWarningFlag = BIT12 '1=Turn off AC/Heat if Warning light(s) on 0=Keep on symbol ACFlag = BIT13 '1=AC 0=Heat symbol ProblemFlag = BIT14 symbol ACHeatOnFlag = BIT15 symbol AirFlowFlag = BIT16 ProblemFlag = 0 ACHeatOnFlag = 0 AirFlowFlag = 0 symbol CurTemp = B4 symbol SumTemp = W11 symbol Temp = W10 symbol AirFlow = B5 symbol SetTemp = B6 symbol SetAirFlow = B7 symbol W = W9 symbol T = B27 symbol K = B26 symbol L = B25 'init timer settimer 49910 setintflags %10000000,%10000000 timer = 65535 - 3600 'Display codes '0 1 2 3 4 5 6 7 8 9 A F S C "" table 0,(192,249,164,176,153,146,130,248,128,152,136,142,146,198,255) 'Startup ChrFlag = 1 NumberOrChrOut = 13 gosub Display NumberOrChrOut = 12 gosub Display for K = 1 to 5 for L = 2 to 5 high L pause 100 low L next L next K 'Get all the settings CelsiusFlag = not pin0 ACLowAirFlowFlag = not pin1 HeatLowAirFlowFlag = not pin2 ACHeatWarningFlag = not pin3 if pin4 = 1 then Hystersis = HystersisPosition_0 else Hystersis = HystersisPosition_1 end if ACFlag = pin7 if CelsiusFlag = 1 then read 0,SetAirFlow,T,SetTemp else read 0,SetAirFlow,SetTemp end if 'main loop do gosub ReadTemperature gosub ReadAirFlow 'dispay temperature in Fahrenheit or Celsius if CelsiusFlag = 0 then ChrFlag = 0 NumberOrChrOut = CurTemp gosub display pause 1000 else LoopCounter = LoopCounter + 1 if LoopCounter = 4 then ChrFlag = 1 NumberOrChrOut = 13 gosub display NumberOrChrOut = 14 gosub display LoopCounter = 0 end if pause 1000 ChrFlag = 0 NumberOrChrOut = CurTemp gosub display end if 'timer for air flow sensor if AirFlowTimer > 0 then AirFlowTimer = AirFlowTimer + 1 If AirFlowTimer >= AirFlowDelayTime then AirFlowFlag = 1 AirFlowTimer = 0 end if end if 'Problem flag will be set if low air flow if AirFlowFlag = 1 then if AirFlow < SetAirFlow then if ACLowAirFlowFlag = 1 or HeatLowAirFlowFlag = 1 then ProblemFlag = 1 end if high LED2 else ProblemFlag = 0 low LED2 end if end if 'Problem flag will be set if differance between SetTemp and 'CurTemp is to great if CelsiusFlag = 1 then T = SystemFailureTempDiff_C else T = SystemFailureTempDiff_F end if if ACFlag = 1 then Temp = SetTemp + T MAX 120 If CurTemp => Temp then high LED3 if ACHeatWarningFlag = 1 then ProblemFlag = 1 end if else low LED3 ProblemFlag = 0 end if end if if ACFlag = 0 then Temp = SetTemp - T MIN 0 If CurTemp <= Temp then high LED3 if ACHeatWarningFlag = 1 then ProblemFlag = 1 end if else low LED3 ProblemFlag = 0 end if end if 'Dangerously high temperature check if CelsiusFlag = 1 then if CurTemp => HighTempAlert_C then high LED4 if ACHeatWarningFlag = 1 then ProblemFlag = 1 end if else ProblemFlag = 0 low LED4 end if else if CurTemp => HighTempAlert_F then high LED4 if ACHeatWarningFlag = 1 then ProblemFlag = 1 end if else ProblemFlag = 0 low LED4 end if end if 'freeze alert! if CelsiusFlag = 1 then if CurTemp =< FreezeAlert_C then high LED5 else low LED5 end if else if CurTemp =< FreezeAlert_F then high LED5 else low LED5 end if end if 'controling AC or Heat code If ProblemFlag = 0 then 'no problem so continue 'AC control If ACFlag = 1 then 'If it is realy HOT forget the MaxCycle! if CelsiusFlag = 1 then Temp = SetTemp + MaxCycleOverRide_C else Temp = SetTemp + MaxCycleOverRide_F end if if CurTemp >= Temp and CycleCount > MaxCycle then CycleCount = MaxCycle end if 'To hot turn on AC if CurTemp > SetTemp and CycleCount <= MaxCycle and ACHeatOnFlag = 0 then CycleCount = CycleCount + 1 AirFlowTimer = 1 ACHeatOnFlag = 1 high ACHeat end if 'Just right! you can turn it off now Temp = SetTemp - Hystersis if CurTemp =< Temp then AirFlowTimer = 0 AirFlowFlag = 0 ACHeatOnFlag = 0 Low ACHeat end if else 'Heat control 'If it is realy COLD forget the MaxCycle! if CelsiusFlag = 1 then Temp = SetTemp - MaxCycleOverRide_C else Temp = SetTemp - MaxCycleOverRide_F end if if CurTemp <= Temp and CycleCount > MaxCycle then CycleCount = MaxCycle end if 'To cold turn on Heat if CurTemp < SetTemp and CycleCount <= MaxCycle and ACHeatOnFlag = 0 then CycleCount = CycleCount + 1 AirFlowTimer = 1 ACHeatOnFlag = 1 high ACHeat end if 'Just right! you can turn it off now Temp = SetTemp + Hystersis if CurTemp >= Temp then AirFlowTimer = 0 AirFlowFlag = 0 ACHeatOnFlag = 0 Low ACHeat end if end if else 'Problem! turn it all off! low ACHeat end if 'Code for settings If UpButton = 0 or DownButton = 0 then pause 500 If UpButton = 0 and DownButton = 0 then 'set Air Flow level ChrFlag = 1 NumberOrChrOut = 11 gosub display NumberOrChrOut = 10 gosub display pause 3000 do gosub ReadAirFlow ChrFlag = 0 NumberOrChrOut = AirFlow gosub display pause 500 loop until UpButton = 0 or DownButton = 0 for L = 1 to 10 ChrFlag = 0 NumberOrChrOut = SetAirFlow gosub display Pause 500 if UpButton = 0 then SetAirFlow = SetAirFlow + 1 MAX 99 MIN 1 L = 1 end if if DownButton = 0 then SetAirFlow = SetAirFlow - 1 MIN 1 MAX 99 L = 1 end if ChrFlag = 1 NumberOrChrOut = 14 gosub display NumberOrChrOut = 14 gosub display pause 500 next L write 0,SetAirFlow else 'set temp for L = 1 to 10 ChrFlag = 0 NumberOrChrOut = SetTemp gosub display Pause 500 if UpButton = 0 then SetTemp = SetTemp + 1 MAX 94 MIN 10 L = 1 end if if DownButton = 0 then SetTemp = SetTemp - 1 MIN 10 MAX 94 L = 1 end if ChrFlag = 1 NumberOrChrOut = 14 gosub display NumberOrChrOut = 14 gosub display pause 500 next L If CelsiusFlag = 1 then write 2,SetTemp else write 1,SetTemp end if end if end if loop Display: if ChrFlag = 0 then T = NumberOrChrOut % 10 readtable T, OutByte gosub CodeOut T = NumberOrChrOut / 10 readtable T, OutByte gosub CodeOut else readtable NumberOrChrOut, OutByte gosub CodeOut end if pulsout Latch, 10 return CodeOut: for K = 1 to 8 if bit7 = 1 then high SPI_Out else low SPI_Out end if OutByte = OutByte * 2 high SPI_Clk low SPI_Clk next K return ReadTemperature: SumTemp = 0 Temp = 0 for K = 1 to 63 readadc10 0,Temp SumTemp = SumTemp+Temp next K CurTemp = SumTemp/63/2 MIN 0 for L = 1 to 9 SumTemp = 0 Temp = 0 for K = 1 to 63 readadc10 0,Temp SumTemp = SumTemp+Temp next K Temp = SumTemp/63/2 CurTemp = Temp + CurTemp/2 MIN 0 next L if CelsiusFlag = 1 then CurTemp = CurTemp - 32 * 5 / 9 MIN 0 end if return ReadAirFlow: Temp = 0 AirFlow = 0 SumTemp = 0 for K = 1 to 63 readadc10 2,Temp SumTemp = SumTemp+Temp next K AirFlow = SumTemp/63 MIN 0 SumTemp = 0 for K = 1 to 63 readadc10 1,Temp SumTemp = SumTemp+Temp next K Temp = SumTemp/63 MIN 0 AirFlow = AirFlow - Temp MIN 0 AirFlow = 100 - AirFlow MIN 0 return Interrupt: CycleCount = 0 toflag = 0 setintflags %10000000,%10000000 Timer = 65535 - 3600 return