comparison src/ms5541.asm @ 643:7d8a4c60ec1a

3.15 release
author heinrichsweikamp
date Mon, 24 May 2021 18:40:53 +0200
parents 4050675965ea
children 070528a88715 357341239438
comparison
equal deleted inserted replaced
642:a9a0188091e4 643:7d8a4c60ec1a
9 ; HISTORY 9 ; HISTORY
10 ; 2011-08-03 : [mH] moving from OSTC code 10 ; 2011-08-03 : [mH] moving from OSTC code
11 11
12 #include "hwos.inc" ; Mandatory header 12 #include "hwos.inc" ; Mandatory header
13 #include "math.inc" ; Math routines 13 #include "math.inc" ; Math routines
14 14 #include "i2c.inc"
15 #include "shared_definitions.h" ; mailbox from/to p2_deco.c
15 16
16 ms5541 CODE 17 ms5541 CODE
17 18
18 19
19 ; Expose internal variables to ease debug 20 ; Expose internal variables to ease debug
28 ; called from ISR and from sleep mode, returns in bank isr_data 29 ; called from ISR and from sleep mode, returns in bank isr_data
29 ; 30 ;
30 global calculate_compensation 31 global calculate_compensation
31 calculate_compensation: 32 calculate_compensation:
32 banksel isr_backup ; select bank ISR data 33 banksel isr_backup ; select bank ISR data
34
35 btfsc press_sensor_type ; New sensor found?
36 bra press_comp_ms5837 ; Yes, use MS5837
33 37
34 ;---- pressure sensor compensation 38 ;---- pressure sensor compensation
35 39
36 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350) 40 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350)
37 movf C5+0,W ; get value to be subtracted 41 movf C5+0,W ; get value to be subtracted
129 subwfb D1+1,W ; ... 133 subwfb D1+1,W ; ...
130 movwf isr_xA+1 ; ... 134 movwf isr_xA+1 ; ...
131 135
132 MOVII SENS,isr_xB ; sens --> B 136 MOVII SENS,isr_xB ; sens --> B
133 call isr_signed_mult16x16 ; C = A*B 137 call isr_signed_mult16x16 ; C = A*B
134 movlw .13 ; 12 * 256 = 3328 138 movlw .15 ; 15 * 256 = 3840
135 cpfslt C1+1 ; C1 > 3328 ? 139 cpfslt C1+1 ; C1 > 3328 ?
136 bra isr_shift_ms5541_30 ; YES - MS5541-30 140 bra isr_shift_ms5541_30 ; YES - MS5541-30
137 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits 141 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits
138 bra isr_shift_ms5541_common ; - continue 142 bra isr_shift_ms5541_common ; - continue
139 isr_shift_ms5541_30: 143 isr_shift_ms5541_30:
142 call isr_shift_C31 ; special shift 146 call isr_shift_C31 ; special shift
143 movlw LOW .1000 ; add 1000 147 movlw LOW .1000 ; add 1000
144 addwf isr_xC+1,F ; ... 148 addwf isr_xC+1,F ; ...
145 movlw HIGH .1000 ; ... 149 movlw HIGH .1000 ; ...
146 addwfc isr_xC+2,F ; ... 150 addwfc isr_xC+2,F ; ...
147 151 bra press_comp_done_common
152
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
148 ; add opt_pressure_adjust to result (SIGNED!) 295 ; add opt_pressure_adjust to result (SIGNED!)
149 clrf isr_xC+0 ; prepare high byte for adjustment 296 clrf isr_xC+0 ; prepare high byte for adjustment
150 movff opt_pressure_adjust,WREG ; get low byte for adjustment (signed) 297 movff opt_pressure_adjust,WREG ; get low byte for adjustment (signed)
151 movf WREG,W ; excite flags 298 movf WREG,W ; excite flags
152 bz calc_compensation_1 ; opt_pressure_adjust = 0 -> skip adjustment 299 bz calc_compensation_1 ; opt_pressure_adjust = 0 -> skip adjustment
254 ; add current absolute pressure to averaging buffer 401 ; add current absolute pressure to averaging buffer
255 movf isr_xC+1,W ; copy current absolute pressure to WREG, low byte 402 movf isr_xC+1,W ; copy current absolute pressure to WREG, low byte
256 addwf pressure_abs_avg+0,F ; pressure_abs_avg += current pressure, low byte 403 addwf pressure_abs_avg+0,F ; pressure_abs_avg += current pressure, low byte
257 movf isr_xC+2,W ; copy current absolute pressure to WREG, high byte 404 movf isr_xC+2,W ; copy current absolute pressure to WREG, high byte
258 addwfc pressure_abs_avg+1,F ; pressure_abs_avg += current pressure, high byte 405 addwfc pressure_abs_avg+1,F ; pressure_abs_avg += current pressure, high byte
406 movlw .0
407 addwfc pressure_abs_avg+2,F ; pressure_abs_avg += current pressure, upper byte
259 408
260 ;---- temperature sensor compensation 409 ;---- temperature sensor compensation
261 410
411 btfsc press_sensor_type ; New sensor found?
412 bra temp_comp_ms5837 ; Yes, use MS5837
413
262 ; calculate temp = 200 + dT*(C6+100)/2^11 414 ; calculate temp = 200 + dT*(C6+100)/2^11
263 movlw LOW(.100) ; C6 + 100 --> A 415 movlw LOW(.100) ; C6 + 100 --> A
264 addwf C6+0,W ; ... 416 addwf C6+0,W ; ...
265 movwf isr_xA+0 ; ... 417 movwf isr_xA+0 ; ...
266 movlw HIGH(.100) ; ... 418 movlw HIGH(.100) ; ...
274 426
275 movlw LOW(.200) ; add 200 427 movlw LOW(.200) ; add 200
276 addwf isr_xC+1,F ; ... 428 addwf isr_xC+1,F ; ...
277 movlw HIGH(.200) ; ... 429 movlw HIGH(.200) ; ...
278 addwfc isr_xC+2,F ; ... 430 addwfc isr_xC+2,F ; ...
279 431 bra temp_comp_done_common
432
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
280 ; add opt_temperature_adjust to result (SIGNED!) 496 ; add opt_temperature_adjust to result (SIGNED!)
281 clrf isr_xC+0 ; prepare high byte for adjustment 497 clrf isr_xC+0 ; prepare high byte for adjustment
282 movff opt_temperature_adjust,WREG ; get low byte for adjustment (signed) 498 movff opt_temperature_adjust,WREG ; get low byte for adjustment (signed)
283 movf WREG,W ; excite flags 499 movf WREG,W ; excite flags
284 bz calc_temperature_add_avg ; opt_temperature_adjust = 0 -> skip adjustment 500 bz calc_temperature_add_avg ; opt_temperature_adjust = 0 -> skip adjustment
307 ; 523 ;
308 ; called from ISR and sleep mode, needs to be called in bank isr_backup 524 ; called from ISR and sleep mode, needs to be called in bank isr_backup
309 ; 525 ;
310 global get_pressure_start 526 global get_pressure_start
311 get_pressure_start: 527 get_pressure_start:
528 btfsc press_sensor_type ; New sensor found?
529 return ; Yes, ignore routine
312 rcall reset_MS5541 ; reset the chip 530 rcall reset_MS5541 ; reset the chip
313 movlw b'10100000' ; +3*high as start and 1+low as stop 531 movlw b'10100000' ; +3*high as start and 1+low as stop
314 movwf dbuffer ; .... 532 movwf dbuffer ; ....
315 movlw d'12' ; send start command 533 movlw d'12' ; send start command
316 rcall send_data_MS5541 ; ... 534 bra send_data_MS5541 ; ... (And return)
317 return ; done
318 535
319 536
320 ;----------------------------------------------------------------------------- 537 ;-----------------------------------------------------------------------------
321 ; Read Pressure Measurement Result 538 ; Read Pressure Measurement Result
322 ; 539 ;
323 ; called from ISR and sleep mode, needs to be called in bank isr_backup 540 ; called from ISR and sleep mode, needs to be called in bank isr_backup
324 ; 541 ;
325 global get_pressure_value 542 global get_pressure_value
326 get_pressure_value: 543 get_pressure_value:
544 btfsc press_sensor_type ; New sensor found?
545 bra get_pressure_value2
327 btfsc MS5541_miso ; conversion done? 546 btfsc MS5541_miso ; conversion done?
328 return ; NO - abort 547 return ; NO - abort
329 rcall get_2bytes_MS5541 ; YES - read result 548 rcall get_2bytes_MS5541 ; YES - read result
330 movff dMSB,D1+1 ; copy result to D1, high byte first 549 movff dMSB,D1+1 ; copy result to D1, high byte first
331 movff dLSB,D1+0 ; ... low byte second 550 movff dLSB,D1+0 ; ... low byte second
332 return ; done 551 return ; done
333 552
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
334 561
335 ;----------------------------------------------------------------------------- 562 ;-----------------------------------------------------------------------------
336 ; Start Temperature Measurement 563 ; Start Temperature Measurement
337 ; 564 ;
338 ; called from ISR and sleep mode, needs to be called in bank isr_backup 565 ; called from ISR and sleep mode, needs to be called in bank isr_backup
339 ; 566 ;
340 global get_temperature_start 567 global get_temperature_start
341 get_temperature_start: 568 get_temperature_start:
569 btfsc press_sensor_type ; New sensor found?
570 return ; Yes, ignore routine
342 rcall reset_MS5541 ; reset chip 571 rcall reset_MS5541 ; reset chip
343 movlw b'10010000' ; +3*high as start and 1+low as stop 572 movlw b'10010000' ; +3*high as start and 1+low as stop
344 movwf dbuffer ; ... 573 movwf dbuffer ; ...
345 movlw d'12' ; send start command 574 movlw d'12' ; send start command
346 rcall send_data_MS5541 ; ... 575 bra send_data_MS5541 ; ... (And return)
347 return ; done
348 576
349 577
350 ;----------------------------------------------------------------------------- 578 ;-----------------------------------------------------------------------------
351 ; Read Pressure Measurement Result 579 ; Read Pressure Measurement Result
352 ; 580 ;
353 ; called from ISR and sleep mode, needs to be called in bank isr_backup 581 ; called from ISR and sleep mode, needs to be called in bank isr_backup
354 ; 582 ;
355 global get_temperature_value 583 global get_temperature_value
356 get_temperature_value: 584 get_temperature_value:
585 btfsc press_sensor_type ; New sensor found?
586 bra get_temperature_value2
357 btfsc MS5541_miso ; conversion done? 587 btfsc MS5541_miso ; conversion done?
358 return ; NO - done 588 return ; NO - done
359 rcall get_2bytes_MS5541 ; YES - read result 589 rcall get_2bytes_MS5541 ; YES - read result
360 movff dMSB,D2+1 ; copy result to D2, high byte first 590 movff dMSB,D2+1 ; copy result to D2, high byte first
361 movff dLSB,D2+0 ; ... low byte second 591 movff dLSB,D2+0 ; ... low byte second
362 return ; done 592 return ; done
363 593
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
364 602
365 ;----------------------------------------------------------------------------- 603 ;-----------------------------------------------------------------------------
366 ; Retrieve Calibration Data 604 ; Retrieve Calibration Data
367 ; 605 ;
368 ; called by start, returns in bank common 606 ; called by start, returns in bank common
369 ; 607 ;
370 global get_calibration_data 608 global get_calibration_data
371 get_calibration_data: 609 get_calibration_data:
372 banksel isr_backup ; select bank ISR data 610 banksel isr_backup ; select bank ISR data
373 bsf block_sensor_interrupt ; disable sensor interrupts 611 bsf block_sensor_interrupt ; disable sensor interrupts
374 612
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
375 rcall reset_MS5541 ; reset chip 620 rcall reset_MS5541 ; reset chip
376 621
377 movlw b'01010100' ; +3*high as start and 1+low as stop 622 movlw b'01010100' ; +3*high as start and 1+low as stop
378 movwf dbuffer ; ... 623 movwf dbuffer ; ...
379 movlw d'13' ; send command 624 movlw d'13' ; send command
543 ; calculate C6 (16Bit) 788 ; calculate C6 (16Bit)
544 clrf C6+1 789 clrf C6+1
545 movff ir_s8_buffer+6,C6+0 790 movff ir_s8_buffer+6,C6+0
546 bcf C6+0,7 791 bcf C6+0,7
547 792
793 get_calibration_data2:
548 clrf sensor_state_counter ; reset state counter 794 clrf sensor_state_counter ; reset state counter
549 bcf block_sensor_interrupt ; re-enable sensor interrupts 795 bcf block_sensor_interrupt ; re-enable sensor interrupts
550 banksel common ; back to bank common 796 banksel common ; back to bank common
551 return ; done 797 return ; done
552 798