StudentSA
Member
Hi,
Im having trouble designing a control algorithm for a temperature controller.
My uC (PIC16F690) has functionality to allow the user to set a "setpoint" and it is integrated with a LM35 Temp Sensor. I read the 10bit A2D channel to get the temperature into a variable TEMP and TEMP+1. The user selected setpoint is stored in variable SETPOINT and SETPOINT+1. The control method works by driving the PWM pin to control the brightness of a globe. This is my control algorithm extract:
Currently my code really does not work "well" .
The functions CALC_CORR_ACT and SETUP_PWM_DUTY_CYCLE are called every two seconds using a timer interrupt.
Basically i am not sure what is the correct way to go about implementing this... I think I need to include a Derivative term to the control.
Any advice is appreciated.
Regards,
StudentSA
Im having trouble designing a control algorithm for a temperature controller.
My uC (PIC16F690) has functionality to allow the user to set a "setpoint" and it is integrated with a LM35 Temp Sensor. I read the 10bit A2D channel to get the temperature into a variable TEMP and TEMP+1. The user selected setpoint is stored in variable SETPOINT and SETPOINT+1. The control method works by driving the PWM pin to control the brightness of a globe. This is my control algorithm extract:
Code:
CALC_CORR_ACT: ; Determine the required corrective action
; Subtract Setpoint from TEMP
; Check if positive
; If positive Globe on
; If negative globe brightness down
; a TEMP b Setpoint
Calculate_Diffrence
MOVF TEMP,W
SUBWF SETPOINT, W
MOVWF ERRER
MOVF TEMP+1, W
BTFSS STATUS, C
INCFSZ TEMP+1, W ; Result was neg
SUBWF SETPOINT+1, W
MOVWF ERRER+1
BTFSS STATUS, C
GOTO NegativeResult
BCF FLAGS, 5
GOTO Determine_Error_Range
NegativeResult
COMF ERRER+1,F
BSF FLAGS, 5 ; Indicates a negative result
Determine_Error_Range
MOVF ERRER+1, F
BTFSS STATUS, Z
GOTO ErrorOutOfRange
BTFSS FLAGS,5
GOTO ErrerIsPos
ErrerIsNeg
COMF ERRER,F
INCF ERRER,F ; Make Errer Positive
ErrerIsPos
MOVF LEVEL4CORR, W
SUBWF ERRER, W
BTFSC STATUS, C
GOTO ErrorOutOfRange ; Result Pos so ERRER > 80 ( Level4 )
MOVF LEVEL4CORRVALUE,W ; Result Neg so ERRER LESS THAN 80
MOVWF DELTAPWM
MOVF LEVEL3CORR, W
SUBWF ERRER, W
BTFSC STATUS, C
GOTO ErrorInRange ; Result Pos so 80 > ERRER > 40 ( Level4 )
MOVF LEVEL3CORRVALUE,W ; Result Neg so ERRER LESS THAN 40
MOVWF DELTAPWM
MOVF LEVEL2CORR, W
SUBWF ERRER, W
BTFSC STATUS, C
GOTO ErrorInRange ; Result Pos so 40 > ERRER > 20 ( Level4 )
MOVF LEVEL2CORRVALUE,W ; Result Neg so ERRER LESS THAN 20
MOVWF DELTAPWM
MOVF LEVEL1CORR, W
SUBWF ERRER, W
BTFSC STATUS, C
GOTO ErrorInRange ; Result Pos so 20 > ERRER > 10 ( Level4 )
MOVF LEVEL1CORRVALUE,W ; Result Neg so ERRER LESS THAN 10
MOVWF DELTAPWM
MOVF ERRER,F
BTFSC STATUS, Z
CLRF DELTAPWM
GOTO ErrorInRange
ErrorInRange
BTFSC FLAGS,5
GOTO Errorwasneg ; NEG Error
MOVF DELTAPWM,W
SUBWF PWM,F
BTFSC STATUS, C
RETLW PWM
CLRF PWM
RETLW PWM
Errorwasneg
MOVF DELTAPWM,W
ADDWF PWM,F
BTFSS STATUS,C
RETLW PWM
MOVLW B'11111111'
MOVWF PWM
RETLW PWM
ErrorOutOfRange
BTFSS FLAGS,5
GOTO SetPWMOffAndExit ; Large Positive Error
SetPWMFullAndExit
MOVLW B'11111111'
MOVWF PWM
RETLW PWM
SetPWMOffAndExit
MOVLW B'00000000'
MOVWF PWM
RETLW PWM
SETUP_PWM_DUTY_CYCLE:
MOVF PWM,F
BTFSS STATUS,Z
GOTO PWMNotZero
BSF STATUS,RP0 ; Bank 1
BTFSS TRISC, 5
BSF TRISC, 5
BCF STATUS, RP0 ; Bank 0
RETLW 0
PWMNotZero
MOVF PWM,W
MOVWF CCPR1L ; 8 MSB of PWM
BSF STATUS,RP0 ; Bank 1
BTFSC TRISC, 5
BCF TRISC, 5
BCF STATUS, RP0 ; Bank 0
RETLW 0
Currently my code really does not work "well" .
The functions CALC_CORR_ACT and SETUP_PWM_DUTY_CYCLE are called every two seconds using a timer interrupt.
Basically i am not sure what is the correct way to go about implementing this... I think I need to include a Derivative term to the control.
Any advice is appreciated.
Regards,
StudentSA