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