Mercurial > public > hwos_code
comparison src/calibrate.asm @ 634:4050675965ea
3.10 stable release
| author | heinrichsweikamp |
|---|---|
| date | Tue, 28 Apr 2020 17:34:31 +0200 |
| parents | 185ba2f91f59 |
| children | 75e90cd0c2c3 |
comparison
equal
deleted
inserted
replaced
| 633:690c48db7b5b | 634:4050675965ea |
|---|---|
| 1 ;============================================================================= | 1 ;============================================================================= |
| 2 ; | 2 ; |
| 3 ; File calibration.asm combined next generation V3.08.8 | 3 ; File calibration.asm * combined next generation V3.09.4n |
| 4 ; | 4 ; |
| 5 ; o2 sensor calibration subroutines | 5 ; o2 sensor calibration subroutines |
| 6 ; | 6 ; |
| 7 ; Copyright (c) 2014, Heinrichs Weikamp, all right reserved. | 7 ; Copyright (c) 2014, Heinrichs Weikamp, all right reserved. |
| 8 ;============================================================================= | 8 ;============================================================================= |
| 12 #include "math.inc" | 12 #include "math.inc" |
| 13 #include "adc_lightsensor.inc" | 13 #include "adc_lightsensor.inc" |
| 14 #include "eeprom_rs232.inc" | 14 #include "eeprom_rs232.inc" |
| 15 | 15 |
| 16 | 16 |
| 17 calibrate CODE | 17 ;============================================================================= |
| 18 | 18 calibrate1 CODE |
| 19 ;============================================================================= | 19 ;============================================================================= |
| 20 | |
| 20 | 21 |
| 21 IFDEF _external_sensor | 22 IFDEF _external_sensor |
| 22 | 23 |
| 23 global transmit_setpoint ; transmit current setpoint from WREG (in cbar) to external electronics | 24 ;----------------------------------------------------------------------------- |
| 25 ; Transmit current Setpoint from WREG (in cbar) to external Electronics | |
| 26 ; | |
| 27 global transmit_setpoint | |
| 24 transmit_setpoint: | 28 transmit_setpoint: |
| 25 return ; !!!! FUNCTION IS CURRENTLY DISABLED !!!! | 29 return ; !!!! FUNCTION IS CURRENTLY DISABLED !!!! |
| 26 btfss s8_digital_avail ; do we have a digital S8 interface? | 30 |
| 27 return ; NO - ignore | 31 ; btfsc ext_input_optical ; optical input in use? |
| 28 ; YES - transmit setpoint from WREG | 32 ; return ; YES - setpoint - TX not supported |
| 29 clrf lo ; initialize checksum | 33 ; TSTOSS opt_s8_mode ; NO - S8 mode selected? |
| 30 movff char_I_const_ppO2,hi ; copy setpoint value to hi | 34 ; return ; NO |
| 31 movlw 0xAA ; start byte | 35 ; clrf lo ; YES - initialize checksum |
| 32 rcall tx_to_HUD ; transmit to HUD | 36 ; movff char_I_const_ppO2,hi ; - copy setpoint value to hi |
| 33 movlw 0x60 ; command new SP | 37 ; movlw 0xAA ; - load start byte |
| 34 rcall tx_to_HUD ; transmit to HUD | 38 ; rcall tx_to_HUD_chksum ; - transmit to HUD |
| 35 movff hi,WREG ; SP in cbar | 39 ; movlw 0x60 ; - load command 'new SP' |
| 36 rcall tx_to_HUD ; transmit to HUD | 40 ; rcall tx_to_HUD_chksum ; - transmit to HUD |
| 37 movff lo,WREG ; checksum | 41 ; movf hi,W ; - load SP in cbar |
| 38 rcall tx_to_HUD_cs ; transmit checksum | 42 ; rcall tx_to_HUD_chksum ; - transmit to HUD |
| 39 return | 43 ; movf lo,W ; - load checksum |
| 40 | 44 ; rcall tx_to_HUD ; - transmit checksum |
| 41 tx_to_HUD: ; entry point to transmit a byte to the HUD | 45 ; return ; - done |
| 46 | |
| 47 | |
| 48 ;----------------------------------------------------------------------------- | |
| 49 ; Helper Function - Transmit Byte to external Electronics | |
| 50 ; | |
| 51 tx_to_HUD_chksum: | |
| 42 addwf lo,F ; add byte to checksum | 52 addwf lo,F ; add byte to checksum |
| 43 tx_to_HUD_cs: ; entry point to transmit the checksum | 53 tx_to_HUD: |
| 44 movff WREG,TXREG2 ; transmit byte | 54 goto ir_s8_tx_single ; transmit byte and return |
| 45 call ir_s8_wait_tx ; wait for UART | 55 |
| 46 return | 56 |
| 47 | 57 ;----------------------------------------------------------------------------- |
| 48 | 58 ; Compute Calibration Factors |
| 59 ; | |
| 49 global calibrate_mix | 60 global calibrate_mix |
| 50 calibrate_mix: | 61 calibrate_mix: |
| 51 ; set usage and calibration flags as per default | 62 ; set usage and calibration flags to default values |
| 52 bsf use_O2_sensor1 | 63 bsf use_O2_sensor1 ; sensor in use |
| 53 bsf use_O2_sensor2 | 64 bsf use_O2_sensor2 ; ... |
| 54 bsf use_O2_sensor3 | 65 bsf use_O2_sensor3 ; ... |
| 55 bsf sensor1_calibrated_ok | 66 bsf sensor1_calibrated_ok ; sensor calibration ok |
| 56 bsf sensor2_calibrated_ok | 67 bsf sensor2_calibrated_ok ; ... |
| 57 bsf sensor3_calibrated_ok | 68 bsf sensor3_calibrated_ok ; ... |
| 58 | 69 |
| 59 ; ISR-safe 2 byte copy of the current pressure to xB for later use | 70 ; ISR-safe 2 byte copy of the current pressure to xB for later use |
| 60 SMOVII pressure_abs,xB | 71 SMOVII pressure_abs,xB |
| 61 | 72 |
| 62 ; check for HUD | 73 ; check for HUD |
| 63 btfss s8_digital_avail ; do we have a digital S8 interface? | 74 btfsc ext_input_optical ; optical interface in use? |
| 64 bra calibrate_mix1 ; NO - skip HUD part | 75 bra calibrate_mix1 ; YES - skip |
| 65 | 76 TSTOSS opt_s8_mode ; NO - S8 interface in use? |
| 66 ; calibrate any S8-connected HUD | 77 bra calibrate_mix1 ; NO - skip HUD part |
| 78 ;bra calibrate_mix0 ; YES - calibrate S8 HUD | |
| 79 | |
| 80 ; calibrate S8-connected external electronics | |
| 81 calibrate_mix0: | |
| 67 clrf lo ; initialize checksum | 82 clrf lo ; initialize checksum |
| 68 movlw 0xAA ; start byte | 83 movlw 0xAA ; load start byte |
| 84 rcall tx_to_HUD_chksum ; transmit to HUD | |
| 85 movlw 0x31 ; load calibration command | |
| 86 rcall tx_to_HUD_chksum ; transmit to HUD | |
| 87 movff opt_calibration_O2_ratio,WREG ; load calibration gas %O2 | |
| 88 rcall tx_to_HUD_chksum ; transmit to HUD | |
| 89 movf xB+0,W ; load current absolute pressure low byte | |
| 90 rcall tx_to_HUD_chksum ; transmit to HUD | |
| 91 movf xB+1,W ; load current absolute pressure high byte | |
| 92 rcall tx_to_HUD_chksum ; transmit to HUD | |
| 93 movf lo,W ; load checksum | |
| 69 rcall tx_to_HUD ; transmit to HUD | 94 rcall tx_to_HUD ; transmit to HUD |
| 70 movlw 0x31 ; calibration command | |
| 71 rcall tx_to_HUD ; transmit to HUD | |
| 72 movff opt_calibration_O2_ratio,WREG ; calibration gas %O2 | |
| 73 rcall tx_to_HUD ; transmit to HUD | |
| 74 movff xB+0,WREG ; current absolute pressure low byte | |
| 75 rcall tx_to_HUD ; transmit to HUD | |
| 76 movff xB+1,WREG ; current absolute pressure high byte | |
| 77 rcall tx_to_HUD ; transmit to HUD | |
| 78 movff lo,WREG ; checksum | |
| 79 rcall tx_to_HUD_cs ; transmit to HUD | |
| 80 ; bra calibrate_mix2 | |
| 81 | 95 |
| 82 ; calibrate internal sensors | 96 ; calibrate internal sensors |
| 83 calibrate_mix1: ; compute %O2 * 100 * absolute pressure [mbar] / 100 | 97 calibrate_mix1: ; compute C = %O2 * 100 * absolute pressure [mbar] / 100 |
| 84 movff opt_calibration_O2_ratio,WREG | 98 movff opt_calibration_O2_ratio,WREG ; load calibration gas %O2 |
| 85 mullw .100 | 99 mullw .100 ; multiply with 100 |
| 86 MOVII PROD,xA | 100 MOVII PROD,xA ; copy result to xA |
| 87 call mult16x16 ; xA*xB=xC | 101 call mult16x16 ; xC = xA * xB |
| 88 MOVLI .100,xB | 102 MOVLI .100,xB ; prepare division by 100 |
| 89 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 103 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 90 | |
| 91 ; keep a copy of the result | |
| 92 movff xC+0,mpr+0 | |
| 93 movff xC+1,mpr+1 | |
| 94 movff xC+2,mpr+2 | |
| 95 movff xC+3,mpr+3 | |
| 96 | 104 |
| 97 ; compute factor for sensor 1 | 105 ; compute factor for sensor 1 |
| 98 MOVII sensor1_mv,xB | 106 MOVRR xC,mpr,.4 ; backup calibration value C |
| 107 MOVII sensor1_mv,xB ; get mV from sensor 1 | |
| 99 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 108 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 100 MOVII xC,opt_x_s1 ; xC = ppO2/mV as factor for sensor 1 | 109 MOVII xC,opt_x_s1 ; xC = ppO2/mV as factor for sensor 1 |
| 101 | 110 |
| 102 ; restore result | |
| 103 movff mpr+0,xC+0 | |
| 104 movff mpr+1,xC+1 | |
| 105 movff mpr+2,xC+2 | |
| 106 movff mpr+3,xC+3 | |
| 107 | |
| 108 ; compute factor for sensor 2 | 111 ; compute factor for sensor 2 |
| 109 MOVII sensor2_mv,xB | 112 MOVRR mpr,xC,.4 ; restore C |
| 113 MOVII sensor2_mv,xB ; get mV from sensor 2 | |
| 110 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 114 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 111 MOVII xC,opt_x_s2 ; xC = ppO2/mV as factor for sensor 2 | 115 MOVII xC,opt_x_s2 ; xC = ppO2/mV as factor for sensor 2 |
| 112 | 116 |
| 113 ; restore result | |
| 114 movff mpr+0,xC+0 | |
| 115 movff mpr+1,xC+1 | |
| 116 movff mpr+2,xC+2 | |
| 117 movff mpr+3,xC+3 | |
| 118 | |
| 119 ; compute factor for sensor 3 | 117 ; compute factor for sensor 3 |
| 120 MOVII sensor3_mv,xB | 118 MOVRR mpr,xC,.4 ; restore C |
| 119 MOVII sensor3_mv,xB ; get mV from sensor 3 | |
| 121 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 120 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 122 MOVII xC,opt_x_s3 ; xC = ppO2/mV as factor for sensor 3 | 121 MOVII xC,opt_x_s3 ; xC = ppO2/mV as factor for sensor 3 |
| 123 | 122 |
| 124 ; check sensor 1 for min/max mV | 123 ; check sensor 1 for min/max mV |
| 125 MOVII sensor1_mv,sub_a ; get mV from sensor 1 | 124 MOVII sensor1_mv,sub_a ; get mV from sensor 1 |
| 126 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 | 125 rcall calibrate_mix_helper ; check mV for min/max boundary, returns with WREG = 0 if ok, else WREG = 1 |
| 127 TSTFSZ WREG ; sensor mV within thresholds? | 126 TSTFSZ WREG ; sensor mV within boundary? |
| 128 bcf use_O2_sensor1 ; NO - clear usage flag | 127 bcf use_O2_sensor1 ; NO - revoke sensor from usage |
| 129 | 128 |
| 130 ; check sensor 2 for min/max mV | 129 ; check sensor 2 for min/max mV |
| 131 MOVII sensor2_mv,sub_a ; get mV from sensor 2 | 130 MOVII sensor2_mv,sub_a ; get mV from sensor 2 |
| 132 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 | 131 rcall calibrate_mix_helper ; check mV for min/max boundary, returns with WREG = 0 if ok, else WREG = 1 |
| 133 TSTFSZ WREG ; sensor mV within thresholds? | 132 TSTFSZ WREG ; sensor mV within boundary? |
| 134 bcf use_O2_sensor2 ; NO - clear usage flag | 133 bcf use_O2_sensor2 ; NO - revoke sensor from usage |
| 135 | 134 |
| 136 ; check sensor 3 for min/max mV | 135 ; check sensor 3 for min/max mV |
| 137 MOVII sensor3_mv,sub_a ; get mV from sensor 3 | 136 MOVII sensor3_mv,sub_a ; get mV from sensor 3 |
| 138 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 | 137 rcall calibrate_mix_helper ; check mV for min/max boundary, returns with WREG = 0 if ok, else WREG = 1 |
| 139 TSTFSZ WREG ; sensor mV within thresholds? | 138 TSTFSZ WREG ; sensor mV within boundary? |
| 140 bcf use_O2_sensor3 ; NO - clear usage flag | 139 bcf use_O2_sensor3 ; NO - revoke sensor from usage |
| 141 | 140 |
| 142 calibrate_mix2: | 141 calibrate_mix2: |
| 143 ; check for HUD | 142 ; check for HUD |
| 144 btfss hud_connection_ok ; HUD connection existing? | 143 btfss hud_connection_ok ; HUD connection existing? |
| 145 bra calibrate_mix3 ; NO - skip HUD part | 144 bra calibrate_mix3 ; NO - skip HUD part |
| 146 | 145 |
| 146 ; TODO: wait for HUD to complete calibration an send updated data | |
| 147 | |
| 147 ; copy disable flags from HUD digital input | 148 ; copy disable flags from HUD digital input |
| 148 btfss sensor1_active | 149 btfss sensor1_active ; sensor 1 usable? |
| 149 bcf use_O2_sensor1 | 150 bcf use_O2_sensor1 ; NO - revoke sensor from usage |
| 150 btfss sensor2_active | 151 btfss sensor2_active ; sensor 2 usable? |
| 151 bcf use_O2_sensor2 | 152 bcf use_O2_sensor2 ; NO - revoke sensor from usage |
| 152 btfss sensor3_active | 153 btfss sensor3_active ; sensor 3 usable? |
| 153 bcf use_O2_sensor3 | 154 bcf use_O2_sensor3 ; NO - revoke sensor from usage |
| 154 | 155 |
| 155 calibrate_mix3: | 156 calibrate_mix3: |
| 156 ; clear calibration flags if sensors are not found to be ok | 157 ; clear calibration flags if sensors are not usable |
| 157 btfss use_O2_sensor1 ; sensor 1 out of range? | 158 btfss use_O2_sensor1 ; sensor 1 usable? |
| 158 bcf sensor1_calibrated_ok ; YES - disable this sensor | 159 bcf sensor1_calibrated_ok ; NO - revoke calibration |
| 159 btfss use_O2_sensor2 ; sensor 2 out of range? | 160 btfss use_O2_sensor2 ; sensor 2 usable? |
| 160 bcf sensor2_calibrated_ok ; YES - disable this sensor | 161 bcf sensor2_calibrated_ok ; NO - revoke calibration |
| 161 btfss use_O2_sensor3 ; sensor 3 out of range? | 162 btfss use_O2_sensor3 ; sensor 3 usable? |
| 162 bcf sensor3_calibrated_ok ; YES - disable this sensor | 163 bcf sensor3_calibrated_ok ; NO - revoke calibration |
| 163 | 164 |
| 164 ; when no sensor is found, enable all three to show error state and clear calibration factors | 165 ; if there is no usable sensor at all, then enable all three |
| 165 btfsc use_O2_sensor1 | 166 ; sensors to show error state and clear calibration factors |
| 166 return | 167 btfsc use_O2_sensor1 ; sensor 1 usable? |
| 167 btfsc use_O2_sensor2 | 168 return ; YES - done |
| 168 return | 169 btfsc use_O2_sensor2 ; NO - sensor 2 usable? |
| 169 btfsc use_O2_sensor3 | 170 return ; YES - done |
| 170 return | 171 btfsc use_O2_sensor3 ; NO - sensor 3 usable? |
| 171 | 172 return ; YES - done |
| 172 ; enable all sensors | 173 bsf use_O2_sensor1 ; NO - enable all sensors |
| 173 bsf use_O2_sensor1 | 174 bsf use_O2_sensor2 ; - ... |
| 174 bsf use_O2_sensor2 | 175 bsf use_O2_sensor3 ; - ... |
| 175 bsf use_O2_sensor3 | 176 CLRR opt_x_s1,.6 ; - clear calibration factors (3x 16 bit) |
| 176 ; clear calibration factors | 177 return ; - done |
| 177 banksel opt_x_s1 ; switch to bank options table | 178 |
| 178 CLRI opt_x_s1 | 179 |
| 179 CLRI opt_x_s2 | 180 ;----------------------------------------------------------------------------- |
| 180 CLRI opt_x_s3 | 181 ; Helper Function - Check if Sensor mV Value is within Min/Max Boundary |
| 181 banksel common ; back to bank common | 182 ; |
| 182 return | |
| 183 | |
| 184 calibrate_mix_helper: | 183 calibrate_mix_helper: |
| 185 MOVLI min_mv,sub_b ; load minimum threshold into sub_b | 184 MOVLI min_mv,sub_b ; load minimum boundary into sub_b |
| 186 call sub16 ; sub_c = sub_a - sub_b | 185 call sub16 ; sub_c = sub_a - sub_b |
| 187 btfsc neg_flag ; sensor mV lower than minimum threshold? | 186 btfsc neg_flag ; sensor mV lower than minimum boundary? |
| 188 retlw .1 ; YES - return signaling threshold violation | 187 retlw .1 ; YES - return signaling boundary violation |
| 189 MOVLI max_mv,sub_b ; load maximum threshold into sub_b | 188 |
| 189 MOVLI max_mv,sub_b ; load maximum boundary into sub_b | |
| 190 call sub16 ; sub_c = sub_a - sub_b | 190 call sub16 ; sub_c = sub_a - sub_b |
| 191 btfss neg_flag ; sensor mV higher than maximum threshold? | 191 btfss neg_flag ; sensor mV higher than maximum boundary? |
| 192 retlw .1 ; YES - return signaling threshold violation | 192 retlw .1 ; YES - return signaling boundary violation |
| 193 retlw .0 ; NO - return signaling min/max ok | 193 |
| 194 | 194 retlw .0 ; return signaling min/max ok |
| 195 | 195 |
| 196 global compute_mvolts_for_all_sensors | 196 ENDIF ; _external_sensor |
| 197 compute_mvolts_for_all_sensors: ; compute mV or all sensors (S8 mode) | 197 |
| 198 ;============================================================================= | |
| 199 calibrate2 CODE | |
| 200 ;============================================================================= | |
| 201 | |
| 202 IFDEF _external_sensor | |
| 203 | |
| 204 ;----------------------------------------------------------------------------- | |
| 205 ; Compute Sensor mV from Raw Values received via S8 digital Interface | |
| 206 ; | |
| 207 global compute_mvolts_from_rawdata | |
| 208 compute_mvolts_from_rawdata: | |
| 209 | |
| 198 ; compute AD results in 100 µV steps (16 bit/sensor) | 210 ; compute AD results in 100 µV steps (16 bit/sensor) |
| 199 ; 24 bit AD result is in 244.1406541 nV | 211 ; 24 bit AD result is in 244.1406541 nV |
| 200 ; divide 24 bit value by 409.5999512 -> 410 with only 0.01% error | 212 ; divide 24 bit value by 409.5999512 -> 410 with only 0.01% error |
| 201 #DEFINE ad2mv_factor .410 | 213 #DEFINE ad2mv_factor .410 |
| 202 | 214 |
| 203 MOVLI ad2mv_factor,xB | 215 MOVLI ad2mv_factor,xB ; load conversion factor into xB |
| 204 | 216 |
| 205 ; Sensor 1 | 217 ; Sensor 1 |
| 206 SMOVTT s8_rawdata_sensor1,xC ; ISR-safe copy of 3 bytes to xC | 218 SMOVTT s8_rawdata_sensor1,xC ; ISR-safe copy of 3 bytes to xC |
| 207 clrf xC+3 ; clear MSB of xC | 219 clrf xC+3 ; clear MSB of xC |
| 208 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 220 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 209 MOVII xC,sensor1_mv ; in 100 µV steps | 221 MOVII xC,sensor1_mv ; store result |
| 210 | 222 |
| 211 ; Sensor 2 | 223 ; Sensor 2 |
| 212 SMOVTT s8_rawdata_sensor2,xC ; ISR-safe copy of 3 bytes to xC | 224 SMOVTT s8_rawdata_sensor2,xC ; ISR-safe copy of 3 bytes to xC |
| 213 clrf xC+3 ; clear MSB of xC | 225 clrf xC+3 ; clear MSB of xC |
| 214 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 226 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 215 MOVII xC,sensor2_mv ; in 100 µV steps | 227 MOVII xC,sensor2_mv ; store result |
| 216 | 228 |
| 217 ; Sensor 3 | 229 ; Sensor 3 |
| 218 SMOVTT s8_rawdata_sensor3,xC ; ISR-safe copy of 3 bytes to xC | 230 SMOVTT s8_rawdata_sensor3,xC ; ISR-safe copy of 3 bytes to xC |
| 219 clrf xC+3 ; clear MSB of xC | 231 clrf xC+3 ; clear MSB of xC |
| 220 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | 232 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
| 221 MOVII xC,sensor3_mv ; in 100 µV steps | 233 MOVII xC,sensor3_mv ; store result |
| 222 | 234 |
| 223 return ; done | 235 return ; done |
| 224 | 236 |
| 225 ENDIF ; _external_sensor | 237 ENDIF ; _external_sensor |
| 226 | 238 |
| 227 ;============================================================================= | 239 ;----------------------------------------------------------------------------- |
| 228 | 240 |
| 229 END | 241 END |
