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