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