comparison src/ms5541.asm @ 623:c40025d8e750

3.03 beta released
author heinrichsweikamp
date Mon, 03 Jun 2019 14:01:48 +0200
parents ca4556fb60b9
children 4cd81bdbf15c
comparison
equal deleted inserted replaced
622:02d1386429a6 623:c40025d8e750
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File ms5541.asm V2.98c 3 ; File ms5541.asm combined next generation V3.0.3b
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
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
15 ms5541 CODE 15
16 16 ms5541 CODE
17 ;============================================================================= 17
18 ; Expose internal variables, to ease debug: 18 ;=============================================================================
19 ; Expose internal variables to ease debug
19 global D1, D2 20 global D1, D2
20 global C1, C2, C3, C4, C5, C6 21 global C1, C2, C3, C4, C5, C6
21 global xdT, xdT2, OFF, SENS, amb_pressure_avg, temperature_avg 22 global xdT, xdT2, OFF, SENS, pressure_abs_avg, temperature_avg
22 23
23 ;============================================================================= 24 ;=============================================================================
24 global calculate_compensation 25
26 global calculate_compensation ; called from ISR and from sleep mode, returns in bank isr_data
25 calculate_compensation: 27 calculate_compensation:
26 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350) 28 banksel isr_backup ; select bank ISR data
27 movf C5+0,W ; Get Value to be subtracted 29
28 subwf D2+0,W ; Do the Low Byte 30 ;---- pressure sensor compensation
31
32 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350)
33 movf C5+0,W ; get value to be subtracted
34 subwf D2+0,W ; do the low byte
29 movwf xdT+0 35 movwf xdT+0
30 movf C5+1,W ; Then the high byte. 36 movf C5+1,W ; do the high byte
31 subwfb D2+1,W 37 subwfb D2+1,W
32 movwf xdT+1 38 movwf xdT+1
33 39
34 ; Second order temperature calculation 40 ; second order temperature calculation
35 ; xdT/128 is in range -89..+96, hence signed 8bit. dT/128 = (2*dT)/256 41
36 rlcf xdT+0,W ; put hit bit in carry. 42 ; xdT/128 is in range -89..+96, hence signed 8 bit. dT/128 = (2*dT)/256
37 rlcf xdT+1,W ; inject in high byte. 43 rlcf xdT+0,W ; put hit bit in carry
38 movwf isr_xA+0 ; and put result in low byte. 44 rlcf xdT+1,W ; inject in high byte
45 movwf isr_xA+0 ; and put result in low byte
39 clrf isr_xA+1 46 clrf isr_xA+1
40 btfsc xdT+1,7 ; If dT < 0 47 btfsc xdT+1,7 ; dT < 0 ?
41 setf isr_xA+1 ; then signextend to -1 48 setf isr_xA+1 ; YES - sign extend to -1
42 movff isr_xA+0,isr_xB+0 ; copy A to B 49 MOVII isr_xA,isr_xB ; copy A to B
43 movff isr_xA+1,isr_xB+1
44 call isr_signed_mult16x16 ; dT*dT --> xC (32 bits) 50 call isr_signed_mult16x16 ; dT*dT --> xC (32 bits)
45 51
46 ; dT >= 0: divide by 8, ie. 3 shifts rights. 52 ; dT >= 0: divide by 8, i.e. 3 shifts rights
47 ; dT < 0: divide by 2, ie. 1 shifts rights. 53 ; dT < 0: divide by 2, i.e. 1 shifts rights
48 movlw .3 54 movlw .3
49 btfss xdT+1,7 ; Was dT negative ? 55 btfss xdT+1,7 ; was dT negative ?
50 movlw .1 56 movlw .1
51 calc_loop_1: 57 calc_loop_1:
52 bcf STATUS,C ; dT^2 is positive, so injected zeros. 58 bcf STATUS,C ; dT^2 is positive, so injected zeros
53 rrcf isr_xC+1,F 59 rrcf isr_xC+1,F
54 rrcf isr_xC+0,F 60 rrcf isr_xC+0,F
55 decfsz WREG 61 decfsz WREG
56 bra calc_loop_1 62 bra calc_loop_1
57 63
60 movwf xdT2+0 66 movwf xdT2+0
61 movf isr_xC+1,W 67 movf isr_xC+1,W
62 subwfb xdT+1,W 68 subwfb xdT+1,W
63 movwf xdT2+1 69 movwf xdT2+1
64 70
65 ; Calculate OFF = C2 + ((C4-250)*dT2)/2^12 + 10000 71 ; calculate OFF = C2 + ((C4-250)*dT2)/2^12 + 10000 (range +9.246 .. +18.887)
66 ; (range +9.246 .. +18.887) 72 MOVII C4, isr_xA ; C4 - 250 --> A
67 movff C4+0,isr_xA+0 ; C4 - 250 --> A 73 MOVII xdT2,isr_xB ; dT2 --> B
68 movff C4+1,isr_xA+1
69 movff xdT2+0,isr_xB+0 ; dT2 --> B
70 movff xdT2+1,isr_xB+1
71 call isr_signed_mult16x16 74 call isr_signed_mult16x16
72 75
73 movlw .12-.8 ; A 12bit shift = 1 byte + 4 bits. 76 movlw .12-.8 ; a 12 bit shift = 1 byte + 4 bits
74 call isr_shift_C31 77 call isr_shift_C31
75 78
76 movlw LOW(.10000) ; Add 10000 79 movlw LOW(.10000) ; add 10000
77 addwf isr_xC+1,F 80 addwf isr_xC+1,F
78 movlw HIGH(.10000) 81 movlw HIGH(.10000)
79 addwfc isr_xC+2,F 82 addwfc isr_xC+2,F
80 83
81 movf C2+0,W ; Add C2, and save into OFF 84 movf C2+0,W ; add C2 and store result in OFF
82 addwf isr_xC+1,W 85 addwf isr_xC+1,W
83 movwf OFF+0 86 movwf OFF+0
84 movf C2+1,W 87 movf C2+1,W
85 addwfc isr_xC+2,W 88 addwfc isr_xC+2,W
86 movwf OFF+1 89 movwf OFF+1
87 90
88 ; Calculate SENS = C1/2 + ((C3+200)*dT)/2^13 + 3000 91 ; calculate SENS = C1/2 + ((C3+200)*dT)/2^13 + 3000
89 movlw LOW(.200) ; C3+200 --> A 92 movlw LOW(.200) ; C3+200 --> A
90 addwf C3+0,W 93 addwf C3+0,W
91 movwf isr_xA+0 94 movwf isr_xA+0
92 movlw HIGH(.200) 95 movlw HIGH(.200)
93 addwfc C3+1,W 96 addwfc C3+1,W
94 movwf isr_xA+1 97 movwf isr_xA+1
95 ; B still contains dT2 98 ; B still contains dT2
96 call isr_signed_mult16x16 ; A*B --> C 99 call isr_signed_mult16x16 ; A*B --> C
97 movlw .13-.8 ; A 13bit shift = 1 byte + 5 bits. 100 movlw .13-.8 ; A 13 bit shift = 1 byte + 5 bits
98 call isr_shift_C31 101 call isr_shift_C31
99 102
100 bcf STATUS,C ; SENS = C1 / 2 103 bcf STATUS,C ; SENS = C1 / 2
101 rrcf C1+1,W 104 rrcf C1+1,W
102 movwf SENS+1 105 movwf SENS+1
103 rrcf C1+0,W 106 rrcf C1+0,W
104 movwf SENS+0 107 movwf SENS+0
105 108
106 movlw LOW(.3000) ; Add 3000 109 movlw LOW(.3000) ; add 3000
107 addwf isr_xC+1,F 110 addwf isr_xC+1,F
108 movlw HIGH(.3000) 111 movlw HIGH(.3000)
109 addwfc isr_xC+2,F 112 addwfc isr_xC+2,F
110 113
111 movf isr_xC+1,W ; And sum into SENS 114 movf isr_xC+1,W ; and sum into SENS
112 addwf SENS+0,F 115 addwf SENS+0,F
113 movf isr_xC+2,W 116 movf isr_xC+2,W
114 addwfc SENS+1,F 117 addwfc SENS+1,F
115 118
116 ; calculate amb_pressure = (sens * (d1-off))/2^12 + 1000 119 ; calculate absolute pressure = (sens * (d1-off))/2^12 + 1000
117 movf OFF+0,W ; d1-off --> a 120 movf OFF+0,W ; d1-off --> a
118 subwf D1+0,W 121 subwf D1+0,W
119 movwf isr_xA+0 122 movwf isr_xA+0
120 movf OFF+1,W 123 movf OFF+1,W
121 subwfb D1+1,W 124 subwfb D1+1,W
122 movwf isr_xA+1 125 movwf isr_xA+1
123 126
124 movff SENS+0,isr_xB+0 ; sens --> b 127 MOVII SENS,isr_xB ; sens --> b
125 movff SENS+1,isr_xB+1
126 call isr_signed_mult16x16 128 call isr_signed_mult16x16
127 movlw .12-.8 ; a 12bit shift = 1 byte + 4 bits. 129 movlw .12-.8 ; a 12 bit shift = 1 byte + 4 bits
128 call isr_shift_C31 130 call isr_shift_C31
129 131
130 movlw LOW(.1000) ; add 1000 132 movlw LOW .1000 ; add 1000
131 addwf isr_xC+1,F 133 addwf isr_xC+1,F
132 movlw HIGH(.1000) 134 movlw HIGH .1000
133 addwfc isr_xC+2,F 135 addwfc isr_xC+2,F
134 136
135 ; Add opt_pressure_adjust to result (SIGNED!) 137 ; add opt_pressure_adjust to result (SIGNED!)
136 movff opt_pressure_adjust,isr_xC+0 138 movff opt_pressure_adjust,isr_xC+0; get adjustment value (signed)
137 139 movf isr_xC+0,F ; excite flags, opt_pressure_adjust = 0 ?
138 btfss isr_xC+0,7 ; < 0 ? 140 bz calc_compensation_1 ; YES - skip pressure adjustment
139 bra pressure_extra_add ; No 141 btfss isr_xC+0,7 ; NO - opt_pressure_adjust < 0 ?
140 ; Yes 142 bra pressure_extra_add ; NO - add offset
141 comf isr_xC+0,F 143 ;bra pressure_extra_sub ; YES - subtract offset
142 incf isr_xC+0,F 144
143 ; Check for max. of 20mbar 145 pressure_extra_sub:
144 movlw .22 146 comf isr_xC+0,F ; complement opt_pressure_adjust
145 cpfslt isr_xC+0 147 incf isr_xC+0,F ; ...
146 clrf isr_xC+0 148 movlw .22 ; check for max. of 20 mbar
147 ; Subtract 149 cpfslt isr_xC+0 ; opt_pressure_adjust < 21 mbar ?
148 movf isr_xC+0,W 150 clrf isr_xC+0 ; NO - reset opt_pressure_adjust to zero
149 subwf isr_xC+1,F 151 movf isr_xC+0,W ; get opt_pressure_adjust to WREG
150 movlw .0 152 subwf isr_xC+1,F ; pressure value -= opt_pressure_adjust, low byte
151 subwfb isr_xC+2,F 153 movlw .0 ; pressure value -= opt_pressure_adjust, high byte
152 bra pressure_extra_common 154 subwfb isr_xC+2,F ; ...
155 bra calc_compensation_1 ; continue with checking for simulator mode
153 156
154 pressure_extra_add: 157 pressure_extra_add:
155 ; Check for max. of 20mbar 158 movlw .21 ; check for max. of 20 mbar
156 movlw .21 159 cpfslt isr_xC+0 ; opt_pressure_adjust < 21 mbar ?
157 cpfslt isr_xC+0 160 clrf isr_xC+0 ; NO - reset opt_pressure_adjust to zero
158 clrf isr_xC+0 161 movf isr_xC+0,W ; get opt_pressure_adjust to WREG
159 ; Add 162 addwf isr_xC+1,F ; pressure value += opt_pressure_adjust, low byte
160 movf isr_xC+0,W 163 movlw .0 ; pressure value += opt_pressure_adjust, high byte
161 addwf isr_xC+1,F 164 addwfc isr_xC+2,F ; ...
162 movlw .0 165 ;bra calc_compensation_1 ; continue with checking for simulator mode
163 addwfc isr_xC+2,F 166
164 167 calc_compensation_1:
165 pressure_extra_common: 168 bcf sensor_override_active ; clear sensor override active flag by default
166 banksel common ; flag2 is in bank 1 169 btfss sensor_override_request ; sensor override requested?
167 btfss simulatormode_active ; are we in simulator mode? 170 bra calc_compensation_add_avg ; NO - keep taking absolute pressure from sensor
168 bra calc_compensation_2 ; no 171 btfsc quit_simulatormode ; YES - shall quit simulator mode (the fast way)?
169 172 bra calc_compensation_sim_quit ; YES - force pressure_rel_sim to zero
170 banksel isr_xC+2 173 ;bra calc_compensation_sim ; NO - calculate pressure_rel_sim from simulated depth
171 movlw .5 174
172 cpfsgt isr_xC+2 ; > 1280 mbar ? 175 calc_compensation_sim: ; check if OSTC got submerged
173 bra pressure_extra_common2 ; No 176 movlw .5 ; coding in high byte for 1280
174 ; Yes, reset sim_pressure:2 to 1000mbar (End of sim) 177 cpfsgt isr_xC+2 ; absolute pressure > 1280 mbar, i.e. OSTC submerged?
175 movlw LOW .1000 178 bra calc_compensation_sim_slave ; NO - slave pressure_rel_sim to target depth
176 movwf sim_pressure+0 179 ;bra calc_compensation_sim_quit ; YES - force pressure_rel_sim to zero
177 movlw HIGH .1000 180
178 movwf sim_pressure+1 181 calc_compensation_sim_quit:
179 182 CLRI pressure_rel_sim ; set pressure_rel_sim to zero, this paves a restart into surface/dive mode
180 pressure_extra_common2: 183 bra calc_compensation_sim_com ; continue with common part
181 movff sim_pressure+0,isr_xC+1 ; override readings with simulator values 184
182 movff sim_pressure+1,isr_xC+2 185 calc_compensation_sim_slave:
183 186 movf simulatormode_depth,W ; copy simulated depth to WREG
184 calc_compensation_2: 187 mullw .100 ; multiply with 100 to turn depth from meters to relative target pressure in mbar
185 banksel isr_backup 188 ; check if shall go up
186 movf isr_xC+1,W ; Then sum_up to pressure averaging buffer. 189 movf pressure_rel_sim+0,W ; get relative pressure, low byte
187 addwf amb_pressure_avg+0,F 190 subwf PRODL,W ; WREG = relative target pressure - pressure_rel_sim (low byte)
188 movf isr_xC+2,W 191 movf pressure_rel_sim+1,W ; get relative pressure, high byte
189 addwfc amb_pressure_avg+1,F 192 subwfb PRODH,W ; WREG = relative target pressure - pressure_rel_sim (high byte)
193 btfsc STATUS,N ; result < 0, i.e. pressure_rel_sim > relative target pressure ?
194 bra calc_compensation_sim_up ; YES - decrease pressure_rel_sim
195 ; check if shall go down
196 movf PRODL,W ; get relative target pressure, low byte
197 subwf pressure_rel_sim+0,W ; WREG = pressure_rel_sim - relative target pressure (low byte)
198 movf PRODH,W ; get relative target pressure, high byte
199 subwfb pressure_rel_sim+1,W ; WREG = pressure_rel_sim - relative target pressure (high byte)
200 btfsc STATUS,N ; result < 0, i.e. relative target pressure > pressure_rel_sim ?
201 bra calc_compensation_sim_down ; YES - increase pressure_rel_sim
202 ; both pressures are equal
203 bra calc_compensation_sim_com ; NO to both - keep pressure_rel_sim as it is
204
205 calc_compensation_sim_up:
206 movf PRODL,W ; get relative target pressure, low byte
207 subwf pressure_rel_sim+0,W ; WREG = pressure_rel_sim - relative target pressure (low byte)
208 movwf PRODL ; PRODL = pressure_rel_sim - relative target pressure (low byte)
209 movf PRODH,W ; get relative target pressure, high byte
210 subwfb pressure_rel_sim+1,W ; WREG = pressure_rel_sim - relative target pressure (high byte)
211 tstfsz WREG ; more than 255 mbar off from target?
212 bra calc_compensation_sim_up_norm ; YES - go up with normal speed
213 movlw simulator_ascent_threshold ; NO - get remaining difference for slowing down ascent
214 cpfslt PRODL ; - remaining difference to target < decrement?
215 bra calc_compensation_sim_up_norm ; NO - go up with normal speed
216 ;bra calc_compensation_sim_up_slow ; YES - go up with slow speed
217
218 calc_compensation_sim_up_slow:
219 DECI pressure_rel_sim ; subtract slow decrement (1 mbar) from pressure_rel_sim
220 bra calc_compensation_sim_com ; continue with common part
221
222 calc_compensation_sim_up_norm:
223 SUBLI simulator_ascent_rate,pressure_rel_sim ; subtract normal decrement from pressure_rel_sim
224 bra calc_compensation_sim_com ; continue with common part
225
226 calc_compensation_sim_down:
227 movf pressure_rel_sim+0,W ; get relative pressure, low byte
228 subwf PRODL,F ; PRODL = relative target pressure - pressure_rel_sim (low byte)
229 movf pressure_rel_sim+1,W ; get relative pressure, high byte
230 subwfb PRODH,W ; WREG = relative target pressure - pressure_rel_sim (high byte)
231 tstfsz WREG ; more than 255 mbar off from target?
232 bra calc_compensation_sim_down_norm ; YES - go down with normal speed
233 movlw simulator_descent_threshold ; NO - get remaining difference for slowing down descent
234 cpfslt PRODL ; - remaining difference to target < increment?
235 bra calc_compensation_sim_down_norm ; NO - go down with normal speed
236 ;bra calc_compensation_sim_down_slow ; YES - go down with slow speed
237
238 calc_compensation_sim_down_slow:
239 INCI pressure_rel_sim ; add slow increment (1 mbar) to pressure_rel_sim
240 bra calc_compensation_sim_com ; continue with common part
241
242 calc_compensation_sim_down_norm:
243 ADDLI simulator_descent_rate,pressure_rel_sim ; add normal increment to pressure_rel_sim
244 ;bra calc_compensation_sim_com ; continue with common part
245
246 calc_compensation_sim_com:
247 movf pressure_surf+0,W ; copy surface pressure to WREG, low byte
248 addwf pressure_rel_sim+0,W ; add surface pressure to relative pressure to gain simulated absolute pressure, low byte
249 movwf isr_xC+1 ; override sensor pressure with simulated pressure, low byte
250 movf pressure_surf+1,W ; copy surface pressure to WREG, high byte
251 addwfc pressure_rel_sim+1,W ; add surface pressure to relative pressure to gain simulated absolute pressure, high byte
252 movwf isr_xC+2 ; override sensor pressure with simulated pressure, high byte
253 bsf sensor_override_active ; confirm sensor override is active
254
255 calc_compensation_add_avg:
256 ; add current absolute pressure to averaging buffer
257 movf isr_xC+1,W ; copy current absolute pressure to WREG, low byte
258 addwf pressure_abs_avg+0,F ; pressure_abs_avg += current pressure, low byte
259 movf isr_xC+2,W ; copy current absolute pressure to WREG, high byte
260 addwfc pressure_abs_avg+1,F ; pressure_abs_avg += current pressure, high byte
261
262 ;---- temperature sensor compensation
190 263
191 ; calculate temp = 200 + dT*(C6+100)/2^11 264 ; calculate temp = 200 + dT*(C6+100)/2^11
192 movlw LOW(.100) ; C6 + 100 --> A 265 movlw LOW(.100) ; C6 + 100 --> A
193 addwf C6+0,W 266 addwf C6+0,W
194 movwf isr_xA+0 267 movwf isr_xA+0
195 movlw HIGH(.100) 268 movlw HIGH(.100)
196 addwfc C6+1,W 269 addwfc C6+1,W
197 movwf isr_xA+1 270 movwf isr_xA+1
198 271
199 movff xdT2+0,isr_xB+0 ; dT2 --> B 272 MOVII xdT2,isr_xB ; dT2 --> B
200 movff xdT2+1,isr_xB+1
201 call isr_signed_mult16x16 ; A*B 273 call isr_signed_mult16x16 ; A*B
202 movlw .11-.8 ; A 12 bit shift = 1 byte + 3 bits 274 movlw .11-.8 ; a 12 bit shift = 1 byte + 3 bits
203 call isr_shift_C31 275 call isr_shift_C31
204 276
205 movlw LOW(.200) ; Add 200 277 movlw LOW(.200) ; add 200
206 addwf isr_xC+1,F 278 addwf isr_xC+1,F
207 movlw HIGH(.200) 279 movlw HIGH(.200)
208 addwfc isr_xC+2,F 280 addwfc isr_xC+2,F
209 281
210 ; Add opt_temperature_adjust to result (SIGNED!) 282 ; add opt_temperature_adjust to result (SIGNED!)
211 movff opt_temperature_adjust,isr_xC+0 283 movff opt_temperature_adjust,isr_xC+0
212 284
213 btfss isr_xC+0,7 ; < 0 ? 285 btfss isr_xC+0,7 ; < 0 ?
214 bra temperature_extra_add ; No 286 bra temperature_extra_add ; NO
215 ; Yes 287 comf isr_xC+0,F ; YES
216 comf isr_xC+0,F
217 incf isr_xC+0,F 288 incf isr_xC+0,F
218 ; Check for max. of 2.0°C 289 movlw .22 ; check for max. of 2.0°C
219 movlw .22
220 cpfslt isr_xC+0 290 cpfslt isr_xC+0
221 clrf isr_xC+0 291 clrf isr_xC+0
222 ; Subtract 292 movf isr_xC+0,W ; subtract
223 movf isr_xC+0,W
224 subwf isr_xC+1,F 293 subwf isr_xC+1,F
225 movlw .0 294 movlw .0
226 subwfb isr_xC+2,F 295 subwfb isr_xC+2,F
227 bra temperature_extra_common 296 bra temperature_extra_common
228 297
229 temperature_extra_add: 298 temperature_extra_add:
230 ; Check for max. of 2.0°C 299 movlw .21 ; check for max. of 2.0°C
231 movlw .21
232 cpfslt isr_xC+0 300 cpfslt isr_xC+0
233 clrf isr_xC+0 301 clrf isr_xC+0
234 ; Add 302 movf isr_xC+0,W ; add
235 movf isr_xC+0,W
236 addwf isr_xC+1,F 303 addwf isr_xC+1,F
237 movlw .0 304 movlw .0
238 addwfc isr_xC+2,F 305 addwfc isr_xC+2,F
306
239 temperature_extra_common: 307 temperature_extra_common:
240
241 movf isr_xC+1,W 308 movf isr_xC+1,W
242 addwf temperature_avg+0,F 309 addwf temperature_avg+0,F
243 movf isr_xC+2,W 310 movf isr_xC+2,W
244 addwfc temperature_avg+1,F 311 addwfc temperature_avg+1,F
245 312
246 return ; Done. 313 return ; done
247 314
248 ;============================================================================= 315 ;=============================================================================
249 global get_pressure_start 316
317 global get_pressure_start ; called from ISR and sleep mode, needs to be called bank isr_backup
250 get_pressure_start: 318 get_pressure_start:
251 rcall reset_MS5541 319 rcall reset_MS5541
252 movlw b'10100000' ;+3*high as start and 1+low as stop! 320 movlw b'10100000' ; +3*high as start and 1+low as stop
253 get_pressure_start2: 321 movwf dbuffer
254 movwf isr1_temp
255 movlw d'12' 322 movlw d'12'
256 rcall send_data_MS5541 323 rcall send_data_MS5541
257 return 324 return
258 325
259 global get_pressure_value 326 global get_pressure_value ; called from ISR and sleep mode, needs to be called bank isr_backup
260 get_pressure_value: 327 get_pressure_value:
261 btfsc MS5541_miso ; Conversion done? 328 btfsc MS5541_miso ; conversion done?
262 return ; No, Return 329 return ; NO - done
263 rcall get_2bytes_MS5541 330 rcall get_2bytes_MS5541
264 movff dMSB,D1+1 331 movff dMSB,D1+1
265 movff dLSB,D1+0 332 movff dLSB,D1+0
266 return 333 return
267 334
268 ;============================================================================= 335 ;=============================================================================
269 global get_temperature_start 336
337 global get_temperature_start ; called from ISR and sleep mode, needs to be called in bank isr_backup
270 get_temperature_start: 338 get_temperature_start:
271 rcall reset_MS5541 339 rcall reset_MS5541
272 movlw b'10010000' ;+3*high as start and 1+low as stop! 340 movlw b'10010000' ; +3*high as start and 1+low as stop
273 bra get_pressure_start2 ; continue in "get_pressure" 341 movwf dbuffer
274 342 movlw d'12'
275 global get_temperature_value 343 rcall send_data_MS5541
344 return
345
346
347 global get_temperature_value ; called from ISR and sleep mode, needs to be called in bank isr_backup
276 get_temperature_value: 348 get_temperature_value:
277 btfsc MS5541_miso ; Conversion done? 349 btfsc MS5541_miso ; conversion done?
278 return ; No, Return 350 return ; NO - done
279 rcall get_2bytes_MS5541 351 rcall get_2bytes_MS5541
280 movff dMSB,D2+1 352 movff dMSB,D2+1
281 movff dLSB,D2+0 353 movff dLSB,D2+0
282 return 354 return
283 355
284 ;============================================================================= 356 ;=============================================================================
285 global get_calibration_data 357
358 global get_calibration_data ; called by start, returns in bank common
286 get_calibration_data: 359 get_calibration_data:
287 banksel common 360 banksel isr_backup ; select bank ISR data
288 bsf no_sensor_int ; disable sensor interrupts 361 bsf block_sensor_interrupt ; disable sensor interrupts
289 banksel isr_backup ; Back to Bank0 ISR data
290 362
291 rcall reset_MS5541 363 rcall reset_MS5541
292 movlw b'01010100' ;+3*high as start and 1+low as stop! 364 movlw b'01010100' ; +3*high as start and 1+low as stop
293 movwf isr1_temp 365 movwf dbuffer
294 movlw d'13' 366 movlw d'13'
295 rcall send_data_MS5541 367 rcall send_data_MS5541
296 rcall get_2bytes_MS5541 368 rcall get_2bytes_MS5541
297 movff dMSB,ir_s8_buffer+1 369 movff dMSB,ir_s8_buffer+1
298 movff dLSB,ir_s8_buffer+0 370 movff dLSB,ir_s8_buffer+0
299 371
300 movlw b'01011000' ;+3*high as start and 1+low as stop! 372 movlw b'01011000' ; +3*high as start and 1+low as stop
301 movwf isr1_temp 373 movwf dbuffer
302 movlw d'13' 374 movlw d'13'
303 rcall send_data_MS5541 375 rcall send_data_MS5541
304 rcall get_2bytes_MS5541 376 rcall get_2bytes_MS5541
305 movff dMSB,ir_s8_buffer+3 377 movff dMSB,ir_s8_buffer+3
306 movff dLSB,ir_s8_buffer+2 378 movff dLSB,ir_s8_buffer+2
307 379
308 movlw b'01100100' ;+3*high as start and 1+low as stop! 380 movlw b'01100100' ; +3*high as start and 1+low as stop
309 movwf isr1_temp 381 movwf dbuffer
310 movlw d'13' 382 movlw d'13'
311 rcall send_data_MS5541 383 rcall send_data_MS5541
312 rcall get_2bytes_MS5541 384 rcall get_2bytes_MS5541
313 movff dMSB,ir_s8_buffer+5 385 movff dMSB,ir_s8_buffer+5
314 movff dLSB,ir_s8_buffer+4 386 movff dLSB,ir_s8_buffer+4
315 387
316 movlw b'01101000' ;+3*high as start and 1+low as stop! 388 movlw b'01101000' ; +3*high as start and 1+low as stop
317 movwf isr1_temp 389 movwf dbuffer
318 movlw d'13' 390 movlw d'13'
319 rcall send_data_MS5541 391 rcall send_data_MS5541
320 rcall get_2bytes_MS5541 392 rcall get_2bytes_MS5541
321 movff dMSB,ir_s8_buffer+7 393 movff dMSB,ir_s8_buffer+7
322 movff dLSB,ir_s8_buffer+6 394 movff dLSB,ir_s8_buffer+6
323 395
324 ; calculate C1 (16Bit) 396 ; calculate C1 (16Bit)
325 movff ir_s8_buffer+1, C1+1 397 movff ir_s8_buffer+1, C1+1
326 bcf STATUS,C 398 bcf STATUS,C
327 rrcf C1+1 399 rrcf C1+1
328 bcf STATUS,C 400 bcf STATUS,C
329 rrcf C1+1 401 rrcf C1+1
341 bsf STATUS,C 413 bsf STATUS,C
342 btfss ir_s8_buffer+1,2 414 btfss ir_s8_buffer+1,2
343 bcf STATUS,C 415 bcf STATUS,C
344 rrcf C1+0 416 rrcf C1+0
345 417
346 ; calculate C2 (16Bit) 418 ; calculate C2 (16Bit)
347 movff ir_s8_buffer+2, C2+0 419 movff ir_s8_buffer+2, C2+0
348 bsf STATUS,C 420 bsf STATUS,C
349 btfss ir_s8_buffer+3,0 421 btfss ir_s8_buffer+3,0
350 bcf STATUS,C 422 bcf STATUS,C
351 rrcf C2+0 423 rrcf C2+0
388 bcf STATUS,C 460 bcf STATUS,C
389 rrcf C2+1 461 rrcf C2+1
390 bcf STATUS,C 462 bcf STATUS,C
391 rrcf C2+1 463 rrcf C2+1
392 464
393 ; calculate C3 (16Bit) 465 ; calculate C3 (16Bit)
394 movff ir_s8_buffer+5,C3+0 466 movff ir_s8_buffer+5,C3+0
395 bsf STATUS,C 467 bsf STATUS,C
396 btfss ir_s8_buffer+4,7 468 btfss ir_s8_buffer+4,7
397 bcf STATUS,C 469 bcf STATUS,C
398 rlcf C3+0 470 rlcf C3+0
404 btfsc ir_s8_buffer+5,7 476 btfsc ir_s8_buffer+5,7
405 bsf C3+1,1 477 bsf C3+1,1
406 btfsc ir_s8_buffer+5,6 478 btfsc ir_s8_buffer+5,6
407 bsf C3+1,0 479 bsf C3+1,0
408 480
409 ; calculate C4 (16Bit) 481 ; calculate C4 (16Bit)
410 movff ir_s8_buffer+7,C4+0 482 movff ir_s8_buffer+7,C4+0
411 bsf STATUS,C 483 bsf STATUS,C
412 btfss ir_s8_buffer+6,7 484 btfss ir_s8_buffer+6,7
413 bcf STATUS,C 485 bcf STATUS,C
414 rlcf C4+0 486 rlcf C4+0
415 clrf C4+1 487 clrf C4+1
416 btfsc ir_s8_buffer+7,7 488 btfsc ir_s8_buffer+7,7
417 bsf C4+1,0 489 bsf C4+1,0
418 490
419 ; C4=C4-250 491 ; C4=C4-250
420 movlw LOW(-.250) ; C4 - 250 --> C4 492 movlw LOW(-.250) ; C4 - 250 --> C4
421 addwf C4+0,W 493 addwf C4+0,W
422 movwf C4+0 494 movwf C4+0
423 movlw -1 ; HIGH(- .250) is not understood... 495 movlw -1 ; HIGH(- .250) is not understood...
424 addwfc C4+1,W 496 addwfc C4+1,W
425 movwf C4+1 497 movwf C4+1
426 498
427 ; calculate C5 (16Bit) 499 ; calculate C5 (16Bit)
428 movff ir_s8_buffer+4,C5+0 500 movff ir_s8_buffer+4,C5+0
429 bcf C5+0,6 501 bcf C5+0,6
430 btfsc ir_s8_buffer+2,0 502 btfsc ir_s8_buffer+2,0
431 bsf C5+0,6 503 bsf C5+0,6
432 bcf C5+0,7 504 bcf C5+0,7
445 ; calculate C5 = UT1 517 ; calculate C5 = UT1
446 ; C5 = 8*C5 + 10000 (u16 range 10.000 .. +42.760) 518 ; C5 = 8*C5 + 10000 (u16 range 10.000 .. +42.760)
447 clrf isr_xA+1 519 clrf isr_xA+1
448 movlw d'8' 520 movlw d'8'
449 movwf isr_xA+0 521 movwf isr_xA+0
450 movff C5+0,isr_xB+0 522 MOVII C5,isr_xB
451 movff C5+1,isr_xB+1 523 call isr_unsigned_mult16x16 ; isr_xA*isr_xB=isr_xC
452 call isr_unsigned_mult16x16 ;isr_xA*isr_xB=isr_xC 524 MOVII isr_xC,C5
453 movff isr_xC+0,C5+0
454 movff isr_xC+1,C5+1
455 movlw LOW d'10000' 525 movlw LOW d'10000'
456 addwf C5+0,F 526 addwf C5+0,F
457 movlw HIGH d'10000' 527 movlw HIGH d'10000'
458 addwfc C5+1,F ; = 8*C5 + 10000 528 addwfc C5+1,F ; = 8*C5 + 10000
459 529
460 ; calculate C6 (16Bit) 530 ; calculate C6 (16Bit)
461 clrf C6+1 531 clrf C6+1
462 movff ir_s8_buffer+6,C6+0 532 movff ir_s8_buffer+6,C6+0
463 bcf C6+0,7 533 bcf C6+0,7
464 534
465 banksel common 535 clrf sensor_state_counter ; reset state counter
466 bcf no_sensor_int ; enable sensor interrupts 536 bcf block_sensor_interrupt ; re-enable sensor interrupts
467 bcf pressure_refresh ; Clear flag 537 banksel common ; back to bank common
468 banksel isr_backup ; Back to Bank0 ISR data 538 return ; done
469
470 clrf sensor_state_counter ; Then reset State counter
471
472 return
473 539
474 ;============================================================================= 540 ;=============================================================================
475 reset_MS5541_one: 541 reset_MS5541_one:
476 bsf MS5541_mosi 542 bsf MS5541_mosi
477 bra send_clk_pulse ; Send one high-low sequence on MS5541_clk -> and return 543 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return
478 544
479 reset_MS5541_zero: 545 reset_MS5541_zero:
480 bcf MS5541_mosi 546 bcf MS5541_mosi
481 bra send_clk_pulse ; Send one high-low sequence on MS5541_clk -> and return 547 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return
482 548
483 reset_MS5541: 549 reset_MS5541:
484 rcall reset_MS5541_one ; 0 550 rcall reset_MS5541_one ; 0
485 rcall reset_MS5541_zero 551 rcall reset_MS5541_zero
486 rcall reset_MS5541_one 552 rcall reset_MS5541_one
501 rcall reset_MS5541_zero 567 rcall reset_MS5541_zero
502 rcall reset_MS5541_zero 568 rcall reset_MS5541_zero
503 rcall reset_MS5541_zero 569 rcall reset_MS5541_zero
504 rcall reset_MS5541_zero ; 20 570 rcall reset_MS5541_zero ; 20
505 return 571 return
572
506 573
507 get_2bytes_MS5541: 574 get_2bytes_MS5541:
508 movlw d'8' 575 movlw d'8'
509 movwf clock_count 576 movwf clock_count
510 rcall recieve_loop 577 rcall recieve_loop
511 movff isr1_temp,dMSB 578 movff dbuffer,dMSB
512
513 movlw d'8' 579 movlw d'8'
514 movwf clock_count 580 movwf clock_count
515 rcall recieve_loop 581 rcall recieve_loop
516 movff isr1_temp,dLSB 582 movff dbuffer,dLSB
517 bra send_clk_pulse ; Send one high-low sequence on MS5541_clk -> and return 583 bra send_clk_pulse ; send one high-low sequence on MS5541_clk and return
518 ;return
519 584
520 recieve_loop: 585 recieve_loop:
521 rcall send_clk_pulse ; Send one high-low sequence on MS5541_clk 586 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
522 btfss MS5541_miso ;MSB first 587 btfss MS5541_miso ; MSB first
523 bcf STATUS,C 588 bcf STATUS,C
524 btfsc MS5541_miso ;MSB first 589 btfsc MS5541_miso ; MSB first
525 bsf STATUS,C 590 bsf STATUS,C
526 rlcf isr1_temp,F 591 rlcf dbuffer,F
527 decfsz clock_count,F 592 decfsz clock_count,F
528 bra recieve_loop 593 bra recieve_loop
529 return 594 return
530 595
531 send_clk_pulse: 596 send_clk_pulse:
541 nop 606 nop
542 nop 607 nop
543 nop 608 nop
544 return 609 return
545 610
611
546 send_data_MS5541: 612 send_data_MS5541:
547 movwf clock_count ; From WREG 613 movwf clock_count ; from WREG
548 ; send three startbits first 614 ; send three start bits first
549 bcf MS5541_clk 615 bcf MS5541_clk
550 nop 616 nop
551 nop 617 nop
552 bsf MS5541_mosi 618 bsf MS5541_mosi
553 movlw d'3' 619 movlw d'3'
554 subwf clock_count,F ; total bit counter 620 subwf clock_count,F ; total bit counter
555 rcall send_clk_pulse ; Send one high-low sequence on MS5541_clk 621 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
556 rcall send_clk_pulse ; Send one high-low sequence on MS5541_clk 622 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
557 rcall send_clk_pulse ; Send one high-low sequence on MS5541_clk 623 rcall send_clk_pulse ; send one high-low sequence on MS5541_clk
558 ; now send 8 bytes from isr_temp1 and fill-up with zeros 624 send_data_MS5541_loop: ; now send 8 bits from dbuffer and fill-up with zeros
559 send_data_MS5541_2:
560 bcf MS5541_clk 625 bcf MS5541_clk
561 nop 626 nop
562 nop 627 nop
563 628 btfss dbuffer,7 ; MSB first
564 btfss isr1_temp,7 ; MSB first
565 bcf MS5541_mosi 629 bcf MS5541_mosi
566 btfsc isr1_temp,7 ; MSB first 630 btfsc dbuffer,7 ; MSB first
567 bsf MS5541_mosi 631 bsf MS5541_mosi
568
569 bsf MS5541_clk 632 bsf MS5541_clk
570 633 bcf STATUS,C
571 bcf STATUS,C 634 rlcf dbuffer,F
572 rlcf isr1_temp,F
573 nop 635 nop
574 nop 636 nop
575 ; nop 637 ; nop
576 ; nop 638 ; nop
577 ; nop 639 ; nop
578 ; nop 640 ; nop
579 ; bcf MS5541_clk 641 ; bcf MS5541_clk
580
581 decfsz clock_count,F 642 decfsz clock_count,F
582 bra send_data_MS5541_2 643 bra send_data_MS5541_loop
583 bcf MS5541_clk 644 bcf MS5541_clk
584 return 645 return
585 646
586 END 647 END