I would love your feedback on the proposed code below. I am not a coder though I have done some coding, so a critical look would be much appreciated.
The purpose is to have a 5-minute timeout on the CS command. That means: a CS command only remains valid for 5 minutes. After that, it is reset to 0, which means that the thermostat's control setpoint message will be relayed unaltered. I am building an external control system, but in case communication is lost, the CS should automatically be cancelled.
So here is my proposed solution. What do you think?
Code: Select all
;#######################################################################
; Peripheral use
;#######################################################################
;TWOSEC contains the number of timer 0 overflows (with a 1:64 prescaler)
;that happen in roughly 2 seconds. This is the time the error LED will stay
;lit after an error has been detected.
constant TWOSEC=2*ONESEC
;FIVEMIN is the timeout for the external control system when
;CS override is cancelled
constant FIVEMIN=300*ONESEC ;Wim
...
;Variables for longer lasting storage
ExtCtrlIdleCounter res 1 ;Wim
...
;########################################################################
; Main program
;########################################################################
...
movlw ONESEC
movwf SecCounter ;Initialize second counter
movlw FIVEMIN ;Wim
movwf ExtCtrlIdleCounter ;Wim - Inilialize timeout
...
; IdleTimer is called whenever timer 0 overflows
IdleTimer bcf INTCON,TMR0IF ;Clear Timer 0 overflow flag
decfsz SecCounter,F ;Decrease second counter
goto IdleTimerJ1
decfsz ExtCtrlIdleCounter,F ;Wim - Decrease timeoutcounter
goto IdleTimerJ0 ;Wim
clrf controlsetpt1 ;Wim
clrf controlsetpt2 ;Wim - Cancel external control
movlw FIVEMIN ;Wim
movwf ExtCtrlIdleCounter ;Wim - Reset timeoutcounter
IdleTimerJ0 ;Wim
btfsc BoilerAlive ;Ever received a message from boiler?
...
;************************************************************************
; Parse commands received on the serial interface
;************************************************************************
...
SetCtrlSetpoint call GetFloatArg
skpnc
return
btfsc NegativeTemp ;Check for a negative value
retlw SyntaxError ;Negative temperatures are not allowed
movwf controlsetpt1
movfw float2
movwf controlsetpt2
movlw FIVEMIN ;Wim - reset ext. control timeout timer
movwf ExtCtrlIdleCounter ;Wim - good external command received
goto CommandFloat