Mercurial > public > hwos_code
annotate src/calibrate.asm @ 601:08a0162d3ca1
Fix false CNS readings in logbook
author | heinrichsweikamp |
---|---|
date | Tue, 14 Aug 2018 11:01:47 +0200 |
parents | b455b31ce022 |
children | ca4556fb60b9 |
rev | line source |
---|---|
113 | 1 ;============================================================================= |
2 ; | |
582 | 3 ; File calibration.asm REFACTORED VERSION V2.98 |
113 | 4 ; |
5 ; o2 sensor calibration subroutines | |
6 ; | |
7 ; Copyright (c) 2014, Heinrichs Weikamp, all right reserved. | |
8 ;============================================================================= | |
9 | |
582 | 10 #include "hwos.inc" |
11 #include "shared_definitions.h" ; Mailbox between c and asm | |
12 #include "math.inc" | |
13 #include "adc_lightsensor.inc" | |
14 #include "eeprom_rs232.inc" | |
113 | 15 |
16 | |
582 | 17 calibrate CODE |
18 | |
19 global check_sensors ; Check O2 sensor thresholds for fallback and voting logic | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
20 check_sensors: |
560 | 21 ; Check min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
22 movff o2_mv_sensor1+0, sub_a+0 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
23 movff o2_mv_sensor1+1, sub_a+1 |
582 | 24 movlw LOW min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
25 movwf sub_b+0 |
582 | 26 movlw HIGH min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
27 movwf sub_b+1 |
582 | 28 call sub16 ; sub_c = sub_a - sub_b |
29 bsf use_O2_sensor1 ;=1: Use this sensor for deco | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
30 btfsc neg_flag |
582 | 31 bcf use_O2_sensor1 ;=1: Use this sensor for deco |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
32 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
33 movff o2_mv_sensor2+0, sub_a+0 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
34 movff o2_mv_sensor2+1, sub_a+1 |
582 | 35 movlw LOW min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
36 movwf sub_b+0 |
582 | 37 movlw HIGH min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
38 movwf sub_b+1 |
582 | 39 call sub16 ; sub_c = sub_a - sub_b |
40 bsf use_O2_sensor2 ;=1: Use this sensor for deco | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
41 btfsc neg_flag |
582 | 42 bcf use_O2_sensor2 ;=1: Use this sensor for deco |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
43 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
44 movff o2_mv_sensor3+0, sub_a+0 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
45 movff o2_mv_sensor3+1, sub_a+1 |
582 | 46 movlw LOW min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
47 movwf sub_b+0 |
582 | 48 movlw HIGH min_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
49 movwf sub_b+1 |
582 | 50 call sub16 ; sub_c = sub_a - sub_b |
51 bsf use_O2_sensor3 ;=1: Use this sensor for deco | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
52 btfsc neg_flag |
582 | 53 bcf use_O2_sensor3 ;=1: Use this sensor for deco |
54 | |
560 | 55 ; Check max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
56 movff o2_mv_sensor1+0, sub_a+0 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
57 movff o2_mv_sensor1+1, sub_a+1 |
582 | 58 movlw LOW max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
59 movwf sub_b+0 |
582 | 60 movlw HIGH max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
61 movwf sub_b+1 |
582 | 62 call sub16 ; sub_c = sub_a - sub_b |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
63 btfss neg_flag |
582 | 64 bcf use_O2_sensor1 ;=1: Use this sensor for deco |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
65 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
66 movff o2_mv_sensor2+0, sub_a+0 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
67 movff o2_mv_sensor2+1, sub_a+1 |
582 | 68 movlw LOW max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
69 movwf sub_b+0 |
582 | 70 movlw HIGH max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
71 movwf sub_b+1 |
582 | 72 call sub16 ; sub_c = sub_a - sub_b |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
73 btfss neg_flag |
582 | 74 bcf use_O2_sensor2 ;=1: Use this sensor for deco |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
75 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
76 movff o2_mv_sensor3+0, sub_a+0 |
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
77 movff o2_mv_sensor3+1, sub_a+1 |
582 | 78 movlw LOW max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
79 movwf sub_b+0 |
582 | 80 movlw HIGH max_mv |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
81 movwf sub_b+1 |
582 | 82 call sub16 ; sub_c = sub_a - sub_b |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
83 btfss neg_flag |
582 | 84 bcf use_O2_sensor3 ;=1: Use this sensor for deco |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
85 |
582 | 86 btfss hud_connection_ok ;=1: HUD connection ok |
87 bra check_sensor2 ; No HUD/Digital data | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
88 |
582 | 89 ; Copy disable flags from digital input |
90 btfss sensor1_active | |
91 bcf use_O2_sensor1 | |
92 btfss sensor2_active | |
93 bcf use_O2_sensor2 | |
94 btfss sensor3_active | |
95 bcf use_O2_sensor3 | |
96 return | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
97 |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
98 check_sensor2: |
560 | 99 ; Copy disable flags from internal calibration routine |
582 | 100 btfss sensor1_calibrated_ok |
101 bcf use_O2_sensor1 | |
102 btfss sensor2_calibrated_ok | |
103 bcf use_O2_sensor2 | |
104 btfss sensor3_calibrated_ok | |
105 bcf use_O2_sensor3 | |
106 return | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
107 |
192 | 108 |
113 | 109 global calibrate_mix |
110 calibrate_mix: | |
582 | 111 ; calibrate S8 HUD |
112 btfss s8_digital ; S8 Digital? | |
113 bra calibrate_mix2 ; No | |
113 | 114 |
582 | 115 ; Yes, calibrate any S8-connected HUD |
116 clrf lo ; Checksum | |
117 movlw 0xAA ; Start Byte | |
118 addwf lo,F | |
119 movff WREG,TXREG2 | |
120 call rs232_wait_tx2 | |
113 | 121 |
582 | 122 movlw 0x31 ; Calibrate |
123 addwf lo,F | |
124 movff WREG,TXREG2 | |
125 call rs232_wait_tx2 | |
113 | 126 |
582 | 127 movff opt_calibration_O2_ratio,WREG ; Calibration gas %O2 |
128 addwf lo,F | |
129 movff WREG,TXREG2 | |
130 call rs232_wait_tx2 | |
113 | 131 |
582 | 132 movff amb_pressure+0,WREG ; Ambient pressure |
133 addwf lo,F | |
134 movff WREG,TXREG2 | |
135 call rs232_wait_tx2 | |
136 movff amb_pressure+1,WREG | |
137 addwf lo,F | |
138 movff WREG,TXREG2 | |
139 call rs232_wait_tx2 | |
113 | 140 |
582 | 141 movff lo,TXREG2 ; Checksum |
142 call rs232_wait_tx2 | |
113 | 143 |
144 calibrate_mix2: | |
582 | 145 movff opt_calibration_O2_ratio,WREG ; Calibration gas %O2 |
146 mullw .100 | |
113 | 147 movff PRODL,xA+0 |
148 movff PRODH,xA+1 | |
560 | 149 ; (%O2*100)*[ambient,mbar]/100 -> xC |
113 | 150 movff amb_pressure+0,xB+0 |
151 movff amb_pressure+1,xB+1 | |
448 | 152 rcall calibrate_mix2_helper |
113 | 153 movff o2_mv_sensor1+0,xB+0 |
154 movff o2_mv_sensor1+1,xB+1 | |
582 | 155 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
113 | 156 ; xC= ppO2/mV |
157 movff xC+0,opt_x_s1+0 | |
560 | 158 movff xC+1,opt_x_s1+1 ; Factor for Sensor1 |
113 | 159 |
582 | 160 movff opt_calibration_O2_ratio,WREG ; Calibration gas %O2 |
161 mullw .100 | |
113 | 162 movff PRODL,xA+0 |
163 movff PRODH,xA+1 | |
560 | 164 ; (%O2*100)*[ambient,mbar]/100 -> xC |
113 | 165 movff amb_pressure+0,xB+0 |
166 movff amb_pressure+1,xB+1 | |
448 | 167 rcall calibrate_mix2_helper |
113 | 168 movff o2_mv_sensor2+0,xB+0 |
169 movff o2_mv_sensor2+1,xB+1 | |
582 | 170 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
113 | 171 ; xC= ppO2/mV |
172 movff xC+0,opt_x_s2+0 | |
560 | 173 movff xC+1,opt_x_s2+1 ; Factor for Sensor2 |
113 | 174 |
582 | 175 movff opt_calibration_O2_ratio,WREG ; Calibration gas %O2 |
176 mullw .100 | |
113 | 177 movff PRODL,xA+0 |
178 movff PRODH,xA+1 | |
560 | 179 ; (%O2*100)*[ambient,mbar]/100 -> xC |
113 | 180 movff amb_pressure+0,xB+0 |
181 movff amb_pressure+1,xB+1 | |
448 | 182 rcall calibrate_mix2_helper |
113 | 183 movff o2_mv_sensor3+0,xB+0 |
184 movff o2_mv_sensor3+1,xB+1 | |
582 | 185 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
113 | 186 ; xC= ppO2/mV |
187 movff xC+0,opt_x_s3+0 | |
560 | 188 movff xC+1,opt_x_s3+1 ; Factor for Sensor3 |
113 | 189 |
582 | 190 bsf sensor1_calibrated_ok |
191 bsf sensor2_calibrated_ok | |
192 bsf sensor3_calibrated_ok ; Set flags prior check | |
560 | 193 |
582 | 194 rcall check_sensors ; Check O2 sensor thresholds min_mv and max_mv and set use_02_sensorX flags |
195 ; initialize internal calibration flags | |
196 btfss use_O2_sensor1 ; Sensor out of range? | |
197 bcf sensor1_calibrated_ok ; Yes, disable this sensor | |
198 btfss use_O2_sensor2 ; Sensor out of range? | |
199 bcf sensor2_calibrated_ok ; Yes, disable this sensor | |
200 btfss use_O2_sensor3 ; Sensor out of range? | |
201 bcf sensor3_calibrated_ok ; Yes, disable this sensor | |
113 | 202 |
203 ; When no sensor is found, enable all three to show error state | |
192 | 204 btfsc use_O2_sensor1 |
113 | 205 return |
192 | 206 btfsc use_O2_sensor2 |
113 | 207 return |
192 | 208 btfsc use_O2_sensor3 |
113 | 209 return |
192 | 210 bsf use_O2_sensor1 |
211 bsf use_O2_sensor2 | |
212 bsf use_O2_sensor3 | |
113 | 213 ; Clear factors |
582 | 214 banksel opt_x_s1+0 |
113 | 215 clrf opt_x_s1+0 |
216 clrf opt_x_s1+1 | |
217 clrf opt_x_s2+0 | |
218 clrf opt_x_s2+1 | |
219 clrf opt_x_s3+0 | |
220 clrf opt_x_s3+1 | |
582 | 221 banksel common |
113 | 222 return |
223 | |
560 | 224 |
582 | 225 calibrate_mix2_helper: |
226 call mult16x16 ; xA*xB=xC | |
227 movlw LOW .100 | |
448 | 228 movwf xB+0 |
582 | 229 movlw HIGH .100 |
448 | 230 movwf xB+1 |
582 | 231 goto div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder (And return) |
560 | 232 |
448 | 233 |
560 | 234 global compute_mvolts_for_all_sensors |
582 | 235 compute_mvolts_for_all_sensors: ; Compute mV or all sensors (S8 Mode) |
113 | 236 ; compute AD results in 100µV steps (16bit/sensor) |
237 ; 24bit AD result is in 244,1406541nV | |
582 | 238 ; Divide 24bit value through 409,5999512 -> 410 (0,01% error) |
239 #DEFINE ad2mv_factor .410 | |
560 | 240 ; Sensor 1 |
241 clrf xC+3 | |
242 movff s8_rawdata_sensor1+2,xC+2 | |
243 movff s8_rawdata_sensor1+1,xC+1 | |
244 movff s8_rawdata_sensor1+0,xC+0 | |
582 | 245 movlw LOW ad2mv_factor |
560 | 246 movwf xB+0 |
582 | 247 movlw HIGH ad2mv_factor |
560 | 248 movwf xB+1 |
582 | 249 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
560 | 250 movff xC+1,o2_mv_sensor1+1 |
582 | 251 movff xC+0,o2_mv_sensor1+0 ; in 100uV steps |
560 | 252 ; Sensor 2 |
253 clrf xC+3 | |
254 movff s8_rawdata_sensor2+2,xC+2 | |
255 movff s8_rawdata_sensor2+1,xC+1 | |
256 movff s8_rawdata_sensor2+0,xC+0 | |
582 | 257 movlw LOW ad2mv_factor |
560 | 258 movwf xB+0 |
582 | 259 movlw HIGH ad2mv_factor |
560 | 260 movwf xB+1 |
582 | 261 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
560 | 262 movff xC+1,o2_mv_sensor2+1 |
582 | 263 movff xC+0,o2_mv_sensor2+0 ; in 100uV steps |
560 | 264 ; Sensor 3 |
265 clrf xC+3 | |
266 movff s8_rawdata_sensor3+2,xC+2 | |
267 movff s8_rawdata_sensor3+1,xC+1 | |
268 movff s8_rawdata_sensor3+0,xC+0 | |
582 | 269 movlw LOW ad2mv_factor |
560 | 270 movwf xB+0 |
582 | 271 movlw HIGH ad2mv_factor |
560 | 272 movwf xB+1 |
582 | 273 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
560 | 274 movff xC+1,o2_mv_sensor3+1 |
582 | 275 movff xC+0,o2_mv_sensor3+0 ; in 100uV steps |
113 | 276 |
582 | 277 bcf new_s8_data_available ; Clear flag |
278 return ; Done. | |
113 | 279 |
560 | 280 |
582 | 281 global transmit_setpoint ; Transmit current setpoint from WREG (in cbar) to external electronics |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
282 transmit_setpoint: |
582 | 283 return ; !!!! FUNCTION IS CURRENTLY DISABLED !!!! |
284 btfss s8_digital ; S8 Digital? | |
285 return ; No, ignore | |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
286 |
582 | 287 ; Yes, transmit setpoint from WREG |
288 movwf hi ; Store setpoint | |
289 clrf lo ; Checksum | |
290 movlw 0xAA ; Start Byte | |
291 addwf lo,F | |
292 movff WREG,TXREG2 | |
293 call rs232_wait_tx2 | |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
294 |
582 | 295 movlw 0x60 ; New SP |
296 addwf lo,F | |
297 movff WREG,TXREG2 | |
298 call rs232_wait_tx2 | |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
299 |
582 | 300 movff hi,WREG ; SP in cbar |
301 addwf lo,F | |
302 movff WREG,TXREG2 | |
303 call rs232_wait_tx2 | |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
304 |
582 | 305 movff lo,TXREG2 ; Checksum |
306 call rs232_wait_tx2 | |
307 return | |
113 | 308 |
309 | |
310 END |