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