Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 623:c40025d8e750
3.03 beta released
author | heinrichsweikamp |
---|---|
date | Mon, 03 Jun 2019 14:01:48 +0200 |
parents | b87f23fae743 |
children | cd58f7fc86db |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
623 | 3 ; File adc_lightsensor.asm combined next generation V3.03.2 |
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 | |
582 | 222 get_ambient_level: ; starts ADC and waits until finished |
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 | |
582 | 244 movlw b'00000000' ; Vref+ = Vdd |
0 | 245 movwf ADCON1 |
582 | 246 movlw b'00011101' ; power on ADC, select AN7 |
247 rcall wait_adc | |
623 | 248 MOVII ADRESL,ambient_light |
582 | 249 bcf ADCON0,0 ; power off ADC |
0 | 250 |
251 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
604 | 252 ; first: divide by 16 |
0 | 253 bcf STATUS,C |
254 rrcf ambient_light+1 | |
255 rrcf ambient_light+0 | |
256 bcf STATUS,C | |
257 rrcf ambient_light+1 | |
258 rrcf ambient_light+0 | |
259 bcf STATUS,C | |
260 rrcf ambient_light+1 | |
261 rrcf ambient_light+0 | |
262 bcf STATUS,C | |
263 rrcf ambient_light+1 | |
264 rrcf ambient_light+0 | |
604 | 265 ; result: ambient_light:2/16 |
266 ; now make sure to have value between ambient_light_low and ambient_light_max | |
0 | 267 |
582 | 268 movlw .254 |
604 | 269 tstfsz ambient_light+1 ; > 255 ? |
270 movwf ambient_light+0 ; YES - avoid ADC clipping | |
0 | 271 |
604 | 272 incfsz ambient_light+0,W ; = 255 ? |
273 bra get_ambient_level2 ; NO - continue | |
0 | 274 |
582 | 275 movlw .254 |
276 movwf ambient_light+0 ; avoid ADC clipping | |
0 | 277 |
278 get_ambient_level2: | |
582 | 279 ; movlw .10 |
604 | 280 ; subwf ambient_light+0,F ; subtract 10 (ADC Offset) |
582 | 281 ; btfsc STATUS,N |
282 ; movwf ambient_light+0 ; avoid clipping | |
201 | 283 |
623 | 284 movff opt_brightness,isr_lo ; get brightness setting |
0 | 285 |
582 | 286 btfsc RCSTA1,7 ; UART module on? |
623 | 287 clrf isr_lo ; YES - set temporary to eco mode |
288 incf isr_lo,F ; adjust 0-2 to 1-3 | |
0 | 289 |
623 | 290 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting |
0 | 291 |
623 | 292 banksel HW_descriptor ; select bank where hardware descriptor and model variant is stored |
582 | 293 btfss battery_gauge_available |
604 | 294 movlw ambient_light_max_high_15V ; 1.5V battery brightest setting |
295 btfsc battery_is_36v ; 3.6V battery in use? | |
296 movlw ambient_light_max_high_36V ; YES - 3.6V battery brightest setting | |
623 | 297 banksel isr_backup ; back to ISR default bank |
0 | 298 |
623 | 299 dcfsnz isr_lo,F |
300 movlw ambient_light_max_eco ; eco setting | |
301 dcfsnz isr_lo,F | |
582 | 302 movlw ambient_light_max_medium ; brightest setting |
0 | 303 |
582 | 304 incf ambient_light+0,F ; +1 |
604 | 305 cpfslt ambient_light+0 ; smaller than WREG? |
306 movwf ambient_light+0 ; NO - set to max. | |
0 | 307 |
623 | 308 movff opt_brightness,isr_lo |
309 incf isr_lo,F ; adjust 0-2 to 1-3 | |
310 movlw ambient_light_min_high ; darkest setting | |
311 dcfsnz isr_lo,F | |
312 movlw ambient_light_min_eco ; darkest setting | |
313 dcfsnz isr_lo,F | |
314 movlw ambient_light_min_medium ; darkest setting | |
315 dcfsnz isr_lo,F | |
582 | 316 movlw ambient_light_min_high ; darkest setting |
0 | 317 |
604 | 318 cpfsgt ambient_light+0 ; bigger than WREG? |
319 movwf ambient_light+0 ; NO - set to min | |
582 | 320 |
604 | 321 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt |
0 | 322 return |
323 | |
623 | 324 |
325 ;============================================================================= | |
326 ; routines for reading analog-attached external sensors | |
327 ; | |
328 IFDEF _external_sensor | |
329 | |
330 global get_analog_inputs ; called from outside ISR only | |
604 | 331 get_analog_inputs: ; start ADC and wait until finished |
623 | 332 bsf adc_is_running ; =1: the ADC is in use |
582 | 333 btfsc TFT_PWM |
604 | 334 bra get_analog_inputs ; wait for PWM low |
335 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV | |
113 | 336 movwf ADCON1 |
623 | 337 banksel sensor1_mv ; select bank where sensor1_mv is stored |
338 ; Sensor 1 | |
582 | 339 movlw b'00100001' ; power on ADC, select AN8 |
340 rcall wait_adc | |
341 bcf STATUS,C | |
342 rrcf ADRESH,F ; /2 | |
343 rrcf ADRESL,W | |
623 | 344 addwf sensor1_mv+0,F ; add to sensor1_mv:2 |
582 | 345 movf ADRESH,W |
623 | 346 addwfc sensor1_mv+1,F |
582 | 347 bcf STATUS,C |
623 | 348 rrcf sensor1_mv+1,F ; divide /2 |
349 rrcf sensor1_mv+0,F | |
582 | 350 movlw HIGH ignore_mv |
623 | 351 cpfsgt sensor1_mv+1 ; > ignore_mv ? |
352 bra get_analog_inputs1a ; NO | |
353 CLRI sensor1_mv ; YES - ignore this reading | |
354 get_analog_inputs1a: ; ignore 1.9 mV noise for not-connected inputs | |
355 tstfsz sensor1_mv+1 ; > 25.5 mV ? | |
604 | 356 bra get_analog_inputs2 ; YES - skip here |
582 | 357 movlw .19 |
623 | 358 cpfsgt sensor1_mv+0 ; > 1.9 mV ? |
359 clrf sensor1_mv+0 ; NO - clear result | |
360 | |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
361 get_analog_inputs2: |
623 | 362 ; Sensor 2 |
582 | 363 movlw b'00100101' ; power on ADC, select AN9 |
364 rcall wait_adc | |
365 bcf STATUS,C | |
366 rrcf ADRESH,F ; /2 | |
367 rrcf ADRESL,W | |
623 | 368 addwf sensor2_mv+0,F ; add to sensor2_mv:2 |
582 | 369 movf ADRESH,W |
623 | 370 addwfc sensor2_mv+1,F |
582 | 371 bcf STATUS,C |
623 | 372 rrcf sensor2_mv+1,F ; divide /2 |
373 rrcf sensor2_mv+0,F | |
582 | 374 movlw HIGH ignore_mv |
623 | 375 cpfsgt sensor2_mv+1 ; > ignore_mv ? |
376 bra get_analog_inputs2a ; NO | |
377 CLRI sensor2_mv ; YES - ignore this reading | |
378 get_analog_inputs2a: ; ignore 1.9 mV noise for not-connected inputs | |
379 tstfsz sensor2_mv+1 ; > 25.5 mV ? | |
604 | 380 bra get_analog_inputs3 ; YES - skip here |
582 | 381 movlw .19 |
623 | 382 cpfsgt sensor2_mv+0 ; > 1.9 mV ? |
383 clrf sensor2_mv+0 ; NO - clear result | |
384 | |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
385 get_analog_inputs3: |
623 | 386 ; Sensor 3 |
582 | 387 movlw b'00101001' ; power on ADC, select AN10 |
388 rcall wait_adc | |
389 bcf STATUS,C | |
390 rrcf ADRESH,F ; /2 | |
391 rrcf ADRESL,W | |
623 | 392 addwf sensor3_mv+0,F ; add to sensor3_mv:2 |
582 | 393 movf ADRESH,W |
623 | 394 addwfc sensor3_mv+1,F |
582 | 395 bcf STATUS,C |
623 | 396 rrcf sensor3_mv+1,F ; divide /2 |
397 rrcf sensor3_mv+0,F | |
582 | 398 movlw HIGH ignore_mv |
623 | 399 cpfsgt sensor3_mv+1 ; > ignore_mv ? |
400 bra get_analog_inputs3a ; NO | |
401 CLRI sensor3_mv ; YES - ignore this reading | |
402 get_analog_inputs3a: ; ignore 1.9 mV noise for not-connected inputs | |
403 tstfsz sensor3_mv+1 ; > 25.5 mV ? | |
604 | 404 bra get_analog_inputs4 ; YES - skip here |
582 | 405 movlw .19 |
623 | 406 cpfsgt sensor3_mv+0 ; > 1.9 mV ? |
407 clrf sensor3_mv+0 ; NO - clear result | |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
408 get_analog_inputs4: |
623 | 409 banksel common ; back to bank common |
582 | 410 bcf ADCON0,0 ; power off ADC |
623 | 411 bcf adc_is_running ; done with ADC |
582 | 412 return |
0 | 413 |
623 | 414 ENDIF ; _external_sensor |
415 | |
416 ;============================================================================= | |
417 | |
418 global piezo_config ; called from outside ISR only | |
419 piezo_config: ; set up sensitivity of heinrichsweikamp piezo buttons (~30ms) | |
420 clrf TMR5H ; clear TMR5H first | |
421 clrf TMR5L ; clear TMR5L thereafter | |
422 bcf PIR5,TMR5IF ; clear timer 5 overrun flag, will take ~ 2 seconds to overrun after reset | |
423 bcf switch_right ; clear left-over button events | |
424 bcf switch_left ; ... | |
416
4389fe9673b2
NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents:
357
diff
changeset
|
425 piezo_config0: |
623 | 426 btfsc switch_right ; user still pressing the right button? |
427 bra piezo_config ; YES - loop to wait for release | |
428 btfsc switch_left ; user still pressing the left button? | |
429 bra piezo_config ; YES - loop to wait for release | |
582 | 430 |
623 | 431 btfss PIR5,TMR5IF ; timeout? |
432 bra piezo_config0 ; NO - not yet, loop | |
468 | 433 |
582 | 434 bcf INTCON,GIE |
604 | 435 |
582 | 436 movff opt_cR_button_right,WREG ; right button |
437 btfsc flip_screen ; 180° rotation ? | |
604 | 438 movff opt_cR_button_left,WREG ; YES - left button |
582 | 439 rcall piezo_config_tx |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
440 |
582 | 441 movff opt_cR_button_left,WREG ; left button |
442 btfsc flip_screen ; 180° rotation ? | |
604 | 443 movff opt_cR_button_right,WREG ; YES - right button |
582 | 444 rcall piezo_config_tx |
343
34e2f5bdc5a2
BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents:
275
diff
changeset
|
445 |
582 | 446 movlw .20 ; reserved |
447 rcall piezo_config_tx | |
448 movlw .20 ; reserved | |
449 rcall piezo_config_tx | |
604 | 450 |
582 | 451 bsf INTCON,GIE |
452 return | |
204 | 453 |
582 | 454 piezo_config_tx: ; send one byte |
455 movwf lo ; store byte | |
456 movlw .8 | |
457 movwf hi ; bit counter | |
458 bcf TX3_PIEZO_CFG ; start bit | |
459 rcall piezo_config_wait_bit | |
113 | 460 piezo_config_tx_loop: |
582 | 461 btfss lo,0 ; LSB first |
462 bcf TX3_PIEZO_CFG | |
463 btfsc lo,0 ; LSB first | |
464 bsf TX3_PIEZO_CFG | |
465 rcall piezo_config_wait_bit | |
466 rrncf lo,F | |
467 decfsz hi,F | |
468 bra piezo_config_tx_loop | |
469 bsf TX3_PIEZO_CFG ; stop bit | |
470 rcall piezo_config_wait_bit | |
471 return | |
113 | 472 |
473 piezo_config_wait_bit: | |
623 | 474 setf TMR5H ; set TMR5H first (to 255) |
475 movlw .255-.26 ; 26 x 31.5 µs = 819 us | |
476 movwf TMR5L ; set TMR5L thereafter | |
477 bcf PIR5,TMR5IF ; clear timer 5 overrun flag | |
113 | 478 piezo_config_wait_bit3: |
623 | 479 btfss PIR5,TMR5IF ; timeout? |
480 bra piezo_config_wait_bit3 ; NO - loop | |
481 return ; YES - done | |
582 | 482 |
483 | |
623 | 484 global get_analog_switches ; called from ISR and from sleep mode, returns in bank common |
604 | 485 get_analog_switches: ; start ADC and wait until finished |
623 | 486 banksel HW_variants ; switch to bank where OSTC model variant is stored |
604 | 487 btfsc analog_switches ; does the OSTC have analog switches? |
623 | 488 bra get_analog_switches_1 ; YES |
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 |
623 | 498 ;bra get_analog_switches_2 ; NO - read switches |
499 | |
500 get_analog_switches_2: | |
582 | 501 movlw b'00001001' ; left justified |
502 movwf ADCON2 | |
503 ; movlw b'00000000' ; Vref+ = Vdd | |
504 clrf ADCON1 | |
505 movlw b'00100101' ; power on ADC, select AN9 | |
506 rcall wait_adc | |
623 | 507 |
508 banksel isr_backup ; select bank ISR data | |
582 | 509 movff ADRESH,WREG |
510 addwf analog_sw2_raw+0 | |
511 movlw .0 | |
512 addwfc analog_sw2_raw+1 | |
513 decfsz analog_counter,F ; continue averaging? | |
623 | 514 bra get_analog_switches_2a ; YES |
515 bcf STATUS,C ; NO - done, compute average | |
582 | 516 rrcf analog_sw2_raw+1 |
517 rrcf analog_sw2_raw+0 ; /2 | |
518 bcf STATUS,C | |
519 rrcf analog_sw2_raw+1 | |
520 rrcf analog_sw2_raw+0 ; /4 | |
521 bcf STATUS,C | |
522 rrcf analog_sw2_raw+1 | |
523 rrcf analog_sw2_raw+0 ; /8 | |
524 bcf STATUS,C | |
525 rrcf analog_sw2_raw+1 | |
526 rrcf analog_sw2_raw+0 ; /16 | |
527 movff analog_sw2_raw+0,analog_sw2 | |
528 clrf analog_sw2_raw+1 | |
604 | 529 clrf analog_sw2_raw+0 ; reset average registers |
582 | 530 ; movlw .16 |
531 ; movwf analog_counter ; only once... | |
623 | 532 |
533 get_analog_switches_2a: | |
534 bcf analog_sw2_pressed ; default to not pressed | |
604 | 535 movff opt_cR_button_left,WREG ; 20-100 |
582 | 536 bcf STATUS,C |
604 | 537 rrcf WREG ; /2 -> 10-50 |
582 | 538 bcf STATUS,C |
604 | 539 rrcf WREG ; /2 -> 5-25 |
540 decf WREG,W ; -1 | |
541 decf WREG,W ; -1 | |
542 decf WREG,W ; -1 -> 2-22 | |
543 btfss button_polarity,1 ; (1= normal, 0=inverted) | |
623 | 544 bra get_analog_switches_sw2_inv |
582 | 545 addwf analog_sw2,W ; average (~128) |
623 | 546 cpfsgt ADRESH ; pressed? |
547 bra get_analog_switches_3 ; NO | |
548 bra get_analog_switches_sw2_pressed ; YES (left button normal) | |
549 | |
550 get_analog_switches_sw2_inv: | |
582 | 551 subwf analog_sw2,W ; average (~128) |
623 | 552 cpfslt ADRESH ; pressed? |
553 bra get_analog_switches_3 ; NO | |
554 ;bra get_analog_switches_sw2_pressed ; YES (left button inverted) | |
555 | |
556 get_analog_switches_sw2_pressed: | |
557 bsf analog_sw2_pressed ; set left button as pressed | |
558 | |
559 get_analog_switches_3: | |
582 | 560 movlw b'00101001' ; power on ADC, select AN10 |
561 rcall wait_adc | |
562 movff ADRESH,WREG | |
563 addwf analog_sw1_raw+0 | |
564 movlw .0 | |
565 addwfc analog_sw1_raw+1 | |
566 tstfsz analog_counter ; continue averaging? | |
623 | 567 bra get_analog_switches_3a ; YES |
568 bcf STATUS,C ; NO - done, compute average | |
582 | 569 rrcf analog_sw1_raw+1 |
570 rrcf analog_sw1_raw+0 ; /2 | |
571 bcf STATUS,C | |
572 rrcf analog_sw1_raw+1 | |
573 rrcf analog_sw1_raw+0 ; /4 | |
574 bcf STATUS,C | |
575 rrcf analog_sw1_raw+1 | |
576 rrcf analog_sw1_raw+0 ; /8 | |
577 bcf STATUS,C | |
578 rrcf analog_sw1_raw+1 | |
579 rrcf analog_sw1_raw+0 ; /16 | |
580 movff analog_sw1_raw+0,analog_sw1 | |
581 clrf analog_sw1_raw+1 | |
604 | 582 clrf analog_sw1_raw+0 ; reset average registers |
582 | 583 movlw .16 |
584 movwf analog_counter ; only once... | |
623 | 585 |
586 get_analog_switches_3a: | |
587 bcf analog_sw1_pressed ; default to not pressed | |
604 | 588 movff opt_cR_button_right,WREG ; 20-100 |
582 | 589 bcf STATUS,C |
604 | 590 rrcf WREG ; /2 -> 10-50 |
582 | 591 bcf STATUS,C |
604 | 592 rrcf WREG ; /2 -> 5-25 |
593 decf WREG,W ; -1 | |
594 decf WREG,W ; -1 | |
595 decf WREG,W ; -1 -> 2-22 | |
596 btfss button_polarity,0 ; (1= normal, 0=inverted) | |
623 | 597 bra get_analog_switches_sw1_inv |
582 | 598 addwf analog_sw1,W ; average (~128) |
623 | 599 cpfsgt ADRESH ; pressed? |
600 bra get_analog_switches_4 ; NO | |
601 bra get_analog_switches_sw1_pressed ; YES (right button normal) | |
602 | |
603 get_analog_switches_sw1_inv: | |
582 | 604 subwf analog_sw1,W ; average (~128) |
623 | 605 cpfslt ADRESH ; pressed? |
606 bra get_analog_switches_4 ; NO | |
607 ;bra get_analog_switches_sw1_pressed ; YES (right button inverted) | |
608 | |
609 get_analog_switches_sw1_pressed: | |
610 bsf analog_sw1_pressed ; set right button as pressed | |
611 | |
612 get_analog_switches_4: | |
604 | 613 movlw b'10001101' ; restore to right justified |
582 | 614 movwf ADCON2 |
623 | 615 |
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 |