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