comparison src/ms5541.asm @ 634:4050675965ea

3.10 stable release
author heinrichsweikamp
date Tue, 28 Apr 2020 17:34:31 +0200
parents 185ba2f91f59
children 7d8a4c60ec1a 5b7fe7777425
comparison
equal deleted inserted replaced
633:690c48db7b5b 634:4050675965ea
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File ms5541.asm combined next generation V3.8.6 3 ; File ms5541.asm * combined next generation V3.9.4f
4 ; 4 ;
5 ; Sensor subroutines 5 ; Sensor Subroutines
6 ; 6 ;
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. 7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
8 ;============================================================================= 8 ;=============================================================================
9 ; HISTORY 9 ; HISTORY
10 ; 2011-08-03 : [mH] moving from OSTC code 10 ; 2011-08-03 : [mH] moving from OSTC code
13 #include "math.inc" ; Math routines 13 #include "math.inc" ; Math routines
14 14
15 15
16 ms5541 CODE 16 ms5541 CODE
17 17
18 ;============================================================================= 18
19 ; Expose internal variables to ease debug 19 ; Expose internal variables to ease debug
20 global D1, D2 20 global D1, D2
21 global C1, C2, C3, C4, C5, C6 21 global C1, C2, C3, C4, C5, C6
22 global xdT, xdT2, OFF, SENS, pressure_abs_avg, temperature_avg 22 global xdT, xdT2, OFF, SENS, pressure_abs_avg, temperature_avg
23 23
24 ;============================================================================= 24
25 25 ;-----------------------------------------------------------------------------
26 global calculate_compensation ; called from ISR and from sleep mode, returns in bank isr_data 26 ; Convert Temperature & Pressure raw Values to compensated Values
27 ;
28 ; called from ISR and from sleep mode, returns in bank isr_data
29 ;
30 global calculate_compensation
27 calculate_compensation: 31 calculate_compensation:
28 banksel isr_backup ; select bank ISR data 32 banksel isr_backup ; select bank ISR data
29 33
30 ;---- pressure sensor compensation 34 ;---- pressure sensor compensation
31 35
49 MOVII isr_xA,isr_xB ; copy A to B 53 MOVII isr_xA,isr_xB ; copy A to B
50 call isr_signed_mult16x16 ; dT*dT --> xC (32 bits) 54 call isr_signed_mult16x16 ; dT*dT --> xC (32 bits)
51 55
52 ; dT >= 0: divide by 8, i.e. 3 shifts rights 56 ; dT >= 0: divide by 8, i.e. 3 shifts rights
53 ; dT < 0: divide by 2, i.e. 1 shifts rights 57 ; dT < 0: divide by 2, i.e. 1 shifts rights
54 movlw .3 58 movlw .3 ; 3 shifts by default
55 btfss xdT+1,7 ; was dT negative ? 59 btfss xdT+1,7 ; was dT negative ?
56 movlw .1 60 movlw .1 ; YES - replace by 1 shift
57 calc_loop_1: 61 calc_loop_1:
58 bcf STATUS,C ; dT^2 is positive, so injected zeros 62 bcf STATUS,C ; dT^2 is positive, so injecte zeros
59 rrcf isr_xC+1,F 63 rrcf isr_xC+1,F ; shift right high byte
60 rrcf isr_xC+0,F 64 rrcf isr_xC+0,F ; shift right low byte
61 decfsz WREG 65 decfsz WREG ; decrement loop counter, all shifts done?
62 bra calc_loop_1 66 bra calc_loop_1 ; NO - loop
63 67
64 movf isr_xC+0,W ; dT2 = dT - (dT/128)*(dT/128)/(2 ...or... 8) 68 movf isr_xC+0,W ; dT2 = dT - (dT/128)*(dT/128)/(2 ...or... 8)
65 subwf xdT+0,W 69 subwf xdT+0,W ; ...
66 movwf xdT2+0 70 movwf xdT2+0 ; ...
67 movf isr_xC+1,W 71 movf isr_xC+1,W ; ...
68 subwfb xdT+1,W 72 subwfb xdT+1,W ; ...
69 movwf xdT2+1 73 movwf xdT2+1 ; ...
70 74
71 ; calculate OFF = C2 + ((C4-250)*dT2)/2^12 + 10000 (range +9.246 .. +18.887) 75 ; calculate OFF = C2 + ((C4-250)*dT2)/2^12 + 10000 (range +9.246 .. +18.887)
72 MOVII C4, isr_xA ; C4 - 250 --> A 76 MOVII C4, isr_xA ; C4 - 250 --> A
73 MOVII xdT2,isr_xB ; dT2 --> B 77 MOVII xdT2,isr_xB ; dT2 --> B
74 call isr_signed_mult16x16 78 call isr_signed_mult16x16 ; C = A*B
75 79
76 movlw .12-.8 ; a 12 bit shift = 1 byte + 4 bits 80 movlw .12-.8 ; a 12 bit shift = 1 byte + 4 bits
77 call isr_shift_C31 81 call isr_shift_C31 ; special shift
78 82
79 movlw LOW(.10000) ; add 10000 83 movlw LOW(.10000) ; add 10000
80 addwf isr_xC+1,F 84 addwf isr_xC+1,F ; ...
81 movlw HIGH(.10000) 85 movlw HIGH(.10000) ; ...
82 addwfc isr_xC+2,F 86 addwfc isr_xC+2,F ; ...
83 87
84 movf C2+0,W ; add C2 and store result in OFF 88 movf C2+0,W ; add C2 and store result in OFF
85 addwf isr_xC+1,W 89 addwf isr_xC+1,W ; ...
86 movwf OFF+0 90 movwf OFF+0 ; ...
87 movf C2+1,W 91 movf C2+1,W ; ...
88 addwfc isr_xC+2,W 92 addwfc isr_xC+2,W ; ...
89 movwf OFF+1 93 movwf OFF+1 ; ...
90 94
91 ; calculate SENS = C1/2 + ((C3+200)*dT)/2^13 + 3000 95 ; calculate SENS = C1/2 + ((C3+200)*dT)/2^13 + 3000
92 movlw LOW(.200) ; C3+200 --> A 96 movlw LOW(.200) ; C3+200 --> A
93 addwf C3+0,W 97 addwf C3+0,W ; ...
94 movwf isr_xA+0 98 movwf isr_xA+0 ; ...
95 movlw HIGH(.200) 99 movlw HIGH(.200) ; ...
96 addwfc C3+1,W 100 addwfc C3+1,W ; ...
97 movwf isr_xA+1 101 movwf isr_xA+1 ; ...
98 ; B still contains dT2 102 ; B still contains dT2
99 call isr_signed_mult16x16 ; A*B --> C 103 call isr_signed_mult16x16 ; C = A*B
100 movlw .13-.8 ; A 13 bit shift = 1 byte + 5 bits 104 movlw .13-.8 ; A 13 bit shift = 1 byte + 5 bits
101 call isr_shift_C31 105 call isr_shift_C31 ; special shift
102 106
103 bcf STATUS,C ; SENS = C1 / 2 107 bcf STATUS,C ; SENS = C1 / 2
104 rrcf C1+1,W 108 rrcf C1+1,W ; ...
105 movwf SENS+1 109 movwf SENS+1 ; ...
106 rrcf C1+0,W 110 rrcf C1+0,W ; ...
107 movwf SENS+0 111 movwf SENS+0 ; ...
108 112
109 movlw LOW(.3000) ; add 3000 113 movlw LOW(.3000) ; add 3000
110 addwf isr_xC+1,F 114 addwf isr_xC+1,F ; ...
111 movlw HIGH(.3000) 115 movlw HIGH(.3000) ; ...
112 addwfc isr_xC+2,F 116 addwfc isr_xC+2,F ; ...
113 117
114 movf isr_xC+1,W ; and sum into SENS 118 movf isr_xC+1,W ; and sum into SENS
115 addwf SENS+0,F 119 addwf SENS+0,F ; ...
116 movf isr_xC+2,W 120 movf isr_xC+2,W ; ...
117 addwfc SENS+1,F 121 addwfc SENS+1,F ; ...
118 122
119 ; calculate absolute pressure = (sens * (d1-off))/2^12 + 1000 (for MS5541C) 123 ; calculate absolute pressure = (sens * (d1-off))/2^12 + 1000 (for MS5541C)
120 ; calculate absolute pressure = (sens * (d1-off))/2^11 + 1000 (for MS5541C-30) 124 ; calculate absolute pressure = (sens * (d1-off))/2^11 + 1000 (for MS5541C-30)
121 movf OFF+0,W ; d1-off --> a 125 movf OFF+0,W ; d1-off --> a
122 subwf D1+0,W 126 subwf D1+0,W ; ...
123 movwf isr_xA+0 127 movwf isr_xA+0 ; ...
124 movf OFF+1,W 128 movf OFF+1,W ; ...
125 subwfb D1+1,W 129 subwfb D1+1,W ; ...
126 movwf isr_xA+1 130 movwf isr_xA+1 ; ...
127 131
128 MOVII SENS,isr_xB ; sens --> b 132 MOVII SENS,isr_xB ; sens --> B
129 call isr_signed_mult16x16 133 call isr_signed_mult16x16 ; C = A*B
130 movlw .13 134 movlw .13 ; 12 * 256 = 3328
131 cpfslt C1+1 ; C1 > 3328 ? 135 cpfslt C1+1 ; C1 > 3328 ?
132 bra isr_shift_ms5541_30 ; YES - MS5541-30 136 bra isr_shift_ms5541_30 ; YES - MS5541-30
133 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits 137 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits
134 bra isr_shift_ms5541_common 138 bra isr_shift_ms5541_common ; - continue
135 isr_shift_ms5541_30: 139 isr_shift_ms5541_30:
136 movlw .11-.8 ; MS5541-30: 11 bit shift = 1 byte + 3 bits 140 movlw .11-.8 ; MS5541-30: 11 bit shift = 1 byte + 3 bits
137 isr_shift_ms5541_common: 141 isr_shift_ms5541_common:
138 call isr_shift_C31 142 call isr_shift_C31 ; special shift
139 movlw LOW .1000 ; add 1000 143 movlw LOW .1000 ; add 1000
140 addwf isr_xC+1,F 144 addwf isr_xC+1,F ; ...
141 movlw HIGH .1000 145 movlw HIGH .1000 ; ...
142 addwfc isr_xC+2,F 146 addwfc isr_xC+2,F ; ...
143 147
144 ; add opt_pressure_adjust to result (SIGNED!) 148 ; add opt_pressure_adjust to result (SIGNED!)
145 movff opt_pressure_adjust,isr_xC+0; get adjustment value (signed) 149 clrf isr_xC+0 ; prepare high byte for adjustment
146 movf isr_xC+0,F ; excite flags, opt_pressure_adjust = 0 ? 150 movff opt_pressure_adjust,WREG ; get low byte for adjustment (signed)
147 bz calc_compensation_1 ; YES - skip pressure adjustment 151 movf WREG,W ; excite flags
148 btfss isr_xC+0,7 ; NO - opt_pressure_adjust < 0 ? 152 bz calc_compensation_1 ; opt_pressure_adjust = 0 -> skip adjustment
149 bra pressure_extra_add ; NO - add offset 153 btfss STATUS,N ; opt_pressure_adjust < 0 ?
150 ;bra pressure_extra_sub ; YES - subtract offset 154 bra calc_compensation_adjust ; NO - positive
151 155 negf WREG ; YES - negate opt_pressure_adjust for limit check
152 pressure_extra_sub: 156 setf isr_xC+0 ; - adopt high byte for adjustment
153 comf isr_xC+0,F ; complement opt_pressure_adjust 157 calc_compensation_adjust:
154 incf isr_xC+0,F ; ... 158 addlw -.21 ; limit is 20 mbar, subtract 21
155 movlw .22 ; check for max. of 20 mbar 159 bnn calc_compensation_1 ; result not negative -> skip adjustment
156 cpfslt isr_xC+0 ; opt_pressure_adjust < 21 mbar ? 160 movff opt_pressure_adjust,WREG ; get opt_pressure_adjust, low byte
157 clrf isr_xC+0 ; NO - reset opt_pressure_adjust to zero
158 movf isr_xC+0,W ; get opt_pressure_adjust to WREG
159 subwf isr_xC+1,F ; pressure value -= opt_pressure_adjust, low byte
160 movlw .0 ; pressure value -= opt_pressure_adjust, high byte
161 subwfb isr_xC+2,F ; ...
162 bra calc_compensation_1 ; continue with checking for simulator mode
163
164 pressure_extra_add:
165 movlw .21 ; check for max. of 20 mbar
166 cpfslt isr_xC+0 ; opt_pressure_adjust < 21 mbar ?
167 clrf isr_xC+0 ; NO - reset opt_pressure_adjust to zero
168 movf isr_xC+0,W ; get opt_pressure_adjust to WREG
169 addwf isr_xC+1,F ; pressure value += opt_pressure_adjust, low byte 161 addwf isr_xC+1,F ; pressure value += opt_pressure_adjust, low byte
170 movlw .0 ; pressure value += opt_pressure_adjust, high byte 162 movf isr_xC+0,W ; get adjustment, high byte
171 addwfc isr_xC+2,F ; ... 163 addwfc isr_xC+2,F ; pressure value += opt_pressure_adjust, high byte
172 ;bra calc_compensation_1 ; continue with checking for simulator mode
173 164
174 calc_compensation_1: 165 calc_compensation_1:
175 bcf sensor_override_active ; clear sensor override active flag by default 166 bcf sensor_override_active ; clear sensor override active flag by default
176 btfss sensor_override_request ; sensor override requested? 167 btfss sensor_override_request ; sensor override requested?
177 bra calc_compensation_add_avg ; NO - keep taking absolute pressure from sensor 168 bra calc_compensation_add_avg ; NO - keep taking absolute pressure from sensor
268 259
269 ;---- temperature sensor compensation 260 ;---- temperature sensor compensation
270 261
271 ; calculate temp = 200 + dT*(C6+100)/2^11 262 ; calculate temp = 200 + dT*(C6+100)/2^11
272 movlw LOW(.100) ; C6 + 100 --> A 263 movlw LOW(.100) ; C6 + 100 --> A
273 addwf C6+0,W 264 addwf C6+0,W ; ...
274 movwf isr_xA+0 265 movwf isr_xA+0 ; ...
275 movlw HIGH(.100) 266 movlw HIGH(.100) ; ...
276 addwfc C6+1,W 267 addwfc C6+1,W ; ...
277 movwf isr_xA+1 268 movwf isr_xA+1 ; ...
278 269
279 MOVII xdT2,isr_xB ; dT2 --> B 270 MOVII xdT2,isr_xB ; dT2 --> B
280 call isr_signed_mult16x16 ; A*B 271 call isr_signed_mult16x16 ; A*B
281 movlw .11-.8 ; a 12 bit shift = 1 byte + 3 bits 272 movlw .11-.8 ; a 12 bit shift = 1 byte + 3 bits
282 call isr_shift_C31 273 call isr_shift_C31 ; special shift
283 274
284 movlw LOW(.200) ; add 200 275 movlw LOW(.200) ; add 200
285 addwf isr_xC+1,F 276 addwf isr_xC+1,F ; ...
286 movlw HIGH(.200) 277 movlw HIGH(.200) ; ...
287 addwfc isr_xC+2,F 278 addwfc isr_xC+2,F ; ...
288 279
289 ; add opt_temperature_adjust to result (SIGNED!) 280 ; add opt_temperature_adjust to result (SIGNED!)
290 movff opt_temperature_adjust,isr_xC+0 281 clrf isr_xC+0 ; prepare high byte for adjustment
291 282 movff opt_temperature_adjust,WREG ; get low byte for adjustment (signed)
292 btfss isr_xC+0,7 ; < 0 ? 283 movf WREG,W ; excite flags
293 bra temperature_extra_add ; NO 284 bz calc_temperature_add_avg ; opt_temperature_adjust = 0 -> skip adjustment
294 comf isr_xC+0,F ; YES 285 btfss STATUS,N ; opt_temperature_adjust < 0 ?
295 incf isr_xC+0,F 286 bra calc_temperature_adjust ; NO - positive
296 movlw .22 ; check for max. of 2.0°C 287 negf WREG ; YES - negate opt_temperature_adjust for limit check
297 cpfslt isr_xC+0 288 setf isr_xC+0 ; - adopt high byte for adjustment
298 clrf isr_xC+0 289 calc_temperature_adjust:
299 movf isr_xC+0,W ; subtract 290 addlw -.21 ; limit is 2.0°C, subtract 21 (scaling is 0.1°C)
300 subwf isr_xC+1,F 291 bnn calc_temperature_add_avg ; result not negative -> skip adjustment
301 movlw .0 292 movff opt_temperature_adjust,WREG ; get opt_temperature_adjust, low byte
302 subwfb isr_xC+2,F 293 addwf isr_xC+1,F ; temperature value += opt_temperature_adjust, low byte
303 bra temperature_extra_common 294 movf isr_xC+0,W ; get adjustment high byte
304 295 addwfc isr_xC+2,F ; temperature value += opt_temperature_adjust, high byte
305 temperature_extra_add: 296
306 movlw .21 ; check for max. of 2.0°C 297 calc_temperature_add_avg:
307 cpfslt isr_xC+0 298 movf isr_xC+1,W ; copy current temperature to WREG, low byte
308 clrf isr_xC+0 299 addwf temperature_avg+0,F ; temperature_avg += current temperature, low byte
309 movf isr_xC+0,W ; add 300 movf isr_xC+2,W ; copy current temperature to WREG, high byte
310 addwf isr_xC+1,F 301 addwfc temperature_avg+1,F ; temperature_avg += current temperature, high byte
311 movlw .0
312 addwfc isr_xC+2,F
313
314 temperature_extra_common:
315 movf isr_xC+1,W
316 addwf temperature_avg+0,F
317 movf isr_xC+2,W
318 addwfc temperature_avg+1,F
319
320 return ; done 302 return ; done
321 303
322 ;============================================================================= 304
323 305 ;-----------------------------------------------------------------------------
324 global get_pressure_start ; called from ISR and sleep mode, needs to be called bank isr_backup 306 ; Start Pressure Measurement
307 ;
308 ; called from ISR and sleep mode, needs to be called in bank isr_backup
309 ;
310 global get_pressure_start
325 get_pressure_start: 311 get_pressure_start:
326 rcall reset_MS5541 312 rcall reset_MS5541 ; reset the chip
327 movlw b'10100000' ; +3*high as start and 1+low as stop 313 movlw b'10100000' ; +3*high as start and 1+low as stop
328 movwf dbuffer 314 movwf dbuffer ; ....
329 movlw d'12' 315 movlw d'12' ; send start command
330 rcall send_data_MS5541 316 rcall send_data_MS5541 ; ...
331 return 317 return ; done
332 318
333 global get_pressure_value ; called from ISR and sleep mode, needs to be called bank isr_backup 319
320 ;-----------------------------------------------------------------------------
321 ; Read Pressure Measurement Result
322 ;
323 ; called from ISR and sleep mode, needs to be called in bank isr_backup
324 ;
325 global get_pressure_value
334 get_pressure_value: 326 get_pressure_value:
335 btfsc MS5541_miso ; conversion done? 327 btfsc MS5541_miso ; conversion done?
336 return ; NO - done 328 return ; NO - abort
337 rcall get_2bytes_MS5541 329 rcall get_2bytes_MS5541 ; YES - read result
338 movff dMSB,D1+1 330 movff dMSB,D1+1 ; copy result to D1, high byte first
339 movff dLSB,D1+0 331 movff dLSB,D1+0 ; ... low byte second
340 return 332 return ; done
341 333
342 ;============================================================================= 334
343 335 ;-----------------------------------------------------------------------------
344 global get_temperature_start ; called from ISR and sleep mode, needs to be called in bank isr_backup 336 ; Start Temperature Measurement
337 ;
338 ; called from ISR and sleep mode, needs to be called in bank isr_backup
339 ;
340 global get_temperature_start
345 get_temperature_start: 341 get_temperature_start:
346 rcall reset_MS5541 342 rcall reset_MS5541 ; reset chip
347 movlw b'10010000' ; +3*high as start and 1+low as stop 343 movlw b'10010000' ; +3*high as start and 1+low as stop
348 movwf dbuffer 344 movwf dbuffer ; ...
349 movlw d'12' 345 movlw d'12' ; send start command
350 rcall send_data_MS5541 346 rcall send_data_MS5541 ; ...
351 return 347 return ; done
352 348
353 349
354 global get_temperature_value ; called from ISR and sleep mode, needs to be called in bank isr_backup 350 ;-----------------------------------------------------------------------------
351 ; Read Pressure Measurement Result
352 ;
353 ; called from ISR and sleep mode, needs to be called in bank isr_backup
354 ;
355 global get_temperature_value
355 get_temperature_value: 356 get_temperature_value:
356 btfsc MS5541_miso ; conversion done? 357 btfsc MS5541_miso ; conversion done?
357 return ; NO - done 358 return ; NO - done
358 rcall get_2bytes_MS5541 359 rcall get_2bytes_MS5541 ; YES - read result
359 movff dMSB,D2+1 360 movff dMSB,D2+1 ; copy result to D2, high byte first
360 movff dLSB,D2+0 361 movff dLSB,D2+0 ; ... low byte second
361 return 362 return ; done
362 363
363 ;============================================================================= 364
364 365 ;-----------------------------------------------------------------------------
365 global get_calibration_data ; called by start, returns in bank common 366 ; Retrieve Calibration Data
367 ;
368 ; called by start, returns in bank common
369 ;
370 global get_calibration_data
366 get_calibration_data: 371 get_calibration_data:
367 banksel isr_backup ; select bank ISR data 372 banksel isr_backup ; select bank ISR data
368 bsf block_sensor_interrupt ; disable sensor interrupts 373 bsf block_sensor_interrupt ; disable sensor interrupts
369 374
370 rcall reset_MS5541 375 rcall reset_MS5541 ; reset chip
376
371 movlw b'01010100' ; +3*high as start and 1+low as stop 377 movlw b'01010100' ; +3*high as start and 1+low as stop
372 movwf dbuffer 378 movwf dbuffer ; ...
373 movlw d'13' 379 movlw d'13' ; send command
374 rcall send_data_MS5541 380 rcall send_data_MS5541 ; ...
375 rcall get_2bytes_MS5541 381 rcall get_2bytes_MS5541 ; read result
376 movff dMSB,ir_s8_buffer+1 382 movff dMSB,ir_s8_buffer+1 ; copy result, high byte first
377 movff dLSB,ir_s8_buffer+0 383 movff dLSB,ir_s8_buffer+0 ; copy result, low byte second
378 384
379 movlw b'01011000' ; +3*high as start and 1+low as stop 385 movlw b'01011000' ; +3*high as start and 1+low as stop
380 movwf dbuffer 386 movwf dbuffer ; ...
381 movlw d'13' 387 movlw d'13' ; send command
382 rcall send_data_MS5541 388 rcall send_data_MS5541 ; ...
383 rcall get_2bytes_MS5541 389 rcall get_2bytes_MS5541 ; read result
384 movff dMSB,ir_s8_buffer+3 390 movff dMSB,ir_s8_buffer+3 ; copy result, high byte first
385 movff dLSB,ir_s8_buffer+2 391 movff dLSB,ir_s8_buffer+2 ; copy result, low byte second
386 392
387 movlw b'01100100' ; +3*high as start and 1+low as stop 393 movlw b'01100100' ; +3*high as start and 1+low as stop
388 movwf dbuffer 394 movwf dbuffer ; ...
389 movlw d'13' 395 movlw d'13' ; send command
390 rcall send_data_MS5541 396 rcall send_data_MS5541 ; ...
391 rcall get_2bytes_MS5541 397 rcall get_2bytes_MS5541 ; read result
392 movff dMSB,ir_s8_buffer+5 398 movff dMSB,ir_s8_buffer+5 ; copy result, high byte first
393 movff dLSB,ir_s8_buffer+4 399 movff dLSB,ir_s8_buffer+4 ; copy result, low byte second
394 400
395 movlw b'01101000' ; +3*high as start and 1+low as stop 401 movlw b'01101000' ; +3*high as start and 1+low as stop
396 movwf dbuffer 402 movwf dbuffer ; ...
397 movlw d'13' 403 movlw d'13' ; send command
398 rcall send_data_MS5541 404 rcall send_data_MS5541 ; ...
399 rcall get_2bytes_MS5541 405 rcall get_2bytes_MS5541 ; read result
400 movff dMSB,ir_s8_buffer+7 406 movff dMSB,ir_s8_buffer+7 ; copy result, high byte first
401 movff dLSB,ir_s8_buffer+6 407 movff dLSB,ir_s8_buffer+6 ; copy result, low byte second
402 408
403 ; calculate C1 (16Bit) 409 ; calculate C1 (16 Bit)
404 movff ir_s8_buffer+1, C1+1 410 movff ir_s8_buffer+1,C1+1
405 bcf STATUS,C 411 bcf STATUS,C
406 rrcf C1+1 412 rrcf C1+1
407 bcf STATUS,C 413 bcf STATUS,C
408 rrcf C1+1 414 rrcf C1+1
409 bcf STATUS,C 415 bcf STATUS,C
410 rrcf C1+1 416 rrcf C1+1
411 movff ir_s8_buffer+0, C1+0 417 movff ir_s8_buffer+0,C1+0
412 bsf STATUS,C 418 bsf STATUS,C
413 btfss ir_s8_buffer+1,0 419 btfss ir_s8_buffer+1,0
414 bcf STATUS,C 420 bcf STATUS,C
415 rrcf C1+0 421 rrcf C1+0
416 bsf STATUS,C 422 bsf STATUS,C
420 bsf STATUS,C 426 bsf STATUS,C
421 btfss ir_s8_buffer+1,2 427 btfss ir_s8_buffer+1,2
422 bcf STATUS,C 428 bcf STATUS,C
423 rrcf C1+0 429 rrcf C1+0
424 430
425 ; calculate C2 (16Bit) 431 ; calculate C2 (16 Bit)
426 movff ir_s8_buffer+2, C2+0 432 movff ir_s8_buffer+2, C2+0
427 bsf STATUS,C 433 bsf STATUS,C
428 btfss ir_s8_buffer+3,0 434 btfss ir_s8_buffer+3,0
429 bcf STATUS,C 435 bcf STATUS,C
430 rrcf C2+0 436 rrcf C2+0
467 bcf STATUS,C 473 bcf STATUS,C
468 rrcf C2+1 474 rrcf C2+1
469 bcf STATUS,C 475 bcf STATUS,C
470 rrcf C2+1 476 rrcf C2+1
471 477
472 ; calculate C3 (16Bit) 478 ; calculate C3 (16 Bit)
473 movff ir_s8_buffer+5,C3+0 479 movff ir_s8_buffer+5,C3+0
474 bsf STATUS,C 480 bsf STATUS,C
475 btfss ir_s8_buffer+4,7 481 btfss ir_s8_buffer+4,7
476 bcf STATUS,C 482 bcf STATUS,C
477 rlcf C3+0 483 rlcf C3+0
483 btfsc ir_s8_buffer+5,7 489 btfsc ir_s8_buffer+5,7
484 bsf C3+1,1 490 bsf C3+1,1
485 btfsc ir_s8_buffer+5,6 491 btfsc ir_s8_buffer+5,6
486 bsf C3+1,0 492 bsf C3+1,0
487 493
488 ; calculate C4 (16Bit) 494 ; calculate C4 (16 Bit)
489 movff ir_s8_buffer+7,C4+0 495 movff ir_s8_buffer+7,C4+0
490 bsf STATUS,C 496 bsf STATUS,C
491 btfss ir_s8_buffer+6,7 497 btfss ir_s8_buffer+6,7
492 bcf STATUS,C 498 bcf STATUS,C
493 rlcf C4+0 499 rlcf C4+0
501 movwf C4+0 507 movwf C4+0
502 movlw -1 ; HIGH(- .250) is not understood... 508 movlw -1 ; HIGH(- .250) is not understood...
503 addwfc C4+1,W 509 addwfc C4+1,W
504 movwf C4+1 510 movwf C4+1
505 511
506 ; calculate C5 (16Bit) 512 ; calculate C5 (16 Bit)
507 movff ir_s8_buffer+4,C5+0 513 movff ir_s8_buffer+4,C5+0
508 bcf C5+0,6 514 bcf C5+0,6
509 btfsc ir_s8_buffer+2,0 515 btfsc ir_s8_buffer+2,0
510 bsf C5+0,6 516 bsf C5+0,6
511 bcf C5+0,7 517 bcf C5+0,7
542 clrf sensor_state_counter ; reset state counter 548 clrf sensor_state_counter ; reset state counter
543 bcf block_sensor_interrupt ; re-enable sensor interrupts 549 bcf block_sensor_interrupt ; re-enable sensor interrupts
544 banksel common ; back to bank common 550 banksel common ; back to bank common
545 return ; done 551 return ; done
546 552
547 ;============================================================================= 553
554 ;-----------------------------------------------------------------------------
555 ; Helper Function - Reset Chip
556 ;
548 reset_MS5541_one: 557 reset_MS5541_one:
549 bsf MS5541_mosi 558 bsf MS5541_mosi ; set MOSI
550 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return 559 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return
551 560
552 reset_MS5541_zero: 561 reset_MS5541_zero:
553 bcf MS5541_mosi 562 bcf MS5541_mosi ; clear MOSI
554 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return 563 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return
555 564
556 reset_MS5541: 565 reset_MS5541:
557 rcall reset_MS5541_one ; 0 566 rcall reset_MS5541_one ; 0
558 rcall reset_MS5541_zero 567 rcall reset_MS5541_zero
576 rcall reset_MS5541_zero 585 rcall reset_MS5541_zero
577 rcall reset_MS5541_zero ; 20 586 rcall reset_MS5541_zero ; 20
578 return 587 return
579 588
580 589
590 ;-----------------------------------------------------------------------------
591 ; Helper Function - read 2 Byte from Chip
592 ;
581 get_2bytes_MS5541: 593 get_2bytes_MS5541:
582 movlw d'8' 594 movlw d'8' ; load counter for 8 bit
583 movwf clock_count 595 movwf clock_count ; ...
584 rcall recieve_loop 596 rcall recieve_loop ; receive 8 bits
585 movff dbuffer,dMSB 597 movff dbuffer,dMSB ; store result
586 movlw d'8' 598 movlw d'8' ; load counter for 8 bit
587 movwf clock_count 599 movwf clock_count ; ...
588 rcall recieve_loop 600 rcall recieve_loop ; receive 8 bits
589 movff dbuffer,dLSB 601 movff dbuffer,dLSB ; store result
590 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return 602 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return
591 603
604
605 ;-----------------------------------------------------------------------------
606 ; Helper Function - read 1 Bit from Chip
607 ;
592 recieve_loop: 608 recieve_loop:
593 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk 609 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
594 btfss MS5541_miso ; MSB first 610 btfss MS5541_miso ; read bit = 1 ? (MSB first)
595 bcf STATUS,C 611 bcf STATUS,C ; NO - clear carry bit
596 btfsc MS5541_miso ; MSB first 612 btfsc MS5541_miso ; read bit = 0 ? (MSB first)
597 bsf STATUS,C 613 bsf STATUS,C ; NO - set carry bit
598 rlcf dbuffer,F 614 rlcf dbuffer,F ; shift buffer
599 decfsz clock_count,F 615 decfsz clock_count,F ; decrement counter, became zero?
600 bra recieve_loop 616 bra recieve_loop ; NO - loop
601 return 617 return ; YES - done
602 618
619
620 ;-----------------------------------------------------------------------------
621 ; Helper Function - send a Clock Pulse
622 ;
603 send_clk_pulse: 623 send_clk_pulse:
604 bsf MS5541_clk 624 bsf MS5541_clk ; set clock
605 nop 625 nop
606 nop 626 nop
607 nop 627 nop
608 nop 628 nop
609 nop 629 nop
610 nop 630 nop
611 bcf MS5541_clk 631 bcf MS5541_clk ; release clock
612 nop 632 nop
613 nop 633 nop
614 nop 634 nop
615 nop 635 nop
616 return 636 return ; done
617 637
618 638
639 ;-----------------------------------------------------------------------------
640 ; Helper Function - send a Command
641 ;
619 send_data_MS5541: 642 send_data_MS5541:
620 movwf clock_count ; from WREG 643 movwf clock_count ; from WREG
621 ; send three start bits first 644 ; send three start bits first
622 bcf MS5541_clk 645 bcf MS5541_clk ; revoke clock
623 nop 646 nop ; wait
624 nop 647 nop ; wait
625 bsf MS5541_mosi 648 bsf MS5541_mosi ; set MOSI
626 movlw d'3' 649 movlw d'3' ; compute total bit counter
627 subwf clock_count,F ; total bit counter 650 subwf clock_count,F ; ...
628 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk 651 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
629 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk 652 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
630 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk 653 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
631 send_data_MS5541_loop: ; now send 8 bits from dbuffer and fill-up with zeros 654
632 bcf MS5541_clk 655 send_data_MS5541_loop:
633 nop 656 ; now send 8 bits from dbuffer and fill-up with zeros
634 nop 657 bcf MS5541_clk ; revoke clock
635 btfss dbuffer,7 ; MSB first 658 nop ; wait
636 bcf MS5541_mosi 659 nop ; wait
637 btfsc dbuffer,7 ; MSB first 660 btfss dbuffer,7 ; bit = 1 ? (MSB first)
638 bsf MS5541_mosi 661 bcf MS5541_mosi ; NO - clear MOSI
639 bsf MS5541_clk 662 btfsc dbuffer,7 ; bit = 0 ? (MSB first)
640 bcf STATUS,C 663 bsf MS5541_mosi ; NO - set MOSI
641 rlcf dbuffer,F 664 bsf MS5541_clk ; set clock
642 nop 665 bcf STATUS,C ; clear carry bit
643 nop 666 rlcf dbuffer,F ; shift data byte
667 nop ; wait
668 nop ; wait
644 ; nop 669 ; nop
645 ; nop 670 ; nop
646 ; nop 671 ; nop
647 ; nop 672 ; nop
648 ; bcf MS5541_clk 673 ; bcf MS5541_clk
649 decfsz clock_count,F 674 decfsz clock_count,F ; decrement bit counter, became zero?
650 bra send_data_MS5541_loop 675 bra send_data_MS5541_loop ; NO - loop
651 bcf MS5541_clk 676 bcf MS5541_clk ; YES - revoke clock
652 return 677 return ; - done
678
679 ;-----------------------------------------------------------------------------
653 680
654 END 681 END