Mercurial > public > hwos_code
annotate src/ms5541.asm @ 643:7d8a4c60ec1a
3.15 release
author | heinrichsweikamp |
---|---|
date | Mon, 24 May 2021 18:40:53 +0200 |
parents | 4050675965ea |
children | 070528a88715 357341239438 |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
634 | 3 ; File ms5541.asm * combined next generation V3.9.4f |
0 | 4 ; |
634 | 5 ; Sensor Subroutines |
0 | 6 ; |
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
8 ;============================================================================= | |
9 ; HISTORY | |
10 ; 2011-08-03 : [mH] moving from OSTC code | |
11 | |
623 | 12 #include "hwos.inc" ; Mandatory header |
13 #include "math.inc" ; Math routines | |
643 | 14 #include "i2c.inc" |
15 #include "shared_definitions.h" ; mailbox from/to p2_deco.c | |
623 | 16 |
17 ms5541 CODE | |
0 | 18 |
634 | 19 |
623 | 20 ; Expose internal variables to ease debug |
604 | 21 global D1, D2 |
22 global C1, C2, C3, C4, C5, C6 | |
623 | 23 global xdT, xdT2, OFF, SENS, pressure_abs_avg, temperature_avg |
0 | 24 |
623 | 25 |
634 | 26 ;----------------------------------------------------------------------------- |
27 ; Convert Temperature & Pressure raw Values to compensated Values | |
28 ; | |
29 ; called from ISR and from sleep mode, returns in bank isr_data | |
30 ; | |
31 global calculate_compensation | |
0 | 32 calculate_compensation: |
623 | 33 banksel isr_backup ; select bank ISR data |
34 | |
643 | 35 btfsc press_sensor_type ; New sensor found? |
36 bra press_comp_ms5837 ; Yes, use MS5837 | |
37 | |
623 | 38 ;---- pressure sensor compensation |
39 | |
40 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350) | |
41 movf C5+0,W ; get value to be subtracted | |
42 subwf D2+0,W ; do the low byte | |
604 | 43 movwf xdT+0 |
623 | 44 movf C5+1,W ; do the high byte |
604 | 45 subwfb D2+1,W |
46 movwf xdT+1 | |
0 | 47 |
623 | 48 ; second order temperature calculation |
49 | |
50 ; xdT/128 is in range -89..+96, hence signed 8 bit. dT/128 = (2*dT)/256 | |
51 rlcf xdT+0,W ; put hit bit in carry | |
52 rlcf xdT+1,W ; inject in high byte | |
53 movwf isr_xA+0 ; and put result in low byte | |
604 | 54 clrf isr_xA+1 |
623 | 55 btfsc xdT+1,7 ; dT < 0 ? |
56 setf isr_xA+1 ; YES - sign extend to -1 | |
57 MOVII isr_xA,isr_xB ; copy A to B | |
604 | 58 call isr_signed_mult16x16 ; dT*dT --> xC (32 bits) |
0 | 59 |
623 | 60 ; dT >= 0: divide by 8, i.e. 3 shifts rights |
61 ; dT < 0: divide by 2, i.e. 1 shifts rights | |
634 | 62 movlw .3 ; 3 shifts by default |
63 btfss xdT+1,7 ; was dT negative ? | |
64 movlw .1 ; YES - replace by 1 shift | |
0 | 65 calc_loop_1: |
634 | 66 bcf STATUS,C ; dT^2 is positive, so injecte zeros |
67 rrcf isr_xC+1,F ; shift right high byte | |
68 rrcf isr_xC+0,F ; shift right low byte | |
69 decfsz WREG ; decrement loop counter, all shifts done? | |
70 bra calc_loop_1 ; NO - loop | |
0 | 71 |
604 | 72 movf isr_xC+0,W ; dT2 = dT - (dT/128)*(dT/128)/(2 ...or... 8) |
634 | 73 subwf xdT+0,W ; ... |
74 movwf xdT2+0 ; ... | |
75 movf isr_xC+1,W ; ... | |
76 subwfb xdT+1,W ; ... | |
77 movwf xdT2+1 ; ... | |
604 | 78 |
623 | 79 ; calculate OFF = C2 + ((C4-250)*dT2)/2^12 + 10000 (range +9.246 .. +18.887) |
80 MOVII C4, isr_xA ; C4 - 250 --> A | |
81 MOVII xdT2,isr_xB ; dT2 --> B | |
634 | 82 call isr_signed_mult16x16 ; C = A*B |
0 | 83 |
623 | 84 movlw .12-.8 ; a 12 bit shift = 1 byte + 4 bits |
634 | 85 call isr_shift_C31 ; special shift |
368
57e349960ef4
Additional temperature calibration via PC interface
heinrichsweikamp
parents:
353
diff
changeset
|
86 |
623 | 87 movlw LOW(.10000) ; add 10000 |
634 | 88 addwf isr_xC+1,F ; ... |
89 movlw HIGH(.10000) ; ... | |
90 addwfc isr_xC+2,F ; ... | |
0 | 91 |
623 | 92 movf C2+0,W ; add C2 and store result in OFF |
634 | 93 addwf isr_xC+1,W ; ... |
94 movwf OFF+0 ; ... | |
95 movf C2+1,W ; ... | |
96 addwfc isr_xC+2,W ; ... | |
97 movwf OFF+1 ; ... | |
604 | 98 |
623 | 99 ; calculate SENS = C1/2 + ((C3+200)*dT)/2^13 + 3000 |
604 | 100 movlw LOW(.200) ; C3+200 --> A |
634 | 101 addwf C3+0,W ; ... |
102 movwf isr_xA+0 ; ... | |
103 movlw HIGH(.200) ; ... | |
104 addwfc C3+1,W ; ... | |
105 movwf isr_xA+1 ; ... | |
604 | 106 ; B still contains dT2 |
634 | 107 call isr_signed_mult16x16 ; C = A*B |
623 | 108 movlw .13-.8 ; A 13 bit shift = 1 byte + 5 bits |
634 | 109 call isr_shift_C31 ; special shift |
0 | 110 |
604 | 111 bcf STATUS,C ; SENS = C1 / 2 |
634 | 112 rrcf C1+1,W ; ... |
113 movwf SENS+1 ; ... | |
114 rrcf C1+0,W ; ... | |
115 movwf SENS+0 ; ... | |
604 | 116 |
623 | 117 movlw LOW(.3000) ; add 3000 |
634 | 118 addwf isr_xC+1,F ; ... |
119 movlw HIGH(.3000) ; ... | |
120 addwfc isr_xC+2,F ; ... | |
0 | 121 |
623 | 122 movf isr_xC+1,W ; and sum into SENS |
634 | 123 addwf SENS+0,F ; ... |
124 movf isr_xC+2,W ; ... | |
125 addwfc SENS+1,F ; ... | |
0 | 126 |
631 | 127 ; calculate absolute pressure = (sens * (d1-off))/2^12 + 1000 (for MS5541C) |
128 ; calculate absolute pressure = (sens * (d1-off))/2^11 + 1000 (for MS5541C-30) | |
604 | 129 movf OFF+0,W ; d1-off --> a |
634 | 130 subwf D1+0,W ; ... |
131 movwf isr_xA+0 ; ... | |
132 movf OFF+1,W ; ... | |
133 subwfb D1+1,W ; ... | |
134 movwf isr_xA+1 ; ... | |
0 | 135 |
634 | 136 MOVII SENS,isr_xB ; sens --> B |
137 call isr_signed_mult16x16 ; C = A*B | |
643 | 138 movlw .15 ; 15 * 256 = 3840 |
631 | 139 cpfslt C1+1 ; C1 > 3328 ? |
140 bra isr_shift_ms5541_30 ; YES - MS5541-30 | |
141 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits | |
634 | 142 bra isr_shift_ms5541_common ; - continue |
631 | 143 isr_shift_ms5541_30: |
144 movlw .11-.8 ; MS5541-30: 11 bit shift = 1 byte + 3 bits | |
145 isr_shift_ms5541_common: | |
634 | 146 call isr_shift_C31 ; special shift |
623 | 147 movlw LOW .1000 ; add 1000 |
634 | 148 addwf isr_xC+1,F ; ... |
149 movlw HIGH .1000 ; ... | |
150 addwfc isr_xC+2,F ; ... | |
643 | 151 bra press_comp_done_common |
0 | 152 |
643 | 153 press_comp_ms5837: |
154 ; xdT = D2 - C5*2^8 | |
155 movlw .0 ; Low | |
156 subwf D2+0,W | |
157 movwf xdT+0 | |
158 movf C5+0,W ; High | |
159 subwfb D2+1,W | |
160 movwf xdT+1 | |
161 movf C5+1,W ; Upper | |
162 subwfb D2+2,W | |
163 movwf xdT+2 | |
164 ; xdT equals D2 - (C5*2^8) | |
165 | |
166 ; Calculate OFF = C2*2^16+(C4*xdT)/2^7 | |
167 movff C4+0,isr_xA+0 | |
168 movff C4+1,isr_xA+1 | |
169 clrf isr_xA+2 | |
170 movff xdT+0,isr_xB+0 | |
171 movff xdT+1,isr_xB+1 | |
172 movff xdT+2,isr_xB+2 | |
173 call isr_signed_mult24x24 ; C = A*B | |
174 ; isr_xC:6 equals (C4*xdT) | |
175 rlcf isr_xC+0,F | |
176 rlcf isr_xC+1,F | |
177 rlcf isr_xC+2,F | |
178 rlcf isr_xC+3,F | |
179 rlcf isr_xC+4,F | |
180 rlcf isr_xC+5,F | |
181 movff isr_xC+1,OFF+0 | |
182 movff isr_xC+2,OFF+1 | |
183 movff isr_xC+3,OFF+2 | |
184 movff isr_xC+4,OFF+3 | |
185 ; OFF equals (C4*xdT)/2^7 | |
186 movf C2+0,W | |
187 addwf OFF+2,F | |
188 movf C2+1,W | |
189 addwfc OFF+3,F | |
190 ; OFF equals C2*2^16+((C4*xdT)/2^7) | |
191 | |
192 ; Calculate SENS = C1*2^15 + (C3*xdT)/2^8 | |
193 MOVII C1,isr_xA | |
194 movlw LOW (.32768) | |
195 movwf isr_xB+0 | |
196 movlw HIGH (.32768) | |
197 movwf isr_xB+1 | |
198 call isr_unsigned_mult16x16 ; C = A*B | |
199 movff isr_xC+0,SENS+0 | |
200 movff isr_xC+1,SENS+1 | |
201 movff isr_xC+2,SENS+2 | |
202 movff isr_xC+3,SENS+3 ; 32bit copy | |
203 ; SENS equals C1*2^15 | |
204 movff C3+0,isr_xA+0 | |
205 movff C3+1,isr_xA+1 | |
206 clrf isr_xA+2 | |
207 movff xdT+0,isr_xB+0 | |
208 movff xdT+1,isr_xB+1 | |
209 movff xdT+2,isr_xB+2 | |
210 call isr_signed_mult24x24 ; C = A*B | |
211 ; isr_xC:6 equals (C3*xdT) | |
212 movf isr_xC+1,W | |
213 addwf SENS+0,F | |
214 movf isr_xC+2,W | |
215 addwfc SENS+1,F | |
216 movf isr_xC+3,W | |
217 addwfc SENS+2,F | |
218 movf isr_xC+4,W | |
219 addwfc SENS+3,F | |
220 ; SENS equals C1*2^15 + (C3*xdT)/2^8 | |
221 | |
222 ; Calculate PRESSURE = ((D1 * SENS) / 2^21) - OFF) / 2^13 | |
223 movff SENS+0,isr_xA+0 | |
224 movff SENS+1,isr_xA+1 | |
225 movff SENS+2,isr_xA+2 | |
226 movff SENS+3,isr_xA+3 | |
227 movff D1+0,isr_xB+0 | |
228 movff D1+1,isr_xB+1 | |
229 movff D1+2,isr_xB+2 | |
230 call isr_unsigned_mult32x24 ; C = A*B | |
231 ; isr_xC:7 equals D1*SENS | |
232 | |
233 ; devide isr_xC:7 / 2^21 -> shift right 21 times | |
234 movlw .21 | |
235 press_comp_ms5837_2: | |
236 bcf STATUS,C ; clear carry, needed for MS5837 use | |
237 rrcf isr_xC+6,F | |
238 rrcf isr_xC+5,F | |
239 rrcf isr_xC+4,F | |
240 rrcf isr_xC+3,F | |
241 rrcf isr_xC+2,F | |
242 rrcf isr_xC+1,F | |
243 rrcf isr_xC+0,F | |
244 decfsz WREG ; decrement loop counter, done? | |
245 bra press_comp_ms5837_2 ; NO - loop | |
246 ; isr_xC:6 equals (D1*SENS)/2^21 | |
247 | |
248 ; Calculate isr_xC:6-OFF | |
249 movf OFF+0,W ; Low | |
250 subwf isr_xC+0,F | |
251 movf OFF+1,W ; High | |
252 subwfb isr_xC+1,F | |
253 movf OFF+2,W ; Upper | |
254 subwfb isr_xC+2,F | |
255 movf OFF+3,W ; Extra | |
256 subwfb isr_xC+3,F | |
257 movlw .0 ; ultra | |
258 subwfb isr_xC+4,F | |
259 movlw .0 ; Hyper | |
260 subwfb isr_xC+5,F | |
261 ; isr_xC:5 is now isr_xC:4-OFF | |
262 | |
263 ; devide by 2^13 | |
264 ; devide isr_xC:6 / 2^13 -> shift right 13 times | |
265 movlw .13 | |
266 press_comp_ms5837_3: | |
267 bcf STATUS,C ; clear carry | |
268 rrcf isr_xC+5,F | |
269 rrcf isr_xC+4,F | |
270 rrcf isr_xC+3,F | |
271 rrcf isr_xC+2,F | |
272 rrcf isr_xC+1,F | |
273 rrcf isr_xC+0,F | |
274 decfsz WREG ; decrement loop counter, done? | |
275 bra press_comp_ms5837_3 ; NO - loop | |
276 ; isr_xC:4 equals pressure in .1 mbar | |
277 | |
278 movlw .10 | |
279 movwf isr_xB+0 | |
280 clrf isr_xB+1 | |
281 call isr_div32x16 ; isr_xC:4 = isr_xC:4 / isr_xB:2 with isr_xA as remainder | |
282 ; isr_xC:2 equals pressure in 1mbar | |
283 | |
284 ; subtract 20mbar from the final result | |
285 movlw .20 | |
286 subwf isr_xC+0,F | |
287 movlw .0 | |
288 subwfb isr_xC+1,F | |
289 | |
290 ; copy for compatibility of the next routines | |
291 movff isr_xC+1,isr_xC+2 | |
292 movff isr_xC+0,isr_xC+1 | |
293 | |
294 press_comp_done_common: ; continue here even with MS5837 | |
623 | 295 ; add opt_pressure_adjust to result (SIGNED!) |
634 | 296 clrf isr_xC+0 ; prepare high byte for adjustment |
297 movff opt_pressure_adjust,WREG ; get low byte for adjustment (signed) | |
298 movf WREG,W ; excite flags | |
299 bz calc_compensation_1 ; opt_pressure_adjust = 0 -> skip adjustment | |
300 btfss STATUS,N ; opt_pressure_adjust < 0 ? | |
301 bra calc_compensation_adjust ; NO - positive | |
302 negf WREG ; YES - negate opt_pressure_adjust for limit check | |
303 setf isr_xC+0 ; - adopt high byte for adjustment | |
304 calc_compensation_adjust: | |
305 addlw -.21 ; limit is 20 mbar, subtract 21 | |
306 bnn calc_compensation_1 ; result not negative -> skip adjustment | |
307 movff opt_pressure_adjust,WREG ; get opt_pressure_adjust, low byte | |
623 | 308 addwf isr_xC+1,F ; pressure value += opt_pressure_adjust, low byte |
634 | 309 movf isr_xC+0,W ; get adjustment, high byte |
310 addwfc isr_xC+2,F ; pressure value += opt_pressure_adjust, high byte | |
623 | 311 |
312 calc_compensation_1: | |
313 bcf sensor_override_active ; clear sensor override active flag by default | |
314 btfss sensor_override_request ; sensor override requested? | |
315 bra calc_compensation_add_avg ; NO - keep taking absolute pressure from sensor | |
316 btfsc quit_simulatormode ; YES - shall quit simulator mode (the fast way)? | |
317 bra calc_compensation_sim_quit ; YES - force pressure_rel_sim to zero | |
318 ;bra calc_compensation_sim ; NO - calculate pressure_rel_sim from simulated depth | |
319 | |
320 calc_compensation_sim: ; check if OSTC got submerged | |
321 movlw .5 ; coding in high byte for 1280 | |
322 cpfsgt isr_xC+2 ; absolute pressure > 1280 mbar, i.e. OSTC submerged? | |
323 bra calc_compensation_sim_slave ; NO - slave pressure_rel_sim to target depth | |
324 ;bra calc_compensation_sim_quit ; YES - force pressure_rel_sim to zero | |
92
7ca1105751c7
add sensor calibration option (PC only), some cleanup
heinrichsweikamp
parents:
29
diff
changeset
|
325 |
623 | 326 calc_compensation_sim_quit: |
327 CLRI pressure_rel_sim ; set pressure_rel_sim to zero, this paves a restart into surface/dive mode | |
328 bra calc_compensation_sim_com ; continue with common part | |
329 | |
330 calc_compensation_sim_slave: | |
331 movf simulatormode_depth,W ; copy simulated depth to WREG | |
332 mullw .100 ; multiply with 100 to turn depth from meters to relative target pressure in mbar | |
333 ; check if shall go up | |
334 movf pressure_rel_sim+0,W ; get relative pressure, low byte | |
335 subwf PRODL,W ; WREG = relative target pressure - pressure_rel_sim (low byte) | |
336 movf pressure_rel_sim+1,W ; get relative pressure, high byte | |
337 subwfb PRODH,W ; WREG = relative target pressure - pressure_rel_sim (high byte) | |
338 btfsc STATUS,N ; result < 0, i.e. pressure_rel_sim > relative target pressure ? | |
339 bra calc_compensation_sim_up ; YES - decrease pressure_rel_sim | |
340 ; check if shall go down | |
341 movf PRODL,W ; get relative target pressure, low byte | |
342 subwf pressure_rel_sim+0,W ; WREG = pressure_rel_sim - relative target pressure (low byte) | |
343 movf PRODH,W ; get relative target pressure, high byte | |
344 subwfb pressure_rel_sim+1,W ; WREG = pressure_rel_sim - relative target pressure (high byte) | |
345 btfsc STATUS,N ; result < 0, i.e. relative target pressure > pressure_rel_sim ? | |
346 bra calc_compensation_sim_down ; YES - increase pressure_rel_sim | |
347 ; both pressures are equal | |
348 bra calc_compensation_sim_com ; NO to both - keep pressure_rel_sim as it is | |
0 | 349 |
623 | 350 calc_compensation_sim_up: |
351 movf PRODL,W ; get relative target pressure, low byte | |
352 subwf pressure_rel_sim+0,W ; WREG = pressure_rel_sim - relative target pressure (low byte) | |
353 movwf PRODL ; PRODL = pressure_rel_sim - relative target pressure (low byte) | |
354 movf PRODH,W ; get relative target pressure, high byte | |
355 subwfb pressure_rel_sim+1,W ; WREG = pressure_rel_sim - relative target pressure (high byte) | |
356 tstfsz WREG ; more than 255 mbar off from target? | |
357 bra calc_compensation_sim_up_norm ; YES - go up with normal speed | |
358 movlw simulator_ascent_threshold ; NO - get remaining difference for slowing down ascent | |
359 cpfslt PRODL ; - remaining difference to target < decrement? | |
360 bra calc_compensation_sim_up_norm ; NO - go up with normal speed | |
361 ;bra calc_compensation_sim_up_slow ; YES - go up with slow speed | |
362 | |
363 calc_compensation_sim_up_slow: | |
364 DECI pressure_rel_sim ; subtract slow decrement (1 mbar) from pressure_rel_sim | |
365 bra calc_compensation_sim_com ; continue with common part | |
366 | |
367 calc_compensation_sim_up_norm: | |
368 SUBLI simulator_ascent_rate,pressure_rel_sim ; subtract normal decrement from pressure_rel_sim | |
369 bra calc_compensation_sim_com ; continue with common part | |
353
573f2251cf49
NEW: Quit Simulator automatically when starting a real dive
heinrichsweikamp
parents:
275
diff
changeset
|
370 |
623 | 371 calc_compensation_sim_down: |
372 movf pressure_rel_sim+0,W ; get relative pressure, low byte | |
373 subwf PRODL,F ; PRODL = relative target pressure - pressure_rel_sim (low byte) | |
374 movf pressure_rel_sim+1,W ; get relative pressure, high byte | |
375 subwfb PRODH,W ; WREG = relative target pressure - pressure_rel_sim (high byte) | |
376 tstfsz WREG ; more than 255 mbar off from target? | |
377 bra calc_compensation_sim_down_norm ; YES - go down with normal speed | |
378 movlw simulator_descent_threshold ; NO - get remaining difference for slowing down descent | |
379 cpfslt PRODL ; - remaining difference to target < increment? | |
380 bra calc_compensation_sim_down_norm ; NO - go down with normal speed | |
381 ;bra calc_compensation_sim_down_slow ; YES - go down with slow speed | |
382 | |
383 calc_compensation_sim_down_slow: | |
384 INCI pressure_rel_sim ; add slow increment (1 mbar) to pressure_rel_sim | |
385 bra calc_compensation_sim_com ; continue with common part | |
604 | 386 |
623 | 387 calc_compensation_sim_down_norm: |
388 ADDLI simulator_descent_rate,pressure_rel_sim ; add normal increment to pressure_rel_sim | |
389 ;bra calc_compensation_sim_com ; continue with common part | |
390 | |
391 calc_compensation_sim_com: | |
392 movf pressure_surf+0,W ; copy surface pressure to WREG, low byte | |
393 addwf pressure_rel_sim+0,W ; add surface pressure to relative pressure to gain simulated absolute pressure, low byte | |
394 movwf isr_xC+1 ; override sensor pressure with simulated pressure, low byte | |
395 movf pressure_surf+1,W ; copy surface pressure to WREG, high byte | |
396 addwfc pressure_rel_sim+1,W ; add surface pressure to relative pressure to gain simulated absolute pressure, high byte | |
397 movwf isr_xC+2 ; override sensor pressure with simulated pressure, high byte | |
398 bsf sensor_override_active ; confirm sensor override is active | |
399 | |
400 calc_compensation_add_avg: | |
401 ; add current absolute pressure to averaging buffer | |
402 movf isr_xC+1,W ; copy current absolute pressure to WREG, low byte | |
403 addwf pressure_abs_avg+0,F ; pressure_abs_avg += current pressure, low byte | |
404 movf isr_xC+2,W ; copy current absolute pressure to WREG, high byte | |
405 addwfc pressure_abs_avg+1,F ; pressure_abs_avg += current pressure, high byte | |
643 | 406 movlw .0 |
407 addwfc pressure_abs_avg+2,F ; pressure_abs_avg += current pressure, upper byte | |
623 | 408 |
409 ;---- temperature sensor compensation | |
0 | 410 |
643 | 411 btfsc press_sensor_type ; New sensor found? |
412 bra temp_comp_ms5837 ; Yes, use MS5837 | |
413 | |
604 | 414 ; calculate temp = 200 + dT*(C6+100)/2^11 |
415 movlw LOW(.100) ; C6 + 100 --> A | |
634 | 416 addwf C6+0,W ; ... |
417 movwf isr_xA+0 ; ... | |
418 movlw HIGH(.100) ; ... | |
419 addwfc C6+1,W ; ... | |
420 movwf isr_xA+1 ; ... | |
0 | 421 |
623 | 422 MOVII xdT2,isr_xB ; dT2 --> B |
604 | 423 call isr_signed_mult16x16 ; A*B |
623 | 424 movlw .11-.8 ; a 12 bit shift = 1 byte + 3 bits |
634 | 425 call isr_shift_C31 ; special shift |
0 | 426 |
623 | 427 movlw LOW(.200) ; add 200 |
634 | 428 addwf isr_xC+1,F ; ... |
429 movlw HIGH(.200) ; ... | |
430 addwfc isr_xC+2,F ; ... | |
643 | 431 bra temp_comp_done_common |
0 | 432 |
643 | 433 temp_comp_ms5837: |
434 ; TEMP = 2000+((xdT * C6) / 2^23) | |
435 | |
436 ; xdT * C6 -> isr_xC:6 | |
437 movff C6+0,isr_xA+0 | |
438 movff C6+1,isr_xA+1 | |
439 clrf isr_xA+2 | |
440 movff xdT+0,isr_xB+0 | |
441 movff xdT+1,isr_xB+1 | |
442 movff xdT+2,isr_xB+2 | |
443 call isr_signed_mult24x24 ; A*B | |
444 | |
445 ; devide isr_xC:6 / 2^23 -> shift right 23 times | |
446 ; a special variant (again): We loose isr_xC+0 and isr_xC+1 anyway | |
447 ; and do not care about the inserted carry since we won't use isr_xC+5 | |
448 ; and we shift less then 8 bytes | |
449 movlw .23-.16 | |
450 temp_comp_ms5837_2: | |
451 rrcf isr_xC+5,F ; | |
452 rrcf isr_xC+4,F ; | |
453 rrcf isr_xC+3,F ; | |
454 rrcf isr_xC+2,F ; ... | |
455 decfsz WREG ; decrement loop counter, done? | |
456 bra temp_comp_ms5837_2 ; NO - loop | |
457 | |
458 ; only use +2 and +3 | |
459 movlw LOW(.2000) ; add 2000 | |
460 addwf isr_xC+2,F ; ... | |
461 movlw HIGH(.2000) ; ... | |
462 addwfc isr_xC+3,F ; ... | |
463 | |
464 ; devide by 10 for .1°C resolution | |
465 movlw .10 | |
466 movwf isr_xB+0 | |
467 clrf isr_xB+1 | |
468 movff isr_xC+2,isr_xA+0 | |
469 movff isr_xC+3,isr_xA+1 | |
470 | |
471 btfss isr_xC+3,7 ; check sign bit | |
472 bra temp_comp_ms5837_3 ; not negative... | |
473 ; negate isr_xA:2 to make it positive | |
474 comf isr_xA+1 ; 16 bit sign change | |
475 negf isr_xA+0 | |
476 btfsc STATUS,C ; - carry to propagate? | |
477 incf isr_xA+1,F ; YES - do it | |
478 | |
479 temp_comp_ms5837_3: | |
480 call isr_div16x16 ; isr_xC:2 = isr_xA:2 / isr_xB:2 | |
481 | |
482 btfss isr_xC+3,7 ; check sign bit again | |
483 bra temp_comp_ms5837_4 ; not negative... | |
484 ; negate result isr_xC:2 to make it negative again | |
485 comf isr_xC+1 ; 16 bit sign change | |
486 negf isr_xC+0 | |
487 btfsc STATUS,C ;- carry to propagate? | |
488 incf isr_xC+1,F ; YES - do it | |
489 | |
490 temp_comp_ms5837_4: | |
491 ; copy for compatibility of the next routines | |
492 movff isr_xC+1,isr_xC+2 | |
493 movff isr_xC+0,isr_xC+1 | |
494 | |
495 temp_comp_done_common: ; continue here even with MS5837 | |
623 | 496 ; add opt_temperature_adjust to result (SIGNED!) |
634 | 497 clrf isr_xC+0 ; prepare high byte for adjustment |
498 movff opt_temperature_adjust,WREG ; get low byte for adjustment (signed) | |
499 movf WREG,W ; excite flags | |
500 bz calc_temperature_add_avg ; opt_temperature_adjust = 0 -> skip adjustment | |
501 btfss STATUS,N ; opt_temperature_adjust < 0 ? | |
502 bra calc_temperature_adjust ; NO - positive | |
503 negf WREG ; YES - negate opt_temperature_adjust for limit check | |
504 setf isr_xC+0 ; - adopt high byte for adjustment | |
505 calc_temperature_adjust: | |
506 addlw -.21 ; limit is 2.0°C, subtract 21 (scaling is 0.1°C) | |
507 bnn calc_temperature_add_avg ; result not negative -> skip adjustment | |
508 movff opt_temperature_adjust,WREG ; get opt_temperature_adjust, low byte | |
509 addwf isr_xC+1,F ; temperature value += opt_temperature_adjust, low byte | |
510 movf isr_xC+0,W ; get adjustment high byte | |
511 addwfc isr_xC+2,F ; temperature value += opt_temperature_adjust, high byte | |
368
57e349960ef4
Additional temperature calibration via PC interface
heinrichsweikamp
parents:
353
diff
changeset
|
512 |
634 | 513 calc_temperature_add_avg: |
514 movf isr_xC+1,W ; copy current temperature to WREG, low byte | |
515 addwf temperature_avg+0,F ; temperature_avg += current temperature, low byte | |
516 movf isr_xC+2,W ; copy current temperature to WREG, high byte | |
517 addwfc temperature_avg+1,F ; temperature_avg += current temperature, high byte | |
518 return ; done | |
519 | |
623 | 520 |
634 | 521 ;----------------------------------------------------------------------------- |
522 ; Start Pressure Measurement | |
523 ; | |
524 ; called from ISR and sleep mode, needs to be called in bank isr_backup | |
525 ; | |
526 global get_pressure_start | |
527 get_pressure_start: | |
643 | 528 btfsc press_sensor_type ; New sensor found? |
529 return ; Yes, ignore routine | |
634 | 530 rcall reset_MS5541 ; reset the chip |
531 movlw b'10100000' ; +3*high as start and 1+low as stop | |
532 movwf dbuffer ; .... | |
533 movlw d'12' ; send start command | |
643 | 534 bra send_data_MS5541 ; ... (And return) |
0 | 535 |
623 | 536 |
634 | 537 ;----------------------------------------------------------------------------- |
538 ; Read Pressure Measurement Result | |
539 ; | |
540 ; called from ISR and sleep mode, needs to be called in bank isr_backup | |
541 ; | |
542 global get_pressure_value | |
0 | 543 get_pressure_value: |
643 | 544 btfsc press_sensor_type ; New sensor found? |
545 bra get_pressure_value2 | |
623 | 546 btfsc MS5541_miso ; conversion done? |
634 | 547 return ; NO - abort |
548 rcall get_2bytes_MS5541 ; YES - read result | |
549 movff dMSB,D1+1 ; copy result to D1, high byte first | |
550 movff dLSB,D1+0 ; ... low byte second | |
551 return ; done | |
0 | 552 |
643 | 553 get_pressure_value2: |
554 btfsc i2c_busy_pressure ; currently updating pressure? | |
555 return ; Yes, skip update... | |
556 movff D1_buffer+0,D1+0 | |
557 movff D1_buffer+1,D1+1 | |
558 movff D1_buffer+2,D1+2 | |
559 return | |
560 | |
623 | 561 |
634 | 562 ;----------------------------------------------------------------------------- |
563 ; Start Temperature Measurement | |
564 ; | |
565 ; called from ISR and sleep mode, needs to be called in bank isr_backup | |
566 ; | |
567 global get_temperature_start | |
568 get_temperature_start: | |
643 | 569 btfsc press_sensor_type ; New sensor found? |
570 return ; Yes, ignore routine | |
634 | 571 rcall reset_MS5541 ; reset chip |
572 movlw b'10010000' ; +3*high as start and 1+low as stop | |
573 movwf dbuffer ; ... | |
574 movlw d'12' ; send start command | |
643 | 575 bra send_data_MS5541 ; ... (And return) |
634 | 576 |
577 | |
578 ;----------------------------------------------------------------------------- | |
579 ; Read Pressure Measurement Result | |
580 ; | |
581 ; called from ISR and sleep mode, needs to be called in bank isr_backup | |
582 ; | |
583 global get_temperature_value | |
0 | 584 get_temperature_value: |
643 | 585 btfsc press_sensor_type ; New sensor found? |
586 bra get_temperature_value2 | |
623 | 587 btfsc MS5541_miso ; conversion done? |
588 return ; NO - done | |
634 | 589 rcall get_2bytes_MS5541 ; YES - read result |
590 movff dMSB,D2+1 ; copy result to D2, high byte first | |
591 movff dLSB,D2+0 ; ... low byte second | |
592 return ; done | |
593 | |
643 | 594 get_temperature_value2: |
595 btfsc i2c_busy_temperature ; currently updating temperature? | |
596 return ; Yes, skip update... | |
597 movff D2_buffer+0,D2+0 | |
598 movff D2_buffer+1,D2+1 | |
599 movff D2_buffer+2,D2+2 | |
600 return | |
601 | |
0 | 602 |
634 | 603 ;----------------------------------------------------------------------------- |
604 ; Retrieve Calibration Data | |
605 ; | |
606 ; called by start, returns in bank common | |
607 ; | |
608 global get_calibration_data | |
0 | 609 get_calibration_data: |
623 | 610 banksel isr_backup ; select bank ISR data |
643 | 611 bsf block_sensor_interrupt ; disable sensor interrupts |
0 | 612 |
643 | 613 btfsc press_sensor_type ; New sensor found? |
614 call I2C_get_calib_MS5837 ; Yes, read C1 to C6 | |
615 banksel isr_backup ; select bank ISR data | |
616 | |
617 btfsc press_sensor_type ; New sensor found? | |
618 bra get_calibration_data2 ; Yes, skip to the end | |
619 | |
634 | 620 rcall reset_MS5541 ; reset chip |
621 | |
623 | 622 movlw b'01010100' ; +3*high as start and 1+low as stop |
634 | 623 movwf dbuffer ; ... |
624 movlw d'13' ; send command | |
625 rcall send_data_MS5541 ; ... | |
626 rcall get_2bytes_MS5541 ; read result | |
627 movff dMSB,ir_s8_buffer+1 ; copy result, high byte first | |
628 movff dLSB,ir_s8_buffer+0 ; copy result, low byte second | |
0 | 629 |
623 | 630 movlw b'01011000' ; +3*high as start and 1+low as stop |
634 | 631 movwf dbuffer ; ... |
632 movlw d'13' ; send command | |
633 rcall send_data_MS5541 ; ... | |
634 rcall get_2bytes_MS5541 ; read result | |
635 movff dMSB,ir_s8_buffer+3 ; copy result, high byte first | |
636 movff dLSB,ir_s8_buffer+2 ; copy result, low byte second | |
0 | 637 |
623 | 638 movlw b'01100100' ; +3*high as start and 1+low as stop |
634 | 639 movwf dbuffer ; ... |
640 movlw d'13' ; send command | |
641 rcall send_data_MS5541 ; ... | |
642 rcall get_2bytes_MS5541 ; read result | |
643 movff dMSB,ir_s8_buffer+5 ; copy result, high byte first | |
644 movff dLSB,ir_s8_buffer+4 ; copy result, low byte second | |
0 | 645 |
623 | 646 movlw b'01101000' ; +3*high as start and 1+low as stop |
634 | 647 movwf dbuffer ; ... |
648 movlw d'13' ; send command | |
649 rcall send_data_MS5541 ; ... | |
650 rcall get_2bytes_MS5541 ; read result | |
651 movff dMSB,ir_s8_buffer+7 ; copy result, high byte first | |
652 movff dLSB,ir_s8_buffer+6 ; copy result, low byte second | |
0 | 653 |
634 | 654 ; calculate C1 (16 Bit) |
655 movff ir_s8_buffer+1,C1+1 | |
0 | 656 bcf STATUS,C |
657 rrcf C1+1 | |
658 bcf STATUS,C | |
659 rrcf C1+1 | |
660 bcf STATUS,C | |
661 rrcf C1+1 | |
634 | 662 movff ir_s8_buffer+0,C1+0 |
0 | 663 bsf STATUS,C |
255 | 664 btfss ir_s8_buffer+1,0 |
0 | 665 bcf STATUS,C |
666 rrcf C1+0 | |
667 bsf STATUS,C | |
255 | 668 btfss ir_s8_buffer+1,1 |
0 | 669 bcf STATUS,C |
670 rrcf C1+0 | |
671 bsf STATUS,C | |
255 | 672 btfss ir_s8_buffer+1,2 |
0 | 673 bcf STATUS,C |
674 rrcf C1+0 | |
675 | |
634 | 676 ; calculate C2 (16 Bit) |
255 | 677 movff ir_s8_buffer+2, C2+0 |
0 | 678 bsf STATUS,C |
255 | 679 btfss ir_s8_buffer+3,0 |
0 | 680 bcf STATUS,C |
681 rrcf C2+0 | |
682 bsf STATUS,C | |
255 | 683 btfss ir_s8_buffer+3,1 |
0 | 684 bcf STATUS,C |
685 rrcf C2+0 | |
686 bsf STATUS,C | |
255 | 687 btfss ir_s8_buffer+3,2 |
0 | 688 bcf STATUS,C |
689 rrcf C2+0 | |
690 bsf STATUS,C | |
255 | 691 btfss ir_s8_buffer+3,3 |
0 | 692 bcf STATUS,C |
693 rrcf C2+0 | |
694 bsf STATUS,C | |
255 | 695 btfss ir_s8_buffer+3,4 |
0 | 696 bcf STATUS,C |
697 rrcf C2+0 | |
698 bsf STATUS,C | |
255 | 699 btfss ir_s8_buffer+3,5 |
0 | 700 bcf STATUS,C |
701 rrcf C2+0 | |
702 | |
255 | 703 movff ir_s8_buffer+3, C2+1 |
0 | 704 bsf STATUS,C |
255 | 705 btfss ir_s8_buffer+0,0 |
0 | 706 bcf STATUS,C |
707 rrcf C2+1 | |
708 bsf STATUS,C | |
255 | 709 btfss ir_s8_buffer+0,1 |
0 | 710 bcf STATUS,C |
711 rrcf C2+1 | |
712 bsf STATUS,C | |
255 | 713 btfss ir_s8_buffer+0,2 |
0 | 714 bcf STATUS,C |
715 rrcf C2+1 | |
716 bcf STATUS,C | |
717 rrcf C2+1 | |
718 bcf STATUS,C | |
719 rrcf C2+1 | |
720 bcf STATUS,C | |
721 rrcf C2+1 | |
722 | |
634 | 723 ; calculate C3 (16 Bit) |
255 | 724 movff ir_s8_buffer+5,C3+0 |
0 | 725 bsf STATUS,C |
255 | 726 btfss ir_s8_buffer+4,7 |
0 | 727 bcf STATUS,C |
728 rlcf C3+0 | |
729 bsf STATUS,C | |
255 | 730 btfss ir_s8_buffer+4,6 |
0 | 731 bcf STATUS,C |
732 rlcf C3+0 | |
733 clrf C3+1 | |
255 | 734 btfsc ir_s8_buffer+5,7 |
0 | 735 bsf C3+1,1 |
255 | 736 btfsc ir_s8_buffer+5,6 |
0 | 737 bsf C3+1,0 |
604 | 738 |
634 | 739 ; calculate C4 (16 Bit) |
255 | 740 movff ir_s8_buffer+7,C4+0 |
0 | 741 bsf STATUS,C |
255 | 742 btfss ir_s8_buffer+6,7 |
0 | 743 bcf STATUS,C |
744 rlcf C4+0 | |
745 clrf C4+1 | |
255 | 746 btfsc ir_s8_buffer+7,7 |
0 | 747 bsf C4+1,0 |
748 | |
623 | 749 ; C4=C4-250 |
604 | 750 movlw LOW(-.250) ; C4 - 250 --> C4 |
0 | 751 addwf C4+0,W |
604 | 752 movwf C4+0 |
753 movlw -1 ; HIGH(- .250) is not understood... | |
754 addwfc C4+1,W | |
755 movwf C4+1 | |
756 | |
634 | 757 ; calculate C5 (16 Bit) |
255 | 758 movff ir_s8_buffer+4,C5+0 |
0 | 759 bcf C5+0,6 |
255 | 760 btfsc ir_s8_buffer+2,0 |
0 | 761 bsf C5+0,6 |
762 bcf C5+0,7 | |
255 | 763 btfsc ir_s8_buffer+2,1 |
0 | 764 bsf C5+0,7 |
765 clrf C5+1 | |
255 | 766 btfsc ir_s8_buffer+2,2 |
0 | 767 bsf C5+1,0 |
255 | 768 btfsc ir_s8_buffer+2,3 |
0 | 769 bsf C5+1,1 |
255 | 770 btfsc ir_s8_buffer+2,4 |
0 | 771 bsf C5+1,2 |
255 | 772 btfsc ir_s8_buffer+2,5 |
0 | 773 bsf C5+1,3 |
774 | |
604 | 775 ; calculate C5 = UT1 |
776 ; C5 = 8*C5 + 10000 (u16 range 10.000 .. +42.760) | |
0 | 777 clrf isr_xA+1 |
778 movlw d'8' | |
779 movwf isr_xA+0 | |
623 | 780 MOVII C5,isr_xB |
781 call isr_unsigned_mult16x16 ; isr_xA*isr_xB=isr_xC | |
782 MOVII isr_xC,C5 | |
604 | 783 movlw LOW d'10000' |
784 addwf C5+0,F | |
785 movlw HIGH d'10000' | |
623 | 786 addwfc C5+1,F ; = 8*C5 + 10000 |
0 | 787 |
623 | 788 ; calculate C6 (16Bit) |
0 | 789 clrf C6+1 |
255 | 790 movff ir_s8_buffer+6,C6+0 |
0 | 791 bcf C6+0,7 |
792 | |
643 | 793 get_calibration_data2: |
623 | 794 clrf sensor_state_counter ; reset state counter |
795 bcf block_sensor_interrupt ; re-enable sensor interrupts | |
796 banksel common ; back to bank common | |
797 return ; done | |
0 | 798 |
634 | 799 |
800 ;----------------------------------------------------------------------------- | |
801 ; Helper Function - Reset Chip | |
802 ; | |
0 | 803 reset_MS5541_one: |
634 | 804 bsf MS5541_mosi ; set MOSI |
623 | 805 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return |
0 | 806 |
807 reset_MS5541_zero: | |
634 | 808 bcf MS5541_mosi ; clear MOSI |
623 | 809 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return |
0 | 810 |
811 reset_MS5541: | |
604 | 812 rcall reset_MS5541_one ; 0 |
0 | 813 rcall reset_MS5541_zero |
814 rcall reset_MS5541_one | |
815 rcall reset_MS5541_zero | |
816 rcall reset_MS5541_one | |
817 rcall reset_MS5541_zero | |
818 rcall reset_MS5541_one | |
819 rcall reset_MS5541_zero | |
820 rcall reset_MS5541_one | |
821 rcall reset_MS5541_zero | |
822 rcall reset_MS5541_one | |
823 rcall reset_MS5541_zero | |
824 rcall reset_MS5541_one | |
825 rcall reset_MS5541_zero | |
826 rcall reset_MS5541_one | |
604 | 827 rcall reset_MS5541_zero ; 15 |
828 rcall reset_MS5541_zero | |
829 rcall reset_MS5541_zero | |
830 rcall reset_MS5541_zero | |
831 rcall reset_MS5541_zero | |
832 rcall reset_MS5541_zero ; 20 | |
0 | 833 return |
834 | |
623 | 835 |
634 | 836 ;----------------------------------------------------------------------------- |
837 ; Helper Function - read 2 Byte from Chip | |
838 ; | |
0 | 839 get_2bytes_MS5541: |
634 | 840 movlw d'8' ; load counter for 8 bit |
841 movwf clock_count ; ... | |
842 rcall recieve_loop ; receive 8 bits | |
843 movff dbuffer,dMSB ; store result | |
844 movlw d'8' ; load counter for 8 bit | |
845 movwf clock_count ; ... | |
846 rcall recieve_loop ; receive 8 bits | |
847 movff dbuffer,dLSB ; store result | |
623 | 848 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return |
0 | 849 |
634 | 850 |
851 ;----------------------------------------------------------------------------- | |
852 ; Helper Function - read 1 Bit from Chip | |
853 ; | |
0 | 854 recieve_loop: |
623 | 855 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk |
634 | 856 btfss MS5541_miso ; read bit = 1 ? (MSB first) |
857 bcf STATUS,C ; NO - clear carry bit | |
858 btfsc MS5541_miso ; read bit = 0 ? (MSB first) | |
859 bsf STATUS,C ; NO - set carry bit | |
860 rlcf dbuffer,F ; shift buffer | |
861 decfsz clock_count,F ; decrement counter, became zero? | |
862 bra recieve_loop ; NO - loop | |
863 return ; YES - done | |
0 | 864 |
634 | 865 |
866 ;----------------------------------------------------------------------------- | |
867 ; Helper Function - send a Clock Pulse | |
868 ; | |
0 | 869 send_clk_pulse: |
634 | 870 bsf MS5541_clk ; set clock |
0 | 871 nop |
872 nop | |
873 nop | |
874 nop | |
29
50c3e2c7ba7a
adding cardinal (and ordinal) directions for the compass
heinrichsweikamp
parents:
0
diff
changeset
|
875 nop |
50c3e2c7ba7a
adding cardinal (and ordinal) directions for the compass
heinrichsweikamp
parents:
0
diff
changeset
|
876 nop |
634 | 877 bcf MS5541_clk ; release clock |
0 | 878 nop |
879 nop | |
29
50c3e2c7ba7a
adding cardinal (and ordinal) directions for the compass
heinrichsweikamp
parents:
0
diff
changeset
|
880 nop |
50c3e2c7ba7a
adding cardinal (and ordinal) directions for the compass
heinrichsweikamp
parents:
0
diff
changeset
|
881 nop |
634 | 882 return ; done |
0 | 883 |
623 | 884 |
634 | 885 ;----------------------------------------------------------------------------- |
886 ; Helper Function - send a Command | |
887 ; | |
0 | 888 send_data_MS5541: |
623 | 889 movwf clock_count ; from WREG |
890 ; send three start bits first | |
634 | 891 bcf MS5541_clk ; revoke clock |
892 nop ; wait | |
893 nop ; wait | |
894 bsf MS5541_mosi ; set MOSI | |
895 movlw d'3' ; compute total bit counter | |
896 subwf clock_count,F ; ... | |
623 | 897 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk |
898 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk | |
899 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk | |
634 | 900 |
901 send_data_MS5541_loop: | |
902 ; now send 8 bits from dbuffer and fill-up with zeros | |
903 bcf MS5541_clk ; revoke clock | |
904 nop ; wait | |
905 nop ; wait | |
906 btfss dbuffer,7 ; bit = 1 ? (MSB first) | |
907 bcf MS5541_mosi ; NO - clear MOSI | |
908 btfsc dbuffer,7 ; bit = 0 ? (MSB first) | |
909 bsf MS5541_mosi ; NO - set MOSI | |
910 bsf MS5541_clk ; set clock | |
911 bcf STATUS,C ; clear carry bit | |
912 rlcf dbuffer,F ; shift data byte | |
913 nop ; wait | |
914 nop ; wait | |
0 | 915 ; nop |
916 ; nop | |
917 ; nop | |
918 ; nop | |
919 ; bcf MS5541_clk | |
634 | 920 decfsz clock_count,F ; decrement bit counter, became zero? |
921 bra send_data_MS5541_loop ; NO - loop | |
922 bcf MS5541_clk ; YES - revoke clock | |
923 return ; - done | |
0 | 924 |
634 | 925 ;----------------------------------------------------------------------------- |
926 | |
927 END |