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