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