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