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