Mercurial > public > hwos_code
annotate src/calibrate.asm @ 618:7b3903536213
work on new battery menu
author | heinrichsweikamp |
---|---|
date | Mon, 04 Feb 2019 22:57:24 +0100 |
parents | ca4556fb60b9 |
children | c40025d8e750 |
rev | line source |
---|---|
113 | 1 ;============================================================================= |
2 ; | |
604 | 3 ; File calibration.asm REFACTORED VERSION V2.98b |
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" | |
604 | 15 #include "isr.inc" |
113 | 16 |
17 | |
582 | 18 calibrate CODE |
19 | |
604 | 20 |
21 global transmit_setpoint ; transmit current setpoint from WREG (in cbar) to external electronics | |
22 transmit_setpoint: | |
23 return ; !!!! FUNCTION IS CURRENTLY DISABLED !!!! | |
24 btfss s8_digital ; S8 Digital connection existing? | |
25 return ; NO - ignore | |
26 ; YES - transmit setpoint from WREG | |
27 clrf lo ; initialize checksum | |
28 movwf hi ; store setpoint | |
29 movlw 0xAA ; start byte | |
30 rcall tx_to_HUD ; transmit to HUD | |
31 movlw 0x60 ; command new SP | |
32 rcall tx_to_HUD ; transmit to HUD | |
33 movff hi,WREG ; SP in cbar | |
34 rcall tx_to_HUD ; transmit to HUD | |
35 movff lo,WREG ; checksum | |
36 rcall tx_to_HUD_cs ; transmit checksum | |
37 return | |
38 | |
39 tx_to_HUD: ; entry point to transmit a byte to the HUD | |
40 addwf lo,F ; add byte to checksum | |
41 tx_to_HUD_cs: ; entry point to transmit the checksum | |
42 movff WREG,TXREG2 ; transmit byte | |
43 call rs232_wait_tx2 ; wait for UART | |
44 return | |
45 | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
46 |
604 | 47 global calibrate_mix |
48 calibrate_mix: | |
49 ; set usage and calibration flags as per default | |
50 bsf use_O2_sensor1 | |
51 bsf use_O2_sensor2 | |
52 bsf use_O2_sensor3 | |
53 bsf sensor1_calibrated_ok | |
54 bsf sensor2_calibrated_ok | |
55 bsf sensor3_calibrated_ok | |
56 | |
57 ; check for HUD | |
58 btfss s8_digital ; S8 Digital connection existing? | |
59 bra calibrate_mix1 ; NO - skip HUD part | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
60 |
604 | 61 ; calibrate any S8-connected HUD |
62 clrf lo ; initialize checksum | |
63 movlw 0xAA ; start byte | |
64 rcall tx_to_HUD ; transmit to HUD | |
65 movlw 0x31 ; calibration command | |
66 rcall tx_to_HUD ; transmit to HUD | |
67 movff opt_calibration_O2_ratio,WREG ; calibration gas %O2 | |
68 rcall tx_to_HUD ; transmit to HUD | |
69 movff amb_pressure+0,WREG ; ambient pressure low byte | |
70 rcall tx_to_HUD ; transmit to HUD | |
71 movff amb_pressure+1,WREG ; ambient pressure high byte | |
72 rcall tx_to_HUD ; transmit to HUD | |
73 movff lo,WREG ; checksum | |
74 rcall tx_to_HUD_cs ; transmit to HUD | |
75 ; bra calibrate_mix2 | |
582 | 76 |
604 | 77 ; calibrate internal sensors |
78 calibrate_mix1: ; compute %O2 * 100 * ambient_pressure[mbar] / 100 | |
79 movff opt_calibration_O2_ratio,WREG | |
80 mullw .100 | |
81 movff PRODL,xA+0 | |
82 movff PRODH,xA+1 | |
83 SAFE_2BYTE_COPY amb_pressure,xB | |
84 call mult16x16 ; xA*xB=xC | |
85 movlw LOW .100 | |
86 movwf xB+0 | |
87 movlw HIGH .100 | |
88 movwf xB+1 | |
89 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
90 | |
91 ; keep a copy of the result | |
92 movff xC+0,lo | |
93 movff xC+1,hi | |
94 movff xC+2,up | |
95 movff xC+3,ex | |
96 | |
97 ; compute factor for sensor 1 | |
98 movff o2_mv_sensor1+0,xB+0 | |
99 movff o2_mv_sensor1+1,xB+1 | |
100 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
101 movff xC+0,opt_x_s1+0 ; xC= ppO2/mV as factor for sensor 1 | |
102 movff xC+1,opt_x_s1+1 | |
103 | |
104 ; restore result | |
105 movff lo,xC+0 | |
106 movff hi,xC+1 | |
107 movff up,xC+2 | |
108 movff ex,xC+3 | |
109 | |
110 ; compute factor for sensor 2 | |
111 movff o2_mv_sensor2+0,xB+0 | |
112 movff o2_mv_sensor2+1,xB+1 | |
113 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
114 movff xC+0,opt_x_s2+0 ; xC= ppO2/mV as factor for sensor 2 | |
115 movff xC+1,opt_x_s2+1 | |
116 | |
117 ; restore result | |
118 movff lo,xC+0 | |
119 movff hi,xC+1 | |
120 movff up,xC+2 | |
121 movff ex,xC+3 | |
122 | |
123 ; compute factor for sensor 3 | |
124 movff o2_mv_sensor3+0,xB+0 | |
125 movff o2_mv_sensor3+1,xB+1 | |
126 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
127 movff xC+0,opt_x_s3+0 ; xC= ppO2/mV as factor for sensor 3 | |
128 movff xC+1,opt_x_s3+1 | |
129 | |
130 ; check sensor 1 for min/max mV | |
131 movff o2_mv_sensor1+0, sub_a+0 ; get mV from sensor 1 | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
132 movff o2_mv_sensor1+1, sub_a+1 |
604 | 133 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 |
134 TSTFSZ WREG ; sensor mV within thresholds? | |
135 bcf use_O2_sensor1 ; NO - clear usage flag | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
136 |
604 | 137 ; check sensor 2 for min/max mV |
138 movff o2_mv_sensor2+0, sub_a+0 ; get mV from sensor 2 | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
139 movff o2_mv_sensor2+1, sub_a+1 |
604 | 140 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 |
141 TSTFSZ WREG ; sensor mV within thresholds? | |
142 bcf use_O2_sensor2 ; NO - clear usage flag | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
143 |
604 | 144 ; check sensor 3 for min/max mV |
145 movff o2_mv_sensor3+0, sub_a+0 ; get mV from sensor 3 | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
146 movff o2_mv_sensor3+1, sub_a+1 |
604 | 147 rcall calibrate_mix_helper ; check mV for min/max thresholds, returns with WREG = 0 if ok, else WREG = 1 |
148 TSTFSZ WREG ; sensor mV within thresholds? | |
149 bcf use_O2_sensor3 ; NO - clear usage flag | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
150 |
604 | 151 calibrate_mix2: |
152 ; check for HUD | |
153 btfss hud_connection_ok ; HUD connection existing? | |
154 bra calibrate_mix3 ; NO - skip HUD part | |
145
e3ac5b2021bc
NEW: Setpoint-Fallback option for external O2 sensor failure
heinrichsweikamp
parents:
113
diff
changeset
|
155 |
604 | 156 ; Copy disable flags from HUD digital input |
582 | 157 btfss sensor1_active |
158 bcf use_O2_sensor1 | |
159 btfss sensor2_active | |
160 bcf use_O2_sensor2 | |
161 btfss sensor3_active | |
162 bcf use_O2_sensor3 | |
113 | 163 |
604 | 164 calibrate_mix3: |
165 ; clear calibration flags if sensors are not found to be ok | |
166 btfss use_O2_sensor1 ; sensor 1 out of range? | |
167 bcf sensor1_calibrated_ok ; YES - disable this sensor | |
168 btfss use_O2_sensor2 ; sensor 2 out of range? | |
169 bcf sensor2_calibrated_ok ; YES - disable this sensor | |
170 btfss use_O2_sensor3 ; sensor 3 out of range? | |
171 bcf sensor3_calibrated_ok ; YES - disable this sensor | |
113 | 172 |
604 | 173 ; When no sensor is found, enable all three to show error state and clear calibration factors |
192 | 174 btfsc use_O2_sensor1 |
113 | 175 return |
192 | 176 btfsc use_O2_sensor2 |
113 | 177 return |
192 | 178 btfsc use_O2_sensor3 |
113 | 179 return |
604 | 180 ; Enable all sensors |
192 | 181 bsf use_O2_sensor1 |
182 bsf use_O2_sensor2 | |
183 bsf use_O2_sensor3 | |
604 | 184 ; Clear calibration factors |
582 | 185 banksel opt_x_s1+0 |
113 | 186 clrf opt_x_s1+0 |
187 clrf opt_x_s1+1 | |
188 clrf opt_x_s2+0 | |
189 clrf opt_x_s2+1 | |
190 clrf opt_x_s3+0 | |
191 clrf opt_x_s3+1 | |
582 | 192 banksel common |
113 | 193 return |
194 | |
604 | 195 calibrate_mix_helper: |
196 movlw LOW min_mv ; load minimum threshold... | |
197 movwf sub_b+0 ; ...into sub_b | |
198 movlw HIGH min_mv | |
199 movwf sub_b+1 | |
200 call sub16 ; sub_c = sub_a - sub_b | |
201 btfsc neg_flag ; sensor mV lower than minimum threshold? | |
202 retlw .1 ; YES - return signaling threshold violation | |
203 movlw LOW max_mv ; load maximum threshold... | |
204 movwf sub_b+0 ; ...into sub_b | |
205 movlw HIGH max_mv | |
206 movwf sub_b+1 | |
207 call sub16 ; sub_c = sub_a - sub_b | |
208 btfss neg_flag ; sensor mV higher than maximum threshold? | |
209 retlw .1 ; YES - return signaling threshold violation | |
210 retlw .0 ; NO - return signaling min/max ok | |
560 | 211 |
448 | 212 |
560 | 213 global compute_mvolts_for_all_sensors |
604 | 214 compute_mvolts_for_all_sensors: ; compute mV or all sensors (S8 mode) |
113 | 215 ; compute AD results in 100µV steps (16bit/sensor) |
216 ; 24bit AD result is in 244,1406541nV | |
582 | 217 ; Divide 24bit value through 409,5999512 -> 410 (0,01% error) |
218 #DEFINE ad2mv_factor .410 | |
604 | 219 movlw LOW ad2mv_factor |
220 movwf xB+0 | |
221 movlw HIGH ad2mv_factor | |
222 movwf xB+1 | |
560 | 223 ; Sensor 1 |
224 clrf xC+3 | |
225 movff s8_rawdata_sensor1+2,xC+2 | |
226 movff s8_rawdata_sensor1+1,xC+1 | |
227 movff s8_rawdata_sensor1+0,xC+0 | |
582 | 228 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
560 | 229 movff xC+1,o2_mv_sensor1+1 |
604 | 230 movff xC+0,o2_mv_sensor1+0 ; in 100 uV steps |
560 | 231 ; Sensor 2 |
232 clrf xC+3 | |
233 movff s8_rawdata_sensor2+2,xC+2 | |
234 movff s8_rawdata_sensor2+1,xC+1 | |
235 movff s8_rawdata_sensor2+0,xC+0 | |
582 | 236 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
560 | 237 movff xC+1,o2_mv_sensor2+1 |
604 | 238 movff xC+0,o2_mv_sensor2+0 ; in 100 uV steps |
560 | 239 ; Sensor 3 |
240 clrf xC+3 | |
241 movff s8_rawdata_sensor3+2,xC+2 | |
242 movff s8_rawdata_sensor3+1,xC+1 | |
243 movff s8_rawdata_sensor3+0,xC+0 | |
582 | 244 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
560 | 245 movff xC+1,o2_mv_sensor3+1 |
604 | 246 movff xC+0,o2_mv_sensor3+0 ; in 100 uV steps |
352
5c6da9fa5cb0
add setpoint change to first sample in CCR mode
heinrichsweikamp
parents:
275
diff
changeset
|
247 |
604 | 248 bcf new_s8_data_available ; clear flag |
249 return ; done | |
113 | 250 |
251 END |