Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 467:6ca155fc1509
battery gauge handling
author | heinrichsweikamp |
---|---|
date | Sun, 20 Nov 2016 12:54:36 +0100 |
parents | 7f4c3b2e6bb7 |
children | 408329337b8b |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
3 ; File adc.asm | |
4 ; | |
5 ; | |
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
7 ;============================================================================= | |
8 ; HISTORY | |
9 ; 2011-08-08 : [mH] moving from OSTC code | |
10 | |
275 | 11 #include "hwos.inc" |
0 | 12 #include "math.inc" |
13 #include "wait.inc" | |
14 #include "eeprom_rs232.inc" | |
113 | 15 #include "i2c.inc" |
0 | 16 |
17 sensors CODE | |
18 | |
19 wait_adc: | |
20 movwf ADCON0 | |
21 nop | |
22 bsf ADCON0,1 ; start ADC | |
23 wait_adc2: | |
24 btfsc ADCON0,1 ; Wait... | |
25 bra wait_adc2 | |
26 return | |
27 | |
28 global get_battery_voltage | |
29 get_battery_voltage: ; starts ADC and waits until fnished | |
448 | 30 btfss battery_gauge_available |
113 | 31 bra get_battery_voltage1 ; Normal ostc3 hardware |
32 | |
33 call lt2942_get_accumulated_charge | |
34 call lt2942_get_voltage | |
466 | 35 |
36 tstfsz batt_voltage+1 ; <256mV? | |
37 bra get_battery_voltage_noretry ; No | |
218 | 38 |
466 | 39 ; Retry |
40 call lt2942_get_accumulated_charge | |
41 call lt2942_get_voltage | |
42 | |
43 get_battery_voltage_noretry: | |
218 | 44 btfsc divemode |
45 return ; Not in divemode | |
46 | |
207 | 47 bcf cv_active |
48 bcf cc_active | |
120 | 49 bcf LEDr |
50 bcf TRISJ,2 ; Chrg-Out output | |
51 bsf CHRG_OUT | |
52 | |
53 btfss CHRG_IN | |
207 | 54 bra charge_cc_active |
120 | 55 |
56 bcf CHRG_OUT | |
57 bsf TRISJ,2 ; Chrg-Out high impedance | |
58 | |
59 WAITMS d'1' | |
60 | |
61 btfsc CHRG_IN | |
62 return | |
63 ;cv_active: | |
218 | 64 decfsz safety_stop_countdown,F |
65 return | |
266 | 66 movlw .15 |
67 cpfsgt batt_voltage+1 ; Batt Voltage >= 16*256mV (4,096V)? | |
260 | 68 bra charge_cc_active ; No |
207 | 69 bsf cc_active |
70 bsf cv_active | |
120 | 71 bsf LEDr ; Indicate charging |
72 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF | |
73 WAITMS d'10' | |
74 bcf LEDr ; Indicate charging | |
218 | 75 bsf safety_stop_countdown,0 ; =1 |
120 | 76 return |
77 | |
207 | 78 charge_cc_active: |
79 bsf cc_active | |
120 | 80 bsf LEDr ; Indicate charging |
81 bcf CHRG_OUT | |
82 bsf TRISJ,2 ; Chrg-Out high impedance | |
260 | 83 movlw .10 |
218 | 84 movwf safety_stop_countdown |
113 | 85 return |
86 | |
87 get_battery_voltage1: | |
0 | 88 bsf adc_running ; =1: The ADC is in use |
89 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
90 movwf ADCON1 | |
91 movlw b'00011001' ; power on ADC, select AN6 | |
92 rcall wait_adc | |
93 | |
94 movff ADRESH,batt_voltage+1 ; store value | |
95 movff ADRESL,batt_voltage+0 ; store value | |
96 bcf ADCON0,0 ; power off ADC | |
97 | |
98 ; Multiply with 2,006 to be excact here... | |
99 ; bcf STATUS,C | |
100 ; rlcf xA+0,F | |
101 ; | |
102 ; rlcf xA+1,F ; x2 | |
103 | |
104 ; movff xA+0,batt_voltage+0 ; store value | |
105 ; movff xA+1,batt_voltage+1 | |
106 | |
107 movlw LOW lithium_36v_low | |
108 movwf sub_a+0 | |
109 movlw HIGH lithium_36v_low | |
110 movwf sub_a+1 | |
111 movff batt_voltage+0,sub_b+0 | |
112 movff batt_voltage+1,sub_b+1 | |
113 call subU16 ; sub_c = sub_a - sub_b | |
114 ; Battery is 3,6V (>lithium_36v_low?) | |
115 btfss neg_flag | |
116 bra get_battery_voltage4 ; No, use 1,5V | |
117 | |
118 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!) | |
119 | |
120 ; Check if the battery is near-dead already | |
121 movlw LOW lithium_36v_empty | |
122 movwf sub_a+0 | |
123 movlw HIGH lithium_36v_empty | |
124 movwf sub_a+1 | |
125 call subU16 ; sub_c = sub_a - sub_b | |
126 ; Battery is not dead yet (>lithium_36v_empty?) | |
127 btfsc neg_flag | |
128 bra get_battery_voltage2 ; Yes, battery is still ok | |
129 | |
130 ; Battery is probably dead very soon | |
131 ; Set ">=24Ah used" into battery gauge registers | |
132 movlw .128 | |
133 movff WREG,battery_gauge+5 | |
134 | |
135 get_battery_voltage2: | |
136 ; Use 3,6V battery gauging mode | |
137 movff battery_gauge+5,xC+3 | |
138 movff battery_gauge+4,xC+2 | |
139 movff battery_gauge+3,xC+1 | |
140 movff battery_gauge+2,xC+0 | |
141 ; battery_gauge:6 is nAs | |
142 ; devide through 65536 | |
448 | 143 ; devide through battery_capacity:2 |
144 ; Result is in percent | |
145 movff internal_battery_capacity+0,xB+0 | |
146 movff internal_battery_capacity+1,xB+1 | |
0 | 147 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder |
148 movff xC+0,lo | |
149 ; Limit to 100 | |
150 movlw .100 | |
151 cpfslt lo | |
152 movwf lo | |
153 ; lo will be between 0 (Full) and 100 (empty) | |
154 movf lo,W | |
155 sublw .100 | |
156 movwf lo | |
157 get_battery_voltage3: | |
158 movlw .100 | |
159 cpfslt lo | |
160 movwf lo | |
161 ; lo will be between 100 (Full) and 0 (empty) | |
37
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
162 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
163 ; use 3,6V battery sensing based on 50mA load |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
164 ; 75% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
165 movff batt_voltage+0,sub_b+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
166 movff batt_voltage+1,sub_b+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
167 movlw LOW lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
168 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
169 movlw HIGH lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
170 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
171 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
172 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
173 bra get_battery_voltage3a |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
174 movlw .75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
175 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
176 get_battery_voltage3a: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
177 ; 50% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
178 movlw LOW lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
179 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
180 movlw HIGH lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
181 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
182 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
183 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
184 bra get_battery_voltage3b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
185 movlw .50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
186 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
187 get_battery_voltage3b: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
188 ; 25% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
189 movlw LOW lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
190 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
191 movlw HIGH lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
192 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
193 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
194 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
195 bra get_battery_voltage3c |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
196 movlw .25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
197 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
198 get_battery_voltage3c: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
199 ; 10% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
200 movlw LOW lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
201 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
202 movlw HIGH lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
203 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
204 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
205 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
206 bra get_battery_voltage3d |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
207 movlw .10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
208 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
209 get_battery_voltage3d: |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
210 movlw .100 |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
211 cpfslt lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
212 movwf lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
213 ; lo will be between 100 (Full) and 0 (empty) |
0 | 214 movf batt_percent,W |
215 cpfsgt lo ; keep batt_percent on the lowest value found | |
216 movff lo,batt_percent ; store value | |
37
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
217 ; btfsc battery_is_36v ; but always use computed value for 3,6V battery |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
218 ; movff lo,batt_percent ; store value |
0 | 219 bcf adc_running ; =1: The ADC is in use |
220 return | |
221 | |
222 get_battery_voltage4: | |
223 ; Use 1,5V battery voltage mode | |
224 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo | |
225 movff batt_voltage+0,sub_a+0 | |
226 movff batt_voltage+1,sub_a+1 | |
227 movlw LOW aa_15v_low | |
228 movwf sub_b+0 | |
229 movlw HIGH aa_15v_low | |
230 movwf sub_b+1 | |
231 call subU16 ; sub_c = sub_a - sub_b | |
232 bcf STATUS,C | |
233 rrcf sub_c+1 | |
234 rrcf sub_c+0 ; /2 | |
235 bcf STATUS,C | |
236 rrcf sub_c+1 | |
237 rrcf sub_c+0 ; /4 | |
238 movff sub_c+0,lo | |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
239 bra get_battery_voltage3d ; Check limits and return |
0 | 240 |
241 global get_ambient_level | |
242 get_ambient_level: ; starts ADC and waits until finished | |
243 btfsc adc_running ; ADC in use? | |
244 return ; Yes, return | |
245 | |
236 | 246 btfsc ambient_sensor |
113 | 247 bra get_ambient_level1 ; Normal ostc3 hardware |
203 | 248 |
249 banksel isr_backup ; Back to Bank0 ISR data | |
250 movff opt_brightness,isr1_temp | |
251 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
354 | 252 movlw ambient_light_max_high_cr; cR and 2 hardware brightest setting |
203 | 253 dcfsnz isr1_temp,F |
254 movlw ambient_light_max_eco ; brightest setting | |
255 dcfsnz isr1_temp,F | |
256 movlw ambient_light_max_medium; brightest setting | |
257 | |
258 movff WREG,ambient_light+0 ; Set to max. | |
259 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
260 return | |
113 | 261 |
262 get_ambient_level1: | |
0 | 263 movlw b'00000000' ; Vref+ = Vdd |
264 movwf ADCON1 | |
265 movlw b'00011101' ; power on ADC, select AN7 | |
266 rcall wait_adc | |
267 | |
268 movff ADRESH,ambient_light+1 | |
269 movff ADRESL,ambient_light+0 | |
270 bcf ADCON0,0 ; power off ADC | |
271 | |
272 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
273 ; First: Devide through 16 | |
274 bcf STATUS,C | |
275 rrcf ambient_light+1 | |
276 rrcf ambient_light+0 | |
277 bcf STATUS,C | |
278 rrcf ambient_light+1 | |
279 rrcf ambient_light+0 | |
280 bcf STATUS,C | |
281 rrcf ambient_light+1 | |
282 rrcf ambient_light+0 | |
283 bcf STATUS,C | |
284 rrcf ambient_light+1 | |
285 rrcf ambient_light+0 | |
286 ; Result: ambient_light:2/16 | |
287 ; Now, make sure to have value between ambient_light_low and ambient_light_max | |
288 | |
289 movlw .254 | |
290 tstfsz ambient_light+1 ; >255? | |
291 movwf ambient_light+0 ; avoid ADC clipping | |
292 | |
293 incfsz ambient_light+0,W ; =255? | |
294 bra get_ambient_level2 ; No, continue | |
295 | |
296 movlw .254 | |
297 movwf ambient_light+0 ; avoid ADC clipping | |
298 | |
299 get_ambient_level2: | |
357
36bd27f42362
BUGFIX: Auto-Brightness clipped to lowest settings in bright sunlight on some devices
heinrichsweikamp
parents:
354
diff
changeset
|
300 ; movlw .10 |
36bd27f42362
BUGFIX: Auto-Brightness clipped to lowest settings in bright sunlight on some devices
heinrichsweikamp
parents:
354
diff
changeset
|
301 ; subwf ambient_light+0,F ; Subtract 10 (ADC Offset) |
36bd27f42362
BUGFIX: Auto-Brightness clipped to lowest settings in bright sunlight on some devices
heinrichsweikamp
parents:
354
diff
changeset
|
302 ; btfsc STATUS,N |
36bd27f42362
BUGFIX: Auto-Brightness clipped to lowest settings in bright sunlight on some devices
heinrichsweikamp
parents:
354
diff
changeset
|
303 ; movwf ambient_light+0 ; avoid clipping |
201 | 304 |
0 | 305 banksel isr_backup ; Back to Bank0 ISR data |
306 movff opt_brightness,isr1_temp | |
307 | |
308 btfsc RCSTA1,7 ; UART module on? | |
309 clrf isr1_temp ; Yes, set temporally to eco mode | |
310 | |
311 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
312 | |
313 banksel common ; flag is in bank1 | |
354 | 314 movlw ambient_light_max_high_cr; cR and 2 hardware brightest setting |
448 | 315 btfss battery_gauge_available |
136 | 316 movlw ambient_light_max_high_15V; 1,5V battery brightest setting |
317 btfsc battery_is_36v ; 3,6V battery in use? | |
318 movlw ambient_light_max_high_36V ; 3,6V battery brightest setting | |
0 | 319 banksel isr_backup ; Back to Bank0 ISR data |
320 | |
321 dcfsnz isr1_temp,F | |
322 movlw ambient_light_max_eco ; brightest setting | |
323 dcfsnz isr1_temp,F | |
324 movlw ambient_light_max_medium; brightest setting | |
325 | |
326 banksel common ; ambient_light is in Bank1 | |
327 incf ambient_light+0,F ; +1 | |
328 cpfslt ambient_light+0 ; smaller then WREG? | |
329 movwf ambient_light+0 ; No, set to max. | |
330 | |
331 banksel isr_backup ; Back to Bank0 ISR data | |
332 movff opt_brightness,isr1_temp | |
333 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
334 movlw ambient_light_min_high ; darkest setting | |
335 | |
336 dcfsnz isr1_temp,F | |
337 movlw ambient_light_min_eco ; darkest setting | |
338 dcfsnz isr1_temp,F | |
339 movlw ambient_light_min_medium; darkest setting | |
340 dcfsnz isr1_temp,F | |
341 movlw ambient_light_min_high ; darkest setting | |
342 | |
343 banksel common ; ambient_light is in Bank1 | |
344 cpfsgt ambient_light+0 ; bigger then WREG? | |
345 movwf ambient_light+0 ; No, set to min | |
346 | |
347 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
348 return | |
349 | |
113 | 350 global get_analog_inputs |
351 get_analog_inputs: ; starts ADC and waits until finished | |
0 | 352 bsf adc_running ; =1: The ADC is in use |
113 | 353 btfsc TFT_PWM |
354 bra get_analog_inputs ; Wait for PWM low | |
355 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
356 movwf ADCON1 | |
357 movlw b'00100001' ; power on ADC, select AN8 | |
358 rcall wait_adc | |
359 bcf STATUS,C | |
360 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
361 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
362 ; add to o2_mv_sensor1:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
363 addwf o2_mv_sensor1+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
364 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
365 addwfc o2_mv_sensor1+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
366 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
367 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
368 rrcf o2_mv_sensor1+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
369 rrcf o2_mv_sensor1+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
370 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
371 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
372 cpfsgt o2_mv_sensor1+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
373 bra get_analog_inputs2a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
374 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
375 clrf o2_mv_sensor1+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
376 clrf o2_mv_sensor1+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
377 get_analog_inputs2a: |
354 | 378 ; Ignore 1,9mV noise for not-connected inputs |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
379 tstfsz o2_mv_sensor1+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
380 bra get_analog_inputs2 ; Yes, skip here |
270
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
381 movlw .19 |
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
382 cpfsgt o2_mv_sensor1+0 ; >1,9mV? |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
383 clrf o2_mv_sensor1+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
384 get_analog_inputs2: |
113 | 385 movlw b'00100101' ; power on ADC, select AN9 |
0 | 386 rcall wait_adc |
113 | 387 bcf STATUS,C |
388 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
389 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
390 ; add to o2_mv_sensor2:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
391 addwf o2_mv_sensor2+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
392 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
393 addwfc o2_mv_sensor2+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
394 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
395 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
396 rrcf o2_mv_sensor2+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
397 rrcf o2_mv_sensor2+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
398 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
399 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
400 cpfsgt o2_mv_sensor2+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
401 bra get_analog_inputs3a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
402 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
403 clrf o2_mv_sensor2+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
404 clrf o2_mv_sensor2+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
405 get_analog_inputs3a: |
354 | 406 ; Ignore 1,9mV noise for not-connected inputs |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
407 tstfsz o2_mv_sensor2+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
408 bra get_analog_inputs3 ; Yes, skip here |
270
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
409 movlw .19 |
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
410 cpfsgt o2_mv_sensor2+0 ; >1,9mV? |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
411 clrf o2_mv_sensor2+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
412 get_analog_inputs3: |
113 | 413 movlw b'00101001' ; power on ADC, select AN10 |
414 rcall wait_adc | |
415 bcf STATUS,C | |
416 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
417 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
418 ; add to o2_mv_sensor3:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
419 addwf o2_mv_sensor3+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
420 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
421 addwfc o2_mv_sensor3+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
422 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
423 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
424 rrcf o2_mv_sensor3+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
425 rrcf o2_mv_sensor3+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
426 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
427 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
428 cpfsgt o2_mv_sensor3+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
429 bra get_analog_inputs4a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
430 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
431 clrf o2_mv_sensor3+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
432 clrf o2_mv_sensor3+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
433 get_analog_inputs4a: |
354 | 434 ; Ignore 1,9mV noise for not-connected inputs |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
435 tstfsz o2_mv_sensor3+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
436 bra get_analog_inputs4 ; Yes, skip here |
270
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
437 movlw .19 |
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
438 cpfsgt o2_mv_sensor3+0 ; >1,9mV? |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
439 clrf o2_mv_sensor3+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
440 get_analog_inputs4: |
0 | 441 bcf ADCON0,0 ; power off ADC |
442 bcf adc_running ; =1: The ADC is in use | |
443 return | |
444 | |
113 | 445 global piezo_config ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms) |
117 | 446 piezo_config: ; Settings between 20 and 200 |
416
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
447 clrf TMR5H |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
448 clrf TMR5L ; ~2sec |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
449 bcf PIR5,TMR5IF ; Clear flag |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
450 bcf switch_right |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
451 bcf switch_left |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
452 piezo_config0: |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
453 btfsc switch_right |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
454 bra piezo_config |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
455 btfsc switch_left |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
456 bra piezo_config ; Restart on button press |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
457 |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
458 btfss PIR5,TMR5IF |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
459 bra piezo_config0 ; Wait loop |
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
460 |
220
effd7259f5a5
make button sensitivity configurable (cR hardware)
heinrichsweikamp
parents:
218
diff
changeset
|
461 movff opt_cR_button_right,WREG; right button |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
462 btfsc flip_screen ; 180° rotation ? |
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
463 movff opt_cR_button_left,WREG ; Yes, left button |
117 | 464 rcall piezo_config_tx |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
465 |
220
effd7259f5a5
make button sensitivity configurable (cR hardware)
heinrichsweikamp
parents:
218
diff
changeset
|
466 movff opt_cR_button_left,WREG ; left button |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
467 btfsc flip_screen ; 180° rotation ? |
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
468 movff opt_cR_button_right,WREG; Yes, right button |
204 | 469 rcall piezo_config_tx |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
470 |
204 | 471 movlw .200 ; reserved |
472 rcall piezo_config_tx | |
473 movlw .200 ; reserved | |
474 rcall piezo_config_tx | |
475 return | |
476 | |
117 | 477 piezo_config_tx: ; Send one byte |
113 | 478 movwf uart1_temp ; Store byte |
479 movlw .8 | |
480 movwf uart2_temp ; Bit counter | |
481 bcf TX3_PIEZO_CFG ; Startbit | |
482 rcall piezo_config_wait_bit | |
483 piezo_config_tx_loop: | |
484 btfss uart1_temp,0 ; LSB first | |
485 bcf TX3_PIEZO_CFG | |
486 btfsc uart1_temp,0 ; LSB first | |
487 bsf TX3_PIEZO_CFG | |
488 rcall piezo_config_wait_bit | |
489 rrncf uart1_temp,F | |
490 decfsz uart2_temp,F | |
491 bra piezo_config_tx_loop | |
492 bsf TX3_PIEZO_CFG ; Stopbit | |
493 rcall piezo_config_wait_bit | |
494 return | |
495 | |
496 piezo_config_wait_bit: | |
497 setf TMR5H | |
498 movlw .255-.26 ;26 x 31,5µs = 819us | |
499 movwf TMR5L | |
500 bcf PIR5,TMR5IF ; Clear flag | |
501 piezo_config_wait_bit3: | |
502 btfss PIR5,TMR5IF | |
503 bra piezo_config_wait_bit3 ; Wait loop | |
504 return | |
505 | |
448 | 506 global reset_battery_pointer, reset_battery_internal_only |
0 | 507 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 |
113 | 508 extern lt2942_charge_done |
448 | 509 btfsc battery_gauge_available ; Something to reset? |
260 | 510 call lt2942_charge_done ; Yes, reset accumulating registers to 0xFFFF |
448 | 511 reset_battery_internal_only: |
113 | 512 clrf EEADRH |
448 | 513 clrf EEDATA ; Delete to zero |
514 write_int_eeprom 0x07 | |
515 write_int_eeprom 0x08 | |
516 write_int_eeprom 0x09 | |
517 write_int_eeprom 0x0A | |
518 write_int_eeprom 0x0B | |
519 write_int_eeprom 0x0C | |
0 | 520 banksel battery_gauge+0 |
521 clrf battery_gauge+0 | |
522 clrf battery_gauge+1 | |
523 clrf battery_gauge+2 | |
524 clrf battery_gauge+3 | |
525 clrf battery_gauge+4 | |
526 clrf battery_gauge+5 | |
527 banksel common | |
528 movlw .100 | |
529 movwf batt_percent | |
530 return | |
531 | |
448 | 532 global get_analog_switches |
533 get_analog_switches: ; starts ADC and waits until finished | |
534 btfsc analog_switches | |
535 bra get_analog_switches2 | |
536 ; no analog switches | |
537 bcf analog_sw2_pressed | |
538 bcf analog_sw1_pressed | |
539 return ; Done. | |
540 get_analog_switches2: | |
541 btfsc adc_running ; ADC in use? | |
542 return ; Yes, return | |
543 | |
544 movlw b'00001001' ; left justified | |
545 movwf ADCON2 | |
546 movlw b'00000000' ; Vref+ = Vdd | |
547 movwf ADCON1 | |
548 movlw b'00100101' ; power on ADC, select AN9 | |
549 rcall wait_adc | |
550 movff ADRESH,analog_sw2 | |
551 bcf analog_sw2_pressed | |
465
025a0ce21f8b
BUGFIX: Minor layout corrections in imperial units dive screen
heinrichsweikamp
parents:
451
diff
changeset
|
552 ; movlw .64 ; lower limit |
025a0ce21f8b
BUGFIX: Minor layout corrections in imperial units dive screen
heinrichsweikamp
parents:
451
diff
changeset
|
553 ; cpfsgt ADRESH |
025a0ce21f8b
BUGFIX: Minor layout corrections in imperial units dive screen
heinrichsweikamp
parents:
451
diff
changeset
|
554 ; bra sw2_pressed |
466 | 555 movlw .135 ; upper limit |
448 | 556 cpfsgt ADRESH |
557 bra get_analog_sw1 | |
558 sw2_pressed: | |
559 bsf analog_sw2_pressed | |
560 get_analog_sw1: | |
561 movlw b'00101001' ; power on ADC, select AN10 | |
562 rcall wait_adc | |
563 movff ADRESH,analog_sw1 | |
564 bcf analog_sw1_pressed | |
465
025a0ce21f8b
BUGFIX: Minor layout corrections in imperial units dive screen
heinrichsweikamp
parents:
451
diff
changeset
|
565 ; movlw .64 ; lower limit |
025a0ce21f8b
BUGFIX: Minor layout corrections in imperial units dive screen
heinrichsweikamp
parents:
451
diff
changeset
|
566 ; cpfsgt ADRESH |
025a0ce21f8b
BUGFIX: Minor layout corrections in imperial units dive screen
heinrichsweikamp
parents:
451
diff
changeset
|
567 ; bra sw1_pressed |
466 | 568 movlw .135 ; upper limit |
448 | 569 cpfsgt ADRESH |
570 bra get_analog_sw_done | |
571 sw1_pressed: | |
572 bsf analog_sw1_pressed | |
573 get_analog_sw_done: | |
574 movlw b'10001101' ; Restore to right justified | |
575 movwf ADCON2 | |
451 | 576 btfsc analog_sw1_pressed |
577 return | |
578 btfsc analog_sw2_pressed | |
579 return | |
580 setf TMR1H ; No button pressed, enhance timer1 to overflow quickly | |
448 | 581 return |
582 | |
0 | 583 END |