Mercurial > public > hwos_code
annotate src/calibrate.asm @ 631:185ba2f91f59
3.09 beta 1 release
author | heinrichsweikamp |
---|---|
date | Fri, 28 Feb 2020 15:45:07 +0100 |
parents | c40025d8e750 |
children | 4050675965ea |
rev | line source |
---|---|
113 | 1 ;============================================================================= |
2 ; | |
631 | 3 ; File calibration.asm combined next generation V3.08.8 |
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 | |
582 | 17 calibrate CODE |
18 | |
623 | 19 ;============================================================================= |
20 | |
21 IFDEF _external_sensor | |
604 | 22 |
23 global transmit_setpoint ; transmit current setpoint from WREG (in cbar) to external electronics | |
24 transmit_setpoint: | |
623 | 25 return ; !!!! FUNCTION IS CURRENTLY DISABLED !!!! |
26 btfss s8_digital_avail ; do we have a digital S8 interface? | |
604 | 27 return ; NO - ignore |
28 ; YES - transmit setpoint from WREG | |
29 clrf lo ; initialize checksum | |
623 | 30 movff char_I_const_ppO2,hi ; copy setpoint value to hi |
604 | 31 movlw 0xAA ; start byte |
32 rcall tx_to_HUD ; transmit to HUD | |
33 movlw 0x60 ; command new SP | |
34 rcall tx_to_HUD ; transmit to HUD | |
35 movff hi,WREG ; SP in cbar | |
36 rcall tx_to_HUD ; transmit to HUD | |
37 movff lo,WREG ; checksum | |
38 rcall tx_to_HUD_cs ; transmit checksum | |
39 return | |
40 | |
41 tx_to_HUD: ; entry point to transmit a byte to the HUD | |
42 addwf lo,F ; add byte to checksum | |
43 tx_to_HUD_cs: ; entry point to transmit the checksum | |
44 movff WREG,TXREG2 ; transmit byte | |
631 | 45 call ir_s8_wait_tx ; wait for UART |
604 | 46 return |
47 | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
48 |
604 | 49 global calibrate_mix |
50 calibrate_mix: | |
51 ; set usage and calibration flags as per default | |
52 bsf use_O2_sensor1 | |
53 bsf use_O2_sensor2 | |
54 bsf use_O2_sensor3 | |
55 bsf sensor1_calibrated_ok | |
56 bsf sensor2_calibrated_ok | |
57 bsf sensor3_calibrated_ok | |
58 | |
623 | 59 ; ISR-safe 2 byte copy of the current pressure to xB for later use |
60 SMOVII pressure_abs,xB | |
61 | |
604 | 62 ; check for HUD |
623 | 63 btfss s8_digital_avail ; do we have a digital S8 interface? |
604 | 64 bra calibrate_mix1 ; NO - skip HUD part |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
65 |
604 | 66 ; calibrate any S8-connected HUD |
67 clrf lo ; initialize checksum | |
68 movlw 0xAA ; start byte | |
69 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 | |
623 | 74 movff xB+0,WREG ; current absolute pressure low byte |
604 | 75 rcall tx_to_HUD ; transmit to HUD |
623 | 76 movff xB+1,WREG ; current absolute pressure high byte |
604 | 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 | |
582 | 81 |
604 | 82 ; calibrate internal sensors |
623 | 83 calibrate_mix1: ; compute %O2 * 100 * absolute pressure [mbar] / 100 |
604 | 84 movff opt_calibration_O2_ratio,WREG |
85 mullw .100 | |
623 | 86 MOVII PROD,xA |
604 | 87 call mult16x16 ; xA*xB=xC |
623 | 88 MOVLI .100,xB |
89 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | |
604 | 90 |
91 ; keep a copy of the result | |
623 | 92 movff xC+0,mpr+0 |
93 movff xC+1,mpr+1 | |
94 movff xC+2,mpr+2 | |
95 movff xC+3,mpr+3 | |
604 | 96 |
97 ; compute factor for sensor 1 | |
623 | 98 MOVII sensor1_mv,xB |
99 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 | |
604 | 101 |
102 ; restore result | |
623 | 103 movff mpr+0,xC+0 |
104 movff mpr+1,xC+1 | |
105 movff mpr+2,xC+2 | |
106 movff mpr+3,xC+3 | |
604 | 107 |
108 ; compute factor for sensor 2 | |
623 | 109 MOVII sensor2_mv,xB |
110 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 | |
604 | 112 |
113 ; restore result | |
623 | 114 movff mpr+0,xC+0 |
115 movff mpr+1,xC+1 | |
116 movff mpr+2,xC+2 | |
117 movff mpr+3,xC+3 | |
604 | 118 |
119 ; compute factor for sensor 3 | |
623 | 120 MOVII sensor3_mv,xB |
121 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 | |
604 | 123 |
124 ; check sensor 1 for min/max mV | |
623 | 125 MOVII sensor1_mv,sub_a ; get mV from sensor 1 |
604 | 126 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 |
127 TSTFSZ WREG ; sensor mV within thresholds? | |
128 bcf use_O2_sensor1 ; NO - clear usage flag | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
129 |
604 | 130 ; check sensor 2 for min/max mV |
623 | 131 MOVII sensor2_mv,sub_a ; get mV from sensor 2 |
604 | 132 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 |
133 TSTFSZ WREG ; sensor mV within thresholds? | |
134 bcf use_O2_sensor2 ; NO - clear usage flag | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
135 |
604 | 136 ; check sensor 3 for min/max mV |
623 | 137 MOVII sensor3_mv,sub_a ; get mV from sensor 3 |
604 | 138 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 |
139 TSTFSZ WREG ; sensor mV within thresholds? | |
140 bcf use_O2_sensor3 ; NO - clear usage flag | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
141 |
604 | 142 calibrate_mix2: |
143 ; check for HUD | |
144 btfss hud_connection_ok ; HUD connection existing? | |
145 bra calibrate_mix3 ; NO - skip HUD part | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
146 |
623 | 147 ; copy disable flags from HUD digital input |
582 | 148 btfss sensor1_active |
149 bcf use_O2_sensor1 | |
150 btfss sensor2_active | |
151 bcf use_O2_sensor2 | |
152 btfss sensor3_active | |
153 bcf use_O2_sensor3 | |
113 | 154 |
604 | 155 calibrate_mix3: |
156 ; clear calibration flags if sensors are not found to be ok | |
157 btfss use_O2_sensor1 ; sensor 1 out of range? | |
158 bcf sensor1_calibrated_ok ; YES - disable this sensor | |
159 btfss use_O2_sensor2 ; sensor 2 out of range? | |
160 bcf sensor2_calibrated_ok ; YES - disable this sensor | |
161 btfss use_O2_sensor3 ; sensor 3 out of range? | |
162 bcf sensor3_calibrated_ok ; YES - disable this sensor | |
113 | 163 |
623 | 164 ; when no sensor is found, enable all three to show error state and clear calibration factors |
192 | 165 btfsc use_O2_sensor1 |
113 | 166 return |
192 | 167 btfsc use_O2_sensor2 |
113 | 168 return |
192 | 169 btfsc use_O2_sensor3 |
113 | 170 return |
623 | 171 |
172 ; enable all sensors | |
192 | 173 bsf use_O2_sensor1 |
174 bsf use_O2_sensor2 | |
175 bsf use_O2_sensor3 | |
623 | 176 ; clear calibration factors |
177 banksel opt_x_s1 ; switch to bank options table | |
178 CLRI opt_x_s1 | |
179 CLRI opt_x_s2 | |
180 CLRI opt_x_s3 | |
181 banksel common ; back to bank common | |
113 | 182 return |
183 | |
604 | 184 calibrate_mix_helper: |
623 | 185 MOVLI min_mv,sub_b ; load minimum threshold into sub_b |
604 | 186 call sub16 ; sub_c = sub_a - sub_b |
187 btfsc neg_flag ; sensor mV lower than minimum threshold? | |
188 retlw .1 ; YES - return signaling threshold violation | |
623 | 189 MOVLI max_mv,sub_b ; load maximum threshold into sub_b |
604 | 190 call sub16 ; sub_c = sub_a - sub_b |
191 btfss neg_flag ; sensor mV higher than maximum threshold? | |
192 retlw .1 ; YES - return signaling threshold violation | |
193 retlw .0 ; NO - return signaling min/max ok | |
560 | 194 |
448 | 195 |
560 | 196 global compute_mvolts_for_all_sensors |
604 | 197 compute_mvolts_for_all_sensors: ; compute mV or all sensors (S8 mode) |
623 | 198 ; compute AD results in 100 µV steps (16 bit/sensor) |
199 ; 24 bit AD result is in 244.1406541 nV | |
200 ; divide 24 bit value by 409.5999512 -> 410 with only 0.01% error | |
582 | 201 #DEFINE ad2mv_factor .410 |
623 | 202 |
203 MOVLI ad2mv_factor,xB | |
204 | |
560 | 205 ; Sensor 1 |
623 | 206 SMOVTT s8_rawdata_sensor1,xC ; ISR-safe copy of 3 bytes to xC |
207 clrf xC+3 ; clear MSB of xC | |
208 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | |
209 MOVII xC,sensor1_mv ; in 100 µV steps | |
210 | |
560 | 211 ; Sensor 2 |
623 | 212 SMOVTT s8_rawdata_sensor2,xC ; ISR-safe copy of 3 bytes to xC |
213 clrf xC+3 ; clear MSB of xC | |
214 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder | |
215 MOVII xC,sensor2_mv ; in 100 µV steps | |
216 | |
560 | 217 ; Sensor 3 |
623 | 218 SMOVTT s8_rawdata_sensor3,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 | |
221 MOVII xC,sensor3_mv ; in 100 µV steps | |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
222 |
604 | 223 return ; done |
113 | 224 |
623 | 225 ENDIF ; _external_sensor |
226 | |
227 ;============================================================================= | |
228 | |
229 END |