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