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