Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 629:237931377539
3.07 stable release
author | heinrichsweikamp |
---|---|
date | Fri, 29 Nov 2019 18:48:11 +0100 |
parents | cd58f7fc86db |
children | 185ba2f91f59 |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
629 | 3 ; File adc_lightsensor.asm combined next generation V3.06.1 |
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 |
623 | 18 adc_light CODE |
582 | 19 |
20 ;============================================================================= | |
0 | 21 |
623 | 22 wait_adc: ; bank-safe |
0 | 23 movwf ADCON0 |
582 | 24 nop |
25 bsf ADCON0,1 ; start ADC | |
0 | 26 wait_adc2: |
604 | 27 btfsc ADCON0,1 ; wait... |
0 | 28 bra wait_adc2 |
582 | 29 return |
0 | 30 |
31 global get_battery_voltage | |
623 | 32 get_battery_voltage: ; start ADC and wait until finished |
33 btfss battery_gauge_available ; battery gauge IC available? | |
34 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC | |
35 call lt2942_get_accumulated_charge ; YES - read coulomb counter | |
36 call lt2942_get_voltage ; - read battery voltage | |
37 call lt2942_get_temperature ; - read battery temperature | |
38 tstfsz batt_voltage+1 ; - read voltage < 256 mV ? | |
39 bra get_battery_voltage_1 ; NO - proceed | |
40 call lt2942_get_accumulated_charge ; YES - re-read coulomb counter | |
41 call lt2942_get_voltage ; - re-read battery voltage | |
42 call lt2942_get_temperature ; - re-read battery temperature | |
43 ;bra get_battery_voltage_1 ; - proceed | |
582 | 44 |
623 | 45 get_battery_voltage_1: |
46 btfsc divemode ; in dive mode? | |
47 return ; YES - done | |
120 | 48 |
623 | 49 bcf cv_active ; clear CV charing status by default |
50 bcf cc_active ; clear CC charing status ny default | |
51 bcf LEDr ; switch off red LED | |
52 bcf TRISJ,2 ; activate Chrg-Out output | |
53 bsf CHRG_OUT ; start charger | |
54 btfss CHRG_IN ; charging? | |
55 bra charge_cc_active ; YES - charging in CC mode | |
56 bcf CHRG_OUT ; NO - stop charger | |
57 bsf TRISJ,2 ; - set Chrg-Out output to high impedance | |
58 WAITMS d'1' ; - wait 1 ms | |
59 btfsc CHRG_IN ; - still charging? | |
60 return ; NO - done | |
61 ;bra charge_cv_active ; YES - charging in CV mode | |
120 | 62 |
603 | 63 charge_cv_active: |
582 | 64 decfsz get_bat_volt_counter,F |
65 return | |
66 movlw .15 | |
623 | 67 cpfsgt batt_voltage+1 ; battery voltage >= 16*256mV (4.096V) ? |
604 | 68 bra charge_cc_active ; NO |
582 | 69 bsf cc_active |
70 bsf cv_active | |
604 | 71 bsf LEDr ; indicate charging |
72 call lt2942_charge_done ; reset accumulating registers to 0xFFFF | |
582 | 73 WAITMS d'10' |
604 | 74 bcf LEDr ; indicate charging |
582 | 75 bsf get_bat_volt_counter,0 ; =1 |
76 return | |
120 | 77 |
207 | 78 charge_cc_active: |
582 | 79 bsf cc_active |
604 | 80 bsf LEDr ; indicate charging |
582 | 81 bcf CHRG_OUT |
623 | 82 bsf TRISJ,2 ; set chrg-Out output to high impedance |
603 | 83 movlw .15 |
604 | 84 cpfsgt batt_voltage+1 ; battery voltage >= 16*256mV (4.096 V)? |
85 bra charge_cc_active2 ; NO | |
603 | 86 movlw .81 |
604 | 87 cpfslt batt_voltage+0 ; battery voltage >= 80mV (+4096mV from batt_voltage+1)? |
88 bra charge_cv_active ; YES | |
603 | 89 charge_cc_active2: |
582 | 90 movlw .10 |
91 movwf get_bat_volt_counter | |
92 return | |
113 | 93 |
623 | 94 get_battery_voltage_2: ; no gauge IC available, use ADC to measure battery voltage |
95 ; additional charging disable in software | |
96 bsf charge_disable ; set charging-inhibit signal | |
97 bcf charge_enable ; activate charging-inhibit signal | |
615 | 98 |
623 | 99 bsf adc_is_running ; =1: the ADC is in use |
100 movlw b'00100000' ; 2.048 Volt Vref+ -> 1 LSB = 500 µV | |
0 | 101 movwf ADCON1 |
582 | 102 movlw b'00011001' ; power on ADC, select AN6 |
103 rcall wait_adc | |
0 | 104 |
623 | 105 MOVII ADRESL,batt_voltage ; store value |
582 | 106 bcf ADCON0,0 ; power off ADC |
0 | 107 |
604 | 108 ; Multiply with 2.006 to be exact here... |
0 | 109 ; bcf STATUS,C |
110 ; rlcf xA+0,F | |
582 | 111 ; rlcf xA+1,F ; x2 |
623 | 112 ; MOVII xA,batt_voltage ; store value |
0 | 113 |
623 | 114 bcf battery_is_36v ; by default assume it is a 1.5 battery |
115 MOVII batt_voltage,sub_b ; load measured battery voltage | |
0 | 116 |
623 | 117 ; Check if the battery is a 3.6V lithium |
118 MOVLI lithium_36v_low,sub_a ; load threshold for 3.6 Volt lithium battery | |
119 call cmpU16 ; sub_a - sub_b | |
120 btfss neg_flag ; battery voltage > 3.6 V lithium threshold ? | |
121 bra get_battery_voltage_9 ; NO - keep assumption of 1.5V battery | |
122 bsf battery_is_36v ; YES - set flag | |
0 | 123 |
582 | 124 ; Check if the battery is near-dead already |
623 | 125 MOVLI lithium_36v_empty,sub_a ; load threshold for near-dead lithium battery |
126 call cmpU16 ; sub_a - sub_b | |
127 btfsc neg_flag ; battery voltage > dead-battery threshold ? | |
128 bra get_battery_voltage_3 ; YES - battery is still ok | |
129 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used" | |
130 movff WREG,battery_gauge+5 ; - into battery gauge registers | |
0 | 131 |
623 | 132 get_battery_voltage_3: ; 3.6V battery gauge mode |
133 ; SMOVFF "by hand" as the macro does not work with arguments that have a '+something' with them | |
134 bcf trigger_isr_updates ; clear flag, it will be set by the ISR in case it had kicked in | |
0 | 135 movff battery_gauge+5,xC+3 |
136 movff battery_gauge+4,xC+2 | |
137 movff battery_gauge+3,xC+1 | |
138 movff battery_gauge+2,xC+0 | |
623 | 139 btfsc trigger_isr_updates ; did the ISR kicked in since we cleared the flag? |
140 bra get_battery_voltage_3 ; YES - retry copy | |
141 | |
604 | 142 ; battery_gauge: 6 is nAs |
143 ; divide through 65536 | |
144 ; divide through battery_capacity:2 | |
145 ; result is in percent | |
623 | 146 MOVII battery_capacity_internal,xB |
604 | 147 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder |
0 | 148 movff xC+0,lo |
604 | 149 ; limit to 100 |
582 | 150 movlw .100 |
151 cpfslt lo | |
152 movwf lo | |
604 | 153 ; lo will be between 0 (full) and 100 (empty) |
0 | 154 movf lo,W |
155 sublw .100 | |
156 movwf lo | |
623 | 157 get_battery_voltage_4: |
158 movlw .100 ; start with default of 100% | |
159 cpfslt lo ; > 100% | |
160 movwf lo ; YES - limit to 100% | |
604 | 161 ; lo will be between 100 (full) and 0 (empty) |
162 ; use 3.6V battery sensing based on 50 mA load | |
623 | 163 MOVLI lithium_36v_75,sub_a ; load threshold for > 75% |
164 call cmpU16 ; sub_a - sub_b | |
165 btfsc neg_flag ; battery voltage above 75% level? | |
166 bra get_battery_voltage_5 ; YES | |
167 movlw .75 ; NO - set to 75% | |
582 | 168 movwf lo |
623 | 169 get_battery_voltage_5: |
170 ; 50% | |
171 MOVLI lithium_36v_50,sub_a ; load threshold for > 50% | |
172 call cmpU16 ; sub_a - sub_b | |
173 btfsc neg_flag ; battery voltage above 75% level? | |
174 bra get_battery_voltage_6 ; YES | |
175 movlw .50 ; NO - set to 50% | |
582 | 176 movwf lo |
623 | 177 get_battery_voltage_6: |
582 | 178 ; 25% |
623 | 179 MOVLI lithium_36v_25,sub_a ; load threshold for > 25% |
180 call cmpU16 ; sub_a - sub_b | |
181 btfsc neg_flag ; battery voltage above 25% level? | |
182 bra get_battery_voltage_7 ; YES | |
183 movlw .25 ; NO - set to 25% | |
582 | 184 movwf lo |
623 | 185 get_battery_voltage_7: |
582 | 186 ; 10% |
623 | 187 MOVLI lithium_36v_10,sub_a ; load threshold for > 10% |
188 call cmpU16 ; sub_a - sub_b | |
189 btfsc neg_flag ; battery voltage above 10% level? | |
190 bra get_battery_voltage_8 ; YES | |
191 movlw .10 ; NO - set to 10% | |
582 | 192 movwf lo |
623 | 193 get_battery_voltage_8: |
194 movlw .100 ; maximum is 100 (%) | |
195 cpfslt lo ; > 100% ? | |
196 movwf lo ; YES - limit to 100% | |
197 movf batt_percent,W ; get last battery percentage | |
198 cpfsgt lo ; current battery % < last battery % ? | |
199 movff lo,batt_percent ; YES - take new value (keep batt_percent on the lowest value found) | |
200 btfsc battery_is_36v ; using a 3.6 volt battery? | |
201 movff lo,batt_percent ; YES - take new value (always use computed value for 3.6V battery) | |
202 bcf adc_is_running ; done with ADC | |
0 | 203 return |
204 | |
623 | 205 get_battery_voltage_9: |
604 | 206 ; use 1.5V battery voltage mode |
623 | 207 ; use approximation (batt_voltage-aa_15v_low)/4 = lo |
208 MOVII batt_voltage,sub_a ; load battery voltage | |
209 MOVLI aa_15v_low, sub_b ; load offset | |
582 | 210 call subU16 ; sub_c = sub_a - sub_b |
623 | 211 bcf STATUS,C ; shift right to divide / 2 |
582 | 212 rrcf sub_c+1 |
623 | 213 rrcf sub_c+0 |
214 bcf STATUS,C ; another shift right to divide / 4 | |
582 | 215 rrcf sub_c+1 |
623 | 216 rrcf sub_c+0 |
217 movff sub_c+0,lo ; store result | |
218 bra get_battery_voltage_8 ; check limits and return | |
0 | 219 |
623 | 220 |
221 global get_ambient_level ; called from ISR only, in context bank isr_backup | |
628 | 222 get_ambient_level: |
623 | 223 btfsc sleepmode ; in sleep mode? |
224 return ; YES - done | |
225 btfsc adc_is_running ; NO - ADC in use? | |
226 return ; YES - return | |
227 banksel HW_descriptor ; NO - select bank where hardware descriptor is stored | |
228 btfsc ambient_sensor ; - ambient sensor available? | |
229 bra get_ambient_level1 ; YES - use sensor | |
230 banksel isr_backup ; NO - back to ISR default bank | |
231 movff opt_brightness,isr_lo ; - get brightness selection | |
232 incf isr_lo,F ; - 0-2 -> 1-3 | |
233 movlw ambient_light_max_high_cr ; - default selection to brightest setting | |
234 dcfsnz isr_lo,F ; - level 0 (eco) selected? | |
235 movlw ambient_light_max_eco ; YES - select eco brightness | |
236 dcfsnz isr_lo,F ; - level 1 (medium) selected? | |
237 movlw ambient_light_max_medium ; YES - select medium brightness | |
238 movwf ambient_light+0 ; - store selection | |
239 movwf max_CCPR1L ; - store value for dimming in TMR7 interrupt | |
240 return ; - done | |
203 | 241 |
623 | 242 get_ambient_level1: ; using ambient sensor |
243 banksel isr_backup ; back to ISR default bank | |
629 | 244 clrf ADCON1 ; Vref+ = Vdd |
582 | 245 movlw b'00011101' ; power on ADC, select AN7 |
246 rcall wait_adc | |
623 | 247 MOVII ADRESL,ambient_light |
582 | 248 bcf ADCON0,0 ; power off ADC |
0 | 249 |
250 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
604 | 251 ; first: divide by 16 |
0 | 252 bcf STATUS,C |
253 rrcf ambient_light+1 | |
254 rrcf ambient_light+0 | |
255 bcf STATUS,C | |
256 rrcf ambient_light+1 | |
257 rrcf ambient_light+0 | |
258 bcf STATUS,C | |
259 rrcf ambient_light+1 | |
260 rrcf ambient_light+0 | |
261 bcf STATUS,C | |
262 rrcf ambient_light+1 | |
263 rrcf ambient_light+0 | |
604 | 264 ; result: ambient_light:2/16 |
265 ; now make sure to have value between ambient_light_low and ambient_light_max | |
0 | 266 |
582 | 267 movlw .254 |
604 | 268 tstfsz ambient_light+1 ; > 255 ? |
269 movwf ambient_light+0 ; YES - avoid ADC clipping | |
0 | 270 |
604 | 271 incfsz ambient_light+0,W ; = 255 ? |
272 bra get_ambient_level2 ; NO - continue | |
0 | 273 |
582 | 274 movlw .254 |
275 movwf ambient_light+0 ; avoid ADC clipping | |
0 | 276 |
277 get_ambient_level2: | |
582 | 278 ; movlw .10 |
604 | 279 ; subwf ambient_light+0,F ; subtract 10 (ADC Offset) |
582 | 280 ; btfsc STATUS,N |
281 ; movwf ambient_light+0 ; avoid clipping | |
201 | 282 |
623 | 283 movff opt_brightness,isr_lo ; get brightness setting |
0 | 284 |
582 | 285 btfsc RCSTA1,7 ; UART module on? |
623 | 286 clrf isr_lo ; YES - set temporary to eco mode |
287 incf isr_lo,F ; adjust 0-2 to 1-3 | |
0 | 288 |
623 | 289 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting |
0 | 290 |
623 | 291 banksel HW_descriptor ; select bank where hardware descriptor and model variant is stored |
582 | 292 btfss battery_gauge_available |
604 | 293 movlw ambient_light_max_high_15V ; 1.5V battery brightest setting |
294 btfsc battery_is_36v ; 3.6V battery in use? | |
295 movlw ambient_light_max_high_36V ; YES - 3.6V battery brightest setting | |
623 | 296 banksel isr_backup ; back to ISR default bank |
0 | 297 |
623 | 298 dcfsnz isr_lo,F |
299 movlw ambient_light_max_eco ; eco setting | |
300 dcfsnz isr_lo,F | |
582 | 301 movlw ambient_light_max_medium ; brightest setting |
0 | 302 |
582 | 303 incf ambient_light+0,F ; +1 |
604 | 304 cpfslt ambient_light+0 ; smaller than WREG? |
305 movwf ambient_light+0 ; NO - set to max. | |
0 | 306 |
623 | 307 movff opt_brightness,isr_lo |
308 incf isr_lo,F ; adjust 0-2 to 1-3 | |
309 movlw ambient_light_min_high ; darkest setting | |
310 dcfsnz isr_lo,F | |
311 movlw ambient_light_min_eco ; darkest setting | |
312 dcfsnz isr_lo,F | |
313 movlw ambient_light_min_medium ; darkest setting | |
314 dcfsnz isr_lo,F | |
582 | 315 movlw ambient_light_min_high ; darkest setting |
0 | 316 |
604 | 317 cpfsgt ambient_light+0 ; bigger than WREG? |
318 movwf ambient_light+0 ; NO - set to min | |
582 | 319 |
604 | 320 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt |
0 | 321 return |
322 | |
623 | 323 |
324 ;============================================================================= | |
325 ; routines for reading analog-attached external sensors | |
326 ; | |
327 IFDEF _external_sensor | |
328 | |
329 global get_analog_inputs ; called from outside ISR only | |
604 | 330 get_analog_inputs: ; start ADC and wait until finished |
623 | 331 bsf adc_is_running ; =1: the ADC is in use |
582 | 332 btfsc TFT_PWM |
604 | 333 bra get_analog_inputs ; wait for PWM low |
334 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV | |
113 | 335 movwf ADCON1 |
623 | 336 banksel sensor1_mv ; select bank where sensor1_mv is stored |
337 ; Sensor 1 | |
582 | 338 movlw b'00100001' ; power on ADC, select AN8 |
339 rcall wait_adc | |
340 bcf STATUS,C | |
341 rrcf ADRESH,F ; /2 | |
342 rrcf ADRESL,W | |
623 | 343 addwf sensor1_mv+0,F ; add to sensor1_mv:2 |
582 | 344 movf ADRESH,W |
623 | 345 addwfc sensor1_mv+1,F |
582 | 346 bcf STATUS,C |
623 | 347 rrcf sensor1_mv+1,F ; divide /2 |
348 rrcf sensor1_mv+0,F | |
582 | 349 movlw HIGH ignore_mv |
623 | 350 cpfsgt sensor1_mv+1 ; > ignore_mv ? |
351 bra get_analog_inputs1a ; NO | |
352 CLRI sensor1_mv ; YES - ignore this reading | |
353 get_analog_inputs1a: ; ignore 1.9 mV noise for not-connected inputs | |
354 tstfsz sensor1_mv+1 ; > 25.5 mV ? | |
604 | 355 bra get_analog_inputs2 ; YES - skip here |
582 | 356 movlw .19 |
623 | 357 cpfsgt sensor1_mv+0 ; > 1.9 mV ? |
358 clrf sensor1_mv+0 ; NO - clear result | |
359 | |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
360 get_analog_inputs2: |
623 | 361 ; Sensor 2 |
582 | 362 movlw b'00100101' ; power on ADC, select AN9 |
363 rcall wait_adc | |
364 bcf STATUS,C | |
365 rrcf ADRESH,F ; /2 | |
366 rrcf ADRESL,W | |
623 | 367 addwf sensor2_mv+0,F ; add to sensor2_mv:2 |
582 | 368 movf ADRESH,W |
623 | 369 addwfc sensor2_mv+1,F |
582 | 370 bcf STATUS,C |
623 | 371 rrcf sensor2_mv+1,F ; divide /2 |
372 rrcf sensor2_mv+0,F | |
582 | 373 movlw HIGH ignore_mv |
623 | 374 cpfsgt sensor2_mv+1 ; > ignore_mv ? |
375 bra get_analog_inputs2a ; NO | |
376 CLRI sensor2_mv ; YES - ignore this reading | |
377 get_analog_inputs2a: ; ignore 1.9 mV noise for not-connected inputs | |
378 tstfsz sensor2_mv+1 ; > 25.5 mV ? | |
604 | 379 bra get_analog_inputs3 ; YES - skip here |
582 | 380 movlw .19 |
623 | 381 cpfsgt sensor2_mv+0 ; > 1.9 mV ? |
382 clrf sensor2_mv+0 ; NO - clear result | |
383 | |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
384 get_analog_inputs3: |
623 | 385 ; Sensor 3 |
582 | 386 movlw b'00101001' ; power on ADC, select AN10 |
387 rcall wait_adc | |
388 bcf STATUS,C | |
389 rrcf ADRESH,F ; /2 | |
390 rrcf ADRESL,W | |
623 | 391 addwf sensor3_mv+0,F ; add to sensor3_mv:2 |
582 | 392 movf ADRESH,W |
623 | 393 addwfc sensor3_mv+1,F |
582 | 394 bcf STATUS,C |
623 | 395 rrcf sensor3_mv+1,F ; divide /2 |
396 rrcf sensor3_mv+0,F | |
582 | 397 movlw HIGH ignore_mv |
623 | 398 cpfsgt sensor3_mv+1 ; > ignore_mv ? |
399 bra get_analog_inputs3a ; NO | |
400 CLRI sensor3_mv ; YES - ignore this reading | |
401 get_analog_inputs3a: ; ignore 1.9 mV noise for not-connected inputs | |
402 tstfsz sensor3_mv+1 ; > 25.5 mV ? | |
604 | 403 bra get_analog_inputs4 ; YES - skip here |
582 | 404 movlw .19 |
623 | 405 cpfsgt sensor3_mv+0 ; > 1.9 mV ? |
406 clrf sensor3_mv+0 ; NO - clear result | |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
407 get_analog_inputs4: |
623 | 408 banksel common ; back to bank common |
582 | 409 bcf ADCON0,0 ; power off ADC |
623 | 410 bcf adc_is_running ; done with ADC |
582 | 411 return |
0 | 412 |
623 | 413 ENDIF ; _external_sensor |
414 | |
415 ;============================================================================= | |
416 | |
417 global piezo_config ; called from outside ISR only | |
418 piezo_config: ; set up sensitivity of heinrichsweikamp piezo buttons (~30ms) | |
419 clrf TMR5H ; clear TMR5H first | |
420 clrf TMR5L ; clear TMR5L thereafter | |
421 bcf PIR5,TMR5IF ; clear timer 5 overrun flag, will take ~ 2 seconds to overrun after reset | |
422 bcf switch_right ; clear left-over button events | |
423 bcf switch_left ; ... | |
416
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
424 piezo_config0: |
623 | 425 btfsc switch_right ; user still pressing the right button? |
426 bra piezo_config ; YES - loop to wait for release | |
427 btfsc switch_left ; user still pressing the left button? | |
428 bra piezo_config ; YES - loop to wait for release | |
582 | 429 |
623 | 430 btfss PIR5,TMR5IF ; timeout? |
431 bra piezo_config0 ; NO - not yet, loop | |
468 | 432 |
582 | 433 bcf INTCON,GIE |
604 | 434 |
582 | 435 movff opt_cR_button_right,WREG ; right button |
436 btfsc flip_screen ; 180° rotation ? | |
604 | 437 movff opt_cR_button_left,WREG ; YES - left button |
582 | 438 rcall piezo_config_tx |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
439 |
582 | 440 movff opt_cR_button_left,WREG ; left button |
441 btfsc flip_screen ; 180° rotation ? | |
604 | 442 movff opt_cR_button_right,WREG ; YES - right button |
582 | 443 rcall piezo_config_tx |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
444 |
582 | 445 movlw .20 ; reserved |
446 rcall piezo_config_tx | |
447 movlw .20 ; reserved | |
448 rcall piezo_config_tx | |
604 | 449 |
582 | 450 bsf INTCON,GIE |
451 return | |
204 | 452 |
582 | 453 piezo_config_tx: ; send one byte |
454 movwf lo ; store byte | |
455 movlw .8 | |
456 movwf hi ; bit counter | |
457 bcf TX3_PIEZO_CFG ; start bit | |
458 rcall piezo_config_wait_bit | |
113 | 459 piezo_config_tx_loop: |
582 | 460 btfss lo,0 ; LSB first |
461 bcf TX3_PIEZO_CFG | |
462 btfsc lo,0 ; LSB first | |
463 bsf TX3_PIEZO_CFG | |
464 rcall piezo_config_wait_bit | |
465 rrncf lo,F | |
466 decfsz hi,F | |
467 bra piezo_config_tx_loop | |
468 bsf TX3_PIEZO_CFG ; stop bit | |
469 rcall piezo_config_wait_bit | |
470 return | |
113 | 471 |
472 piezo_config_wait_bit: | |
623 | 473 setf TMR5H ; set TMR5H first (to 255) |
474 movlw .255-.26 ; 26 x 31.5 µs = 819 us | |
475 movwf TMR5L ; set TMR5L thereafter | |
476 bcf PIR5,TMR5IF ; clear timer 5 overrun flag | |
113 | 477 piezo_config_wait_bit3: |
623 | 478 btfss PIR5,TMR5IF ; timeout? |
479 bra piezo_config_wait_bit3 ; NO - loop | |
480 return ; YES - done | |
582 | 481 |
482 | |
623 | 483 global get_analog_switches ; called from ISR and from sleep mode, returns in bank common |
604 | 484 get_analog_switches: ; start ADC and wait until finished |
623 | 485 banksel HW_variants ; switch to bank where OSTC model variant is stored |
604 | 486 btfsc analog_switches ; does the OSTC have analog switches? |
623 | 487 bra get_analog_switches_1 ; YES |
629 | 488 get_analog_switches0: |
623 | 489 banksel common ; NO - back to bank common |
490 bcf analog_sw1_pressed ; - clear flag for analog switch 1 | |
604 | 491 bcf analog_sw2_pressed ; - clear flag for analog switch 2 |
492 return ; - done | |
623 | 493 |
494 get_analog_switches_1: | |
495 banksel common ; back to bank common | |
496 btfsc adc_is_running ; ADC in use? | |
604 | 497 return ; YES - abort |
628 | 498 btfsc cc_active ; NO - charging? |
499 bra get_analog_switches0 ; YES - abort (And clear both flags) | |
500 ;bra get_analog_switches_2 ; NO - read switches | |
623 | 501 |
502 get_analog_switches_2: | |
628 | 503 bsf adc_is_running |
629 | 504 bcf ADCON2,ADFM ; left justified |
582 | 505 clrf ADCON1 |
506 movlw b'00100101' ; power on ADC, select AN9 | |
507 rcall wait_adc | |
623 | 508 |
509 banksel isr_backup ; select bank ISR data | |
582 | 510 movff ADRESH,WREG |
511 addwf analog_sw2_raw+0 | |
512 movlw .0 | |
513 addwfc analog_sw2_raw+1 | |
514 decfsz analog_counter,F ; continue averaging? | |
623 | 515 bra get_analog_switches_2a ; YES |
516 bcf STATUS,C ; NO - done, compute average | |
582 | 517 rrcf analog_sw2_raw+1 |
518 rrcf analog_sw2_raw+0 ; /2 | |
519 bcf STATUS,C | |
520 rrcf analog_sw2_raw+1 | |
521 rrcf analog_sw2_raw+0 ; /4 | |
522 bcf STATUS,C | |
523 rrcf analog_sw2_raw+1 | |
524 rrcf analog_sw2_raw+0 ; /8 | |
525 bcf STATUS,C | |
526 rrcf analog_sw2_raw+1 | |
527 rrcf analog_sw2_raw+0 ; /16 | |
528 movff analog_sw2_raw+0,analog_sw2 | |
529 clrf analog_sw2_raw+1 | |
604 | 530 clrf analog_sw2_raw+0 ; reset average registers |
582 | 531 ; movlw .16 |
532 ; movwf analog_counter ; only once... | |
623 | 533 |
534 get_analog_switches_2a: | |
535 bcf analog_sw2_pressed ; default to not pressed | |
604 | 536 movff opt_cR_button_left,WREG ; 20-100 |
582 | 537 bcf STATUS,C |
604 | 538 rrcf WREG ; /2 -> 10-50 |
582 | 539 bcf STATUS,C |
604 | 540 rrcf WREG ; /2 -> 5-25 |
541 decf WREG,W ; -1 | |
542 decf WREG,W ; -1 | |
543 decf WREG,W ; -1 -> 2-22 | |
544 btfss button_polarity,1 ; (1= normal, 0=inverted) | |
623 | 545 bra get_analog_switches_sw2_inv |
582 | 546 addwf analog_sw2,W ; average (~128) |
623 | 547 cpfsgt ADRESH ; pressed? |
548 bra get_analog_switches_3 ; NO | |
549 bra get_analog_switches_sw2_pressed ; YES (left button normal) | |
550 | |
551 get_analog_switches_sw2_inv: | |
582 | 552 subwf analog_sw2,W ; average (~128) |
623 | 553 cpfslt ADRESH ; pressed? |
554 bra get_analog_switches_3 ; NO | |
555 ;bra get_analog_switches_sw2_pressed ; YES (left button inverted) | |
556 | |
557 get_analog_switches_sw2_pressed: | |
558 bsf analog_sw2_pressed ; set left button as pressed | |
559 | |
560 get_analog_switches_3: | |
582 | 561 movlw b'00101001' ; power on ADC, select AN10 |
562 rcall wait_adc | |
563 movff ADRESH,WREG | |
564 addwf analog_sw1_raw+0 | |
565 movlw .0 | |
566 addwfc analog_sw1_raw+1 | |
567 tstfsz analog_counter ; continue averaging? | |
623 | 568 bra get_analog_switches_3a ; YES |
569 bcf STATUS,C ; NO - done, compute average | |
582 | 570 rrcf analog_sw1_raw+1 |
571 rrcf analog_sw1_raw+0 ; /2 | |
572 bcf STATUS,C | |
573 rrcf analog_sw1_raw+1 | |
574 rrcf analog_sw1_raw+0 ; /4 | |
575 bcf STATUS,C | |
576 rrcf analog_sw1_raw+1 | |
577 rrcf analog_sw1_raw+0 ; /8 | |
578 bcf STATUS,C | |
579 rrcf analog_sw1_raw+1 | |
580 rrcf analog_sw1_raw+0 ; /16 | |
581 movff analog_sw1_raw+0,analog_sw1 | |
582 clrf analog_sw1_raw+1 | |
604 | 583 clrf analog_sw1_raw+0 ; reset average registers |
582 | 584 movlw .16 |
585 movwf analog_counter ; only once... | |
623 | 586 |
587 get_analog_switches_3a: | |
588 bcf analog_sw1_pressed ; default to not pressed | |
604 | 589 movff opt_cR_button_right,WREG ; 20-100 |
582 | 590 bcf STATUS,C |
604 | 591 rrcf WREG ; /2 -> 10-50 |
582 | 592 bcf STATUS,C |
604 | 593 rrcf WREG ; /2 -> 5-25 |
594 decf WREG,W ; -1 | |
595 decf WREG,W ; -1 | |
596 decf WREG,W ; -1 -> 2-22 | |
597 btfss button_polarity,0 ; (1= normal, 0=inverted) | |
623 | 598 bra get_analog_switches_sw1_inv |
582 | 599 addwf analog_sw1,W ; average (~128) |
623 | 600 cpfsgt ADRESH ; pressed? |
601 bra get_analog_switches_4 ; NO | |
602 bra get_analog_switches_sw1_pressed ; YES (right button normal) | |
603 | |
604 get_analog_switches_sw1_inv: | |
582 | 605 subwf analog_sw1,W ; average (~128) |
623 | 606 cpfslt ADRESH ; pressed? |
607 bra get_analog_switches_4 ; NO | |
608 ;bra get_analog_switches_sw1_pressed ; YES (right button inverted) | |
609 | |
610 get_analog_switches_sw1_pressed: | |
611 bsf analog_sw1_pressed ; set right button as pressed | |
612 | |
613 get_analog_switches_4: | |
629 | 614 bsf ADCON2,ADFM ; restore to right justified |
628 | 615 bcf adc_is_running |
623 | 616 banksel common ; back to bank common |
617 btfsc analog_sw1_pressed ; right button pressed? | |
618 return ; YES - done | |
619 btfsc analog_sw2_pressed ; left button pressed? | |
620 return ; YES - done | |
621 setf TMR1H ; NO to both - no button pressed, set timer1 to overflow quickly | |
582 | 622 return |
448 | 623 |
0 | 624 END |