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