Mercurial > public > hwos_code
comparison src/adc_lightsensor.asm @ 634:4050675965ea
3.10 stable release
| author | heinrichsweikamp |
|---|---|
| date | Tue, 28 Apr 2020 17:34:31 +0200 |
| parents | 185ba2f91f59 |
| children | 2737ddc643bb |
comparison
equal
deleted
inserted
replaced
| 633:690c48db7b5b | 634:4050675965ea |
|---|---|
| 1 ;============================================================================= | 1 ;============================================================================= |
| 2 ; | 2 ; |
| 3 ; File adc_lightsensor.asm combined next generation V3.08.8 | 3 ; File adc_lightsensor.asm * combined next generation V3.09.4e |
| 4 ; | 4 ; |
| 5 ; | 5 ; |
| 6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | 6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. |
| 7 ;============================================================================= | 7 ;============================================================================= |
| 8 ; HISTORY | 8 ; HISTORY |
| 13 #include "wait.inc" | 13 #include "wait.inc" |
| 14 #include "eeprom_rs232.inc" | 14 #include "eeprom_rs232.inc" |
| 15 #include "i2c.inc" | 15 #include "i2c.inc" |
| 16 | 16 |
| 17 | 17 |
| 18 ;============================================================================= | |
| 18 adc_light CODE | 19 adc_light CODE |
| 19 | |
| 20 ;============================================================================= | 20 ;============================================================================= |
| 21 | 21 |
| 22 wait_adc: ; bank-safe | 22 |
| 23 ;----------------------------------------------------------------------------- | |
| 24 ; Helper Function - wait on ADC to finish conversion | |
| 25 ; | |
| 26 ; bank safe | |
| 27 ; | |
| 28 wait_adc: | |
| 23 movwf ADCON0 ; select ADC channel | 29 movwf ADCON0 ; select ADC channel |
| 24 nop ; wait a short moment | 30 nop ; wait a short moment |
| 31 nop ; ... | |
| 32 nop ; ..... | |
| 33 nop ; ....... | |
| 25 bsf ADCON0,1 ; start ADC | 34 bsf ADCON0,1 ; start ADC |
| 26 wait_adc2: | 35 wait_adc2: |
| 27 btfsc ADCON0,1 ; ADC done? | 36 btfsc ADCON0,1 ; ADC done? |
| 28 bra wait_adc2 ; NO - wait | 37 bra wait_adc2 ; NO - wait |
| 29 return ; YES - done | 38 return ; YES - done |
| 30 | 39 |
| 40 | |
| 41 ;----------------------------------------------------------------------------- | |
| 42 ; get Battery Voltage | |
| 43 ; | |
| 31 global get_battery_voltage | 44 global get_battery_voltage |
| 32 get_battery_voltage: ; start ADC and wait until finished | 45 get_battery_voltage: |
| 33 btfss battery_gauge_available ; battery gauge IC available? | 46 btfss battery_gauge_available ; battery gauge IC available? |
| 34 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC | 47 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC |
| 35 bsf battery_is_36v ; YES - gauge IC available, 3.6V battery | 48 bsf battery_is_36v ; YES - gauge IC available, 3.6V battery |
| 36 call lt2942_get_accumulated_charge ; - read coulomb counter | 49 call lt2942_get_accumulated_charge ; - read coulomb counter |
| 37 call lt2942_get_voltage ; - read battery voltage | 50 call lt2942_get_voltage ; - read battery voltage |
| 104 movlw b'00011001' ; power on ADC, select AN6 | 117 movlw b'00011001' ; power on ADC, select AN6 |
| 105 rcall wait_adc ; take measurement | 118 rcall wait_adc ; take measurement |
| 106 MOVII ADRESL,batt_voltage ; store value | 119 MOVII ADRESL,batt_voltage ; store value |
| 107 bcf ADCON0,0 ; power off ADC | 120 bcf ADCON0,0 ; power off ADC |
| 108 | 121 |
| 109 ; Multiply with 2.006 to be exact here... | 122 ; ; multiply with 2.006 to be exact here... |
| 110 ; bcf STATUS,C | 123 ; bcf STATUS,C |
| 111 ; rlcf xA+0,F | 124 ; rlcf xA+0,F |
| 112 ; rlcf xA+1,F ; x2 | 125 ; rlcf xA+1,F ; x2 |
| 113 ; MOVII xA,batt_voltage ; store value | 126 ; MOVII xA,batt_voltage ; store value |
| 114 | 127 |
| 115 bcf battery_is_36v ; by default assume it is a 1.5 battery | 128 bcf battery_is_36v ; by default assume it is a 1.5 battery |
| 116 MOVII batt_voltage,sub_b ; load measured battery voltage | 129 MOVII batt_voltage,sub_b ; load measured battery voltage |
| 117 | 130 |
| 118 ; Check if the battery is a 3.6V lithium | 131 ; check if the battery is a 3.6V lithium |
| 119 MOVLI lithium_36v_low,sub_a ; load threshold for 3.6 Volt lithium battery | 132 MOVLI lithium_36v_low,sub_a ; load threshold for 3.6 Volt lithium battery |
| 120 call cmpU16 ; sub_a - sub_b | 133 call cmpU16 ; sub_a - sub_b |
| 121 btfss neg_flag ; battery voltage > 3.6 V lithium threshold ? | 134 btfss neg_flag ; battery voltage > 3.6 V lithium threshold ? |
| 122 bra get_battery_voltage_9 ; NO - keep assumption of 1.5V battery | 135 bra get_battery_voltage_9 ; NO - keep assumption of 1.5V battery |
| 123 bsf battery_is_36v ; YES - set flag | 136 bsf battery_is_36v ; YES - set flag |
| 124 | 137 |
| 125 ; Check if the battery is near-dead already | 138 ; check if the battery is near-dead already |
| 126 MOVLI lithium_36v_empty,sub_a ; load threshold for near-dead lithium battery | 139 MOVLI lithium_36v_empty,sub_a ; load threshold for near-dead lithium battery |
| 127 call cmpU16 ; sub_a - sub_b | 140 call cmpU16 ; sub_a - sub_b |
| 128 btfsc neg_flag ; battery voltage > dead-battery threshold ? | 141 btfsc neg_flag ; battery voltage > dead-battery threshold ? |
| 129 bra get_battery_voltage_3 ; YES - battery is still ok | 142 bra get_battery_voltage_3 ; YES - battery is still ok |
| 130 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used" | 143 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used" |
| 211 bsf battery_low_condition ; set battery low condition by default | 224 bsf battery_low_condition ; set battery low condition by default |
| 212 cpfslt batt_percent ; current battery level <= warning threshold ? | 225 cpfslt batt_percent ; current battery level <= warning threshold ? |
| 213 bcf battery_low_condition ; NO - clear battery low condition | 226 bcf battery_low_condition ; NO - clear battery low condition |
| 214 return ; - done | 227 return ; - done |
| 215 | 228 |
| 216 | |
| 217 get_battery_voltage_9: | 229 get_battery_voltage_9: |
| 218 ; use 1.5V battery voltage mode | 230 ; use 1.5V battery voltage mode |
| 219 ; use approximation (batt_voltage-aa_15v_low)/4 = lo | 231 ; use approximation (batt_voltage-aa_15v_low)/4 = lo |
| 220 MOVII batt_voltage,sub_a ; load battery voltage | 232 MOVII batt_voltage,sub_a ; load battery voltage |
| 221 MOVLI aa_15v_low, sub_b ; load offset | 233 MOVLI aa_15v_low, sub_b ; load offset |
| 228 rrcf sub_c+0 | 240 rrcf sub_c+0 |
| 229 movff sub_c+0,lo ; store result | 241 movff sub_c+0,lo ; store result |
| 230 bra get_battery_voltage_8 ; check limits and return | 242 bra get_battery_voltage_8 ; check limits and return |
| 231 | 243 |
| 232 | 244 |
| 233 global get_ambient_level ; called from ISR only, in context bank isr_backup | 245 ;----------------------------------------------------------------------------- |
| 246 ; get ambient Light Level | |
| 247 ; | |
| 248 ; called from ISR only, in context bank isr_backup | |
| 249 ; | |
| 250 global get_ambient_level | |
| 234 get_ambient_level: | 251 get_ambient_level: |
| 235 btfsc sleepmode ; in sleep mode? | 252 btfsc sleepmode ; in sleep mode? |
| 236 return ; YES - done | 253 return ; YES - done |
| 237 btfsc adc_is_running ; NO - ADC in use? | 254 btfsc adc_is_running ; NO - ADC in use? |
| 238 return ; YES - return | 255 return ; YES - return |
| 259 MOVII ADRESL,ambient_light | 276 MOVII ADRESL,ambient_light |
| 260 bcf ADCON0,0 ; power off ADC | 277 bcf ADCON0,0 ; power off ADC |
| 261 | 278 |
| 262 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | 279 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) |
| 263 ; first: divide by 16 | 280 ; first: divide by 16 |
| 264 bcf STATUS,C | 281 |
| 265 rrcf ambient_light+1 | 282 ; bcf STATUS,C ; old /16 code |
| 266 rrcf ambient_light+0 | 283 ; rrcf ambient_light+1 |
| 267 bcf STATUS,C | 284 ; rrcf ambient_light+0 |
| 268 rrcf ambient_light+1 | 285 ; bcf STATUS,C |
| 269 rrcf ambient_light+0 | 286 ; rrcf ambient_light+1 |
| 270 bcf STATUS,C | 287 ; rrcf ambient_light+0 |
| 271 rrcf ambient_light+1 | 288 ; bcf STATUS,C |
| 272 rrcf ambient_light+0 | 289 ; rrcf ambient_light+1 |
| 273 bcf STATUS,C | 290 ; rrcf ambient_light+0 |
| 274 rrcf ambient_light+1 | 291 ; bcf STATUS,C |
| 275 rrcf ambient_light+0 | 292 ; rrcf ambient_light+1 |
| 293 ; rrcf ambient_light+0 | |
| 294 | |
| 295 movlw .4 ; divide by 2^4 = 16 | |
| 296 get_ambient_level1_loop: | |
| 297 bcf STATUS,C ; clear carry | |
| 298 rrcf ambient_light+1 ; rotate right high byte, carry into MSB, LSB into carry | |
| 299 rrcf ambient_light+0 ; rotate right low byte, carry into MSB, LSB into carry | |
| 300 decfsz WREG ; decrement counter, done? | |
| 301 bra get_ambient_level1_loop ; NO - loop | |
| 302 | |
| 276 ; result: ambient_light:2/16 | 303 ; result: ambient_light:2/16 |
| 277 ; now make sure to have value between ambient_light_low and ambient_light_max | 304 ; now make sure to have value between ambient_light_low and ambient_light_max |
| 278 | 305 |
| 279 movlw .254 | 306 movlw .254 |
| 280 tstfsz ambient_light+1 ; > 255 ? | 307 tstfsz ambient_light+1 ; > 255 ? |
| 294 | 321 |
| 295 movff opt_brightness,isr_lo ; get brightness setting | 322 movff opt_brightness,isr_lo ; get brightness setting |
| 296 | 323 |
| 297 btfsc RCSTA1,7 ; UART module on? | 324 btfsc RCSTA1,7 ; UART module on? |
| 298 clrf isr_lo ; YES - set temporary to eco mode | 325 clrf isr_lo ; YES - set temporary to eco mode |
| 326 | |
| 299 incf isr_lo,F ; adjust 0-2 to 1-3 | 327 incf isr_lo,F ; adjust 0-2 to 1-3 |
| 300 | 328 |
| 301 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting | 329 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting |
| 302 | 330 |
| 303 banksel HW_descriptor ; select bank where hardware descriptor and model variant is stored | 331 banksel HW_descriptor ; select bank where hardware descriptor and model variant is stored |
| 305 movlw ambient_light_max_high_15V ; 1.5V battery brightest setting | 333 movlw ambient_light_max_high_15V ; 1.5V battery brightest setting |
| 306 btfsc battery_is_36v ; 3.6V battery in use? | 334 btfsc battery_is_36v ; 3.6V battery in use? |
| 307 movlw ambient_light_max_high_36V ; YES - 3.6V battery brightest setting | 335 movlw ambient_light_max_high_36V ; YES - 3.6V battery brightest setting |
| 308 banksel isr_backup ; back to ISR default bank | 336 banksel isr_backup ; back to ISR default bank |
| 309 | 337 |
| 310 dcfsnz isr_lo,F | 338 dcfsnz isr_lo,F ; eco setting? |
| 311 movlw ambient_light_max_eco ; eco setting | 339 movlw ambient_light_max_eco ; YES |
| 312 dcfsnz isr_lo,F | 340 dcfsnz isr_lo,F ; medium setting? |
| 313 movlw ambient_light_max_medium ; brightest setting | 341 movlw ambient_light_max_medium ; YES |
| 314 | 342 |
| 315 incf ambient_light+0,F ; +1 | 343 incf ambient_light+0,F ; +1 |
| 316 cpfslt ambient_light+0 ; smaller than WREG? | 344 cpfslt ambient_light+0 ; smaller than WREG? |
| 317 movwf ambient_light+0 ; NO - set to max. | 345 movwf ambient_light+0 ; NO - set to max. |
| 318 | 346 |
| 319 movff opt_brightness,isr_lo | 347 movff opt_brightness,isr_lo ; get brightness setting |
| 320 incf isr_lo,F ; adjust 0-2 to 1-3 | 348 incf isr_lo,F ; adjust 0-2 to 1-3 |
| 321 movlw ambient_light_min_high ; darkest setting | 349 movlw ambient_light_min_high ; default to highest setting |
| 322 dcfsnz isr_lo,F | 350 dcfsnz isr_lo,F ; eco setting? |
| 323 movlw ambient_light_min_eco ; darkest setting | 351 movlw ambient_light_min_eco ; YES |
| 324 dcfsnz isr_lo,F | 352 dcfsnz isr_lo,F ; medium setting? |
| 325 movlw ambient_light_min_medium ; darkest setting | 353 movlw ambient_light_min_medium ; YES |
| 326 dcfsnz isr_lo,F | 354 dcfsnz isr_lo,F ; highest setting? |
| 327 movlw ambient_light_min_high ; darkest setting | 355 movlw ambient_light_min_high ; YES |
| 328 | 356 |
| 329 cpfsgt ambient_light+0 ; bigger than WREG? | 357 cpfsgt ambient_light+0 ; bigger than WREG? |
| 330 movwf ambient_light+0 ; NO - set to min | 358 movwf ambient_light+0 ; NO - set to min |
| 331 | 359 |
| 332 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt | 360 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt |
| 333 return | 361 return ; done |
| 334 | 362 |
| 335 | 363 |
| 336 ;============================================================================= | 364 ;----------------------------------------------------------------------------- |
| 337 ; routines for reading analog-attached external sensors | 365 ; Read analog external Sensors |
| 366 ; | |
| 367 ; called from outside ISR only | |
| 338 ; | 368 ; |
| 339 IFDEF _external_sensor | 369 IFDEF _external_sensor |
| 340 | 370 |
| 341 global get_analog_inputs ; called from outside ISR only | 371 global get_analog_inputs |
| 342 get_analog_inputs: ; start ADC and wait until finished | 372 get_analog_inputs: |
| 343 bsf adc_is_running ; =1: the ADC is in use | 373 bsf adc_is_running ; =1: the ADC is in use |
| 344 btfsc TFT_PWM | 374 btfsc TFT_PWM ; PWM active? |
| 345 bra get_analog_inputs ; wait for PWM low | 375 bra get_analog_inputs ; YES - wait for PWM low |
| 376 | |
| 346 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV | 377 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV |
| 347 movwf ADCON1 | 378 movwf ADCON1 ; ... |
| 348 banksel sensor1_mv ; select bank where sensor1_mv is stored | 379 |
| 349 ; Sensor 1 | 380 ; Sensor 1 |
| 350 movlw b'00100001' ; power on ADC, select AN8 | 381 movlw b'00100001' ; power on ADC, select AN8 |
| 351 rcall wait_adc | 382 MOVII sensor1_mv,mpr ; get last sensor voltage |
| 352 bcf STATUS,C | 383 rcall get_analog_inputs_common ; update value |
| 353 rrcf ADRESH,F ; /2 | 384 MOVII mpr,sensor1_mv ; store new value |
| 354 rrcf ADRESL,W | 385 |
| 355 addwf sensor1_mv+0,F ; add to sensor1_mv:2 | |
| 356 movf ADRESH,W | |
| 357 addwfc sensor1_mv+1,F | |
| 358 bcf STATUS,C | |
| 359 rrcf sensor1_mv+1,F ; divide /2 | |
| 360 rrcf sensor1_mv+0,F | |
| 361 movlw HIGH ignore_mv | |
| 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 ? | |
| 367 bra get_analog_inputs2 ; YES - skip here | |
| 368 movlw .19 | |
| 369 cpfsgt sensor1_mv+0 ; > 1.9 mV ? | |
| 370 clrf sensor1_mv+0 ; NO - clear result | |
| 371 | |
| 372 get_analog_inputs2: | |
| 373 ; Sensor 2 | 386 ; Sensor 2 |
| 374 movlw b'00100101' ; power on ADC, select AN9 | 387 movlw b'00100101' ; power on ADC, select AN9 |
| 375 rcall wait_adc | 388 MOVII sensor2_mv,mpr ; get last sensor voltage |
| 376 bcf STATUS,C | 389 rcall get_analog_inputs_common ; update value |
| 377 rrcf ADRESH,F ; /2 | 390 MOVII mpr,sensor2_mv ; store new value |
| 378 rrcf ADRESL,W | 391 |
| 379 addwf sensor2_mv+0,F ; add to sensor2_mv:2 | |
| 380 movf ADRESH,W | |
| 381 addwfc sensor2_mv+1,F | |
| 382 bcf STATUS,C | |
| 383 rrcf sensor2_mv+1,F ; divide /2 | |
| 384 rrcf sensor2_mv+0,F | |
| 385 movlw HIGH ignore_mv | |
| 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 ? | |
| 391 bra get_analog_inputs3 ; YES - skip here | |
| 392 movlw .19 | |
| 393 cpfsgt sensor2_mv+0 ; > 1.9 mV ? | |
| 394 clrf sensor2_mv+0 ; NO - clear result | |
| 395 | |
| 396 get_analog_inputs3: | |
| 397 ; Sensor 3 | 392 ; Sensor 3 |
| 398 movlw b'00101001' ; power on ADC, select AN10 | 393 movlw b'00101001' ; power on ADC, select AN10 |
| 399 rcall wait_adc | 394 MOVII sensor3_mv,mpr ; get last sensor voltage |
| 400 bcf STATUS,C | 395 rcall get_analog_inputs_common ; update value |
| 401 rrcf ADRESH,F ; /2 | 396 MOVII mpr,sensor3_mv ; store new value |
| 402 rrcf ADRESL,W | 397 |
| 403 addwf sensor3_mv+0,F ; add to sensor3_mv:2 | |
| 404 movf ADRESH,W | |
| 405 addwfc sensor3_mv+1,F | |
| 406 bcf STATUS,C | |
| 407 rrcf sensor3_mv+1,F ; divide /2 | |
| 408 rrcf sensor3_mv+0,F | |
| 409 movlw HIGH ignore_mv | |
| 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 ? | |
| 415 bra get_analog_inputs4 ; YES - skip here | |
| 416 movlw .19 | |
| 417 cpfsgt sensor3_mv+0 ; > 1.9 mV ? | |
| 418 clrf sensor3_mv+0 ; NO - clear result | |
| 419 get_analog_inputs4: | |
| 420 banksel common ; back to bank common | |
| 421 bcf ADCON0,0 ; power off ADC | 398 bcf ADCON0,0 ; power off ADC |
| 422 bcf adc_is_running ; done with ADC | 399 bcf adc_is_running ; done with ADC |
| 423 return | 400 return ; done |
| 401 | |
| 402 get_analog_inputs_common: | |
| 403 rcall wait_adc ; wait for ADC | |
| 404 bcf STATUS,C ; clear carry flag | |
| 405 rrcf ADRESH,F ; divide /2 | |
| 406 rrcf ADRESL,W ; ... | |
| 407 addwf mpr+0,F ; add to sensor_mv:2 | |
| 408 movf ADRESH,W ; ... | |
| 409 addwfc mpr+1,F ; ... | |
| 410 bcf STATUS,C ; clear carry flag | |
| 411 rrcf mpr+1,F ; divide /2 | |
| 412 rrcf mpr+0,F ; ... | |
| 413 movlw HIGH ignore_mv_above ; get upper limit of operational range | |
| 414 cpfsgt mpr+1 ; > limit ? | |
| 415 bra get_analog_inputs_common_1 ; NO - ok to use | |
| 416 CLRI mpr ; YES - ignore this reading | |
| 417 return ; - done | |
| 418 get_analog_inputs_common_1: | |
| 419 tstfsz mpr+1 ; > 25.5 mV ? | |
| 420 return ; YES - ok to use anyway | |
| 421 movlw ignore_mv_below ; NO - get lower limit of operational range | |
| 422 cpfsgt mpr+0 ; - > limit ? | |
| 423 clrf mpr+0 ; NO - ignore this reading | |
| 424 return ; - done | |
| 424 | 425 |
| 425 ENDIF ; _external_sensor | 426 ENDIF ; _external_sensor |
| 426 | 427 |
| 427 ;============================================================================= | 428 |
| 428 | 429 ;----------------------------------------------------------------------------- |
| 429 global piezo_config ; called from outside ISR only | 430 ; Set the Sensitivity of the Piezo Buttons |
| 430 piezo_config: ; set up sensitivity of heinrichsweikamp piezo buttons (~30ms) | 431 ; |
| 432 ; called from outside ISR only, ~ 30 ms | |
| 433 ; | |
| 434 global piezo_config | |
| 435 piezo_config: | |
| 431 clrf TMR5H ; clear TMR5H first | 436 clrf TMR5H ; clear TMR5H first |
| 432 clrf TMR5L ; clear TMR5L thereafter | 437 clrf TMR5L ; clear TMR5L thereafter |
| 433 bcf PIR5,TMR5IF ; clear timer 5 overrun flag, will take ~ 2 seconds to overrun after reset | 438 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 | 439 bcf switch_right ; clear left-over button events |
| 435 bcf switch_left ; ... | 440 bcf switch_left ; ... |
| 440 bra piezo_config ; YES - loop to wait for release | 445 bra piezo_config ; YES - loop to wait for release |
| 441 | 446 |
| 442 btfss PIR5,TMR5IF ; timeout? | 447 btfss PIR5,TMR5IF ; timeout? |
| 443 bra piezo_config0 ; NO - not yet, loop | 448 bra piezo_config0 ; NO - not yet, loop |
| 444 | 449 |
| 445 bcf INTCON,GIE | 450 bcf INTCON,GIE ; disable all interrupts |
| 446 | 451 |
| 447 movff opt_cR_button_right,WREG ; right button | 452 movff opt_cR_button_right,WREG ; right button |
| 448 btfsc flip_screen ; 180° rotation ? | 453 btfsc flip_screen ; 180° rotation ? |
| 449 movff opt_cR_button_left,WREG ; YES - left button | 454 movff opt_cR_button_left,WREG ; YES - left button |
| 450 rcall piezo_config_tx | 455 rcall piezo_config_tx ; send to button |
| 451 | 456 |
| 452 movff opt_cR_button_left,WREG ; left button | 457 movff opt_cR_button_left,WREG ; left button |
| 453 btfsc flip_screen ; 180° rotation ? | 458 btfsc flip_screen ; 180° rotation ? |
| 454 movff opt_cR_button_right,WREG ; YES - right button | 459 movff opt_cR_button_right,WREG ; YES - right button |
| 455 rcall piezo_config_tx | 460 rcall piezo_config_tx ; send to button |
| 456 | 461 |
| 457 movlw .20 ; reserved | 462 movlw .20 ; reserved |
| 458 rcall piezo_config_tx | 463 rcall piezo_config_tx ; send to button |
| 459 movlw .20 ; reserved | 464 movlw .20 ; reserved |
| 460 rcall piezo_config_tx | 465 rcall piezo_config_tx ; send to button |
| 461 | 466 |
| 462 bsf INTCON,GIE | 467 bsf INTCON,GIE ; re-enable all interrupts |
| 463 return | 468 return ; done |
| 464 | 469 |
| 465 piezo_config_tx: ; send one byte | 470 |
| 466 movwf lo ; store byte | 471 ;----------------------------------------------------------------------------- |
| 467 movlw .8 | 472 ; Helper Function - send 1 Byte to the Piezo Buttons |
| 468 movwf hi ; bit counter | 473 ; |
| 474 piezo_config_tx: | |
| 475 movwf lo ; store byte to be sent | |
| 476 movlw .8 ; set up bit count | |
| 477 movwf hi ; ... | |
| 469 bcf TX3_PIEZO_CFG ; start bit | 478 bcf TX3_PIEZO_CFG ; start bit |
| 470 rcall piezo_config_wait_bit | 479 rcall piezo_config_wait_bit ; wait |
| 471 piezo_config_tx_loop: | 480 piezo_config_tx_loop: ; LSB first |
| 472 btfss lo,0 ; LSB first | 481 btfss lo,0 ; bit = 0 ? |
| 473 bcf TX3_PIEZO_CFG | 482 bcf TX3_PIEZO_CFG ; YES |
| 474 btfsc lo,0 ; LSB first | 483 btfsc lo,0 ; bit = 1 ? |
| 475 bsf TX3_PIEZO_CFG | 484 bsf TX3_PIEZO_CFG ; YES |
| 476 rcall piezo_config_wait_bit | 485 rcall piezo_config_wait_bit ; wait |
| 477 rrncf lo,F | 486 rrncf lo,F ; shift lo to access next bit |
| 478 decfsz hi,F | 487 decfsz hi,F ; decrement bit counter, all done? |
| 479 bra piezo_config_tx_loop | 488 bra piezo_config_tx_loop ; NO - loop |
| 480 bsf TX3_PIEZO_CFG ; stop bit | 489 bsf TX3_PIEZO_CFG ; YES - stop bit |
| 481 rcall piezo_config_wait_bit | 490 rcall piezo_config_wait_bit ; - wait |
| 482 return | 491 return ; - done |
| 483 | 492 |
| 484 piezo_config_wait_bit: | 493 piezo_config_wait_bit: |
| 485 setf TMR5H ; set TMR5H first (to 255) | 494 setf TMR5H ; set TMR5H first (to 255) |
| 486 movlw .255-.26 ; 26 x 31.5 µs = 819 us | 495 movlw .255-.26 ; 26 x 31.5 µs = 819 µs |
| 487 movwf TMR5L ; set TMR5L thereafter | 496 movwf TMR5L ; set TMR5L thereafter |
| 488 bcf PIR5,TMR5IF ; clear timer 5 overrun flag | 497 bcf PIR5,TMR5IF ; clear timer 5 overrun flag |
| 489 piezo_config_wait_bit3: | 498 piezo_config_wait_bit_loop: |
| 490 btfss PIR5,TMR5IF ; timeout? | 499 btfss PIR5,TMR5IF ; timeout? |
| 491 bra piezo_config_wait_bit3 ; NO - loop | 500 bra piezo_config_wait_bit_loop ; NO - loop |
| 492 return ; YES - done | 501 return ; YES - done |
| 493 | 502 |
| 494 | 503 |
| 495 global get_analog_switches ; called from ISR and from sleep mode, returns in bank common | 504 ;----------------------------------------------------------------------------- |
| 496 get_analog_switches: ; start ADC and wait until finished | 505 ; get the analog Switches |
| 497 banksel HW_variants ; switch to bank where OSTC model variant is stored | 506 ; |
| 507 ; called from ISR and from sleep mode, returns in bank common | |
| 508 ; | |
| 509 global get_analog_switches | |
| 510 get_analog_switches: | |
| 498 btfsc analog_switches ; does the OSTC have analog switches? | 511 btfsc analog_switches ; does the OSTC have analog switches? |
| 499 bra get_analog_switches_1 ; YES | 512 bra get_analog_switches_1 ; YES |
| 513 | |
| 500 get_analog_switches0: | 514 get_analog_switches0: |
| 501 banksel common ; NO - back to bank common | 515 bcf analog_sw1_pressed ; clear flag for analog switch 1 |
| 502 bcf analog_sw1_pressed ; - clear flag for analog switch 1 | 516 bcf analog_sw2_pressed ; clear flag for analog switch 2 |
| 503 bcf analog_sw2_pressed ; - clear flag for analog switch 2 | 517 return ; done |
| 504 return ; - done | |
| 505 | 518 |
| 506 get_analog_switches_1: | 519 get_analog_switches_1: |
| 507 banksel common ; back to bank common | |
| 508 btfsc adc_is_running ; ADC in use? | 520 btfsc adc_is_running ; ADC in use? |
| 509 return ; YES - abort | 521 return ; YES - abort |
| 510 btfsc cc_active ; NO - charging? | 522 btfsc cc_active ; NO - charging? |
| 511 bra get_analog_switches0 ; YES - abort (and clear both flags) | 523 bra get_analog_switches0 ; YES - abort (and clear both flags) |
| 512 ;bra get_analog_switches_2 ; NO - read switches | |
| 513 | 524 |
| 514 get_analog_switches_2: | 525 get_analog_switches_2: |
| 515 bsf adc_is_running | 526 bsf adc_is_running ; flag that ADC is in use |
| 516 bcf ADCON2,ADFM ; left justified | 527 bcf ADCON2,ADFM ; left justified |
| 517 clrf ADCON1 | 528 clrf ADCON1 ; |
| 518 movlw b'00100101' ; power on ADC, select AN9 | 529 movlw b'00100101' ; power on ADC, select AN9 |
| 519 rcall wait_adc | 530 rcall wait_adc ; wait for ADC |
| 520 | 531 |
| 521 banksel isr_backup ; select bank ISR data | 532 banksel isr_backup ; select bank ISR data |
| 522 movff ADRESH,WREG | 533 |
| 523 addwf analog_sw2_raw+0 | 534 movlw .250 ; set upper limit |
| 524 movlw .0 | 535 cpfslt ADRESH ; above upper limit? |
| 525 addwfc analog_sw2_raw+1 | 536 movff analog_sw2,ADRESH ; YES - use (last) average instead |
| 537 | |
| 538 movf ADRESH,W ; get ADC result (high byte) | |
| 539 addwf analog_sw2_raw+0 ; add to averaging register | |
| 540 movlw .0 ; ... | |
| 541 addwfc analog_sw2_raw+1 ; ... | |
| 526 decfsz analog_counter,F ; continue averaging? | 542 decfsz analog_counter,F ; continue averaging? |
| 527 bra get_analog_switches_2a ; YES | 543 bra get_analog_switches_2a ; YES |
| 528 bcf STATUS,C ; NO - done, compute average | 544 |
| 529 rrcf analog_sw2_raw+1 | 545 ; compute average - divide by 16 |
| 530 rrcf analog_sw2_raw+0 ; /2 | 546 swapf analog_sw2_raw+1,F ; swap nibbles in high byte |
| 531 bcf STATUS,C | 547 movlw b'11110000' ; keep only upper nibble |
| 532 rrcf analog_sw2_raw+1 | 548 andwf analog_sw2_raw+1,W ; ... |
| 533 rrcf analog_sw2_raw+0 ; /4 | 549 movwf analog_sw2 ; copy ex lower nibble of high byte to upper nibble of result |
| 534 bcf STATUS,C | 550 swapf analog_sw2_raw+0,F ; swap nibble in low byte |
| 535 rrcf analog_sw2_raw+1 | 551 movlw b'00001111' ; keep only lower nibble |
| 536 rrcf analog_sw2_raw+0 ; /8 | 552 andwf analog_sw2_raw+0,W ; ... |
| 537 bcf STATUS,C | 553 iorwf analog_sw2,F ; copy ex upper nibble of low byte to lower nibble of result |
| 538 rrcf analog_sw2_raw+1 | 554 |
| 539 rrcf analog_sw2_raw+0 ; /16 | 555 clrf analog_sw2_raw+1 ; reset average registers |
| 540 movff analog_sw2_raw+0,analog_sw2 | 556 clrf analog_sw2_raw+0 ; ... |
| 541 clrf analog_sw2_raw+1 | |
| 542 clrf analog_sw2_raw+0 ; reset average registers | |
| 543 ; movlw .16 | |
| 544 ; movwf analog_counter ; only once... | |
| 545 | 557 |
| 546 get_analog_switches_2a: | 558 get_analog_switches_2a: |
| 547 bcf analog_sw2_pressed ; default to not pressed | 559 bcf analog_sw2_pressed ; default to not pressed |
| 548 movff opt_cR_button_left,WREG ; 20-100 | 560 movff opt_cR_button_left,WREG ; get button sensitivity (20-100) |
| 549 bcf STATUS,C | 561 bcf STATUS,C ; clear carry bit |
| 550 rrcf WREG ; /2 -> 10-50 | 562 rrcf WREG ; /2 -> 10-50 |
| 551 bcf STATUS,C | 563 bcf STATUS,C ; clear carry bit |
| 552 rrcf WREG ; /2 -> 5-25 | 564 rrcf WREG ; /4 -> 5-25 |
| 553 decf WREG,W ; -1 | 565 decf WREG,W ; -1 |
| 554 decf WREG,W ; -1 | 566 decf WREG,W ; -1 |
| 555 decf WREG,W ; -1 -> 2-22 | 567 decf WREG,W ; -1 -> 2-22 |
| 556 btfss button_polarity,1 ; (1= normal, 0=inverted) | 568 btfss button_polarity,1 ; (1= normal, 0=inverted) |
| 557 bra get_analog_switches_sw2_inv | 569 bra get_analog_switches_sw2_inv ; 0 |
| 558 addwf analog_sw2,W ; average (~128) | 570 addwf analog_sw2,W ; 1 - add average (~128) |
| 559 cpfsgt ADRESH ; pressed? | 571 cpfsgt ADRESH ; - pressed? |
| 560 bra get_analog_switches_3 ; NO | 572 bra get_analog_switches_3 ; NO - continue with other button |
| 561 bra get_analog_switches_sw2_pressed ; YES (left button normal) | 573 bra get_analog_switches_sw2_pressed ; YES - (left button normal) |
| 562 | 574 |
| 563 get_analog_switches_sw2_inv: | 575 get_analog_switches_sw2_inv: |
| 564 subwf analog_sw2,W ; average (~128) | 576 subwf analog_sw2,W ; subtract average (~128) |
| 565 cpfslt ADRESH ; pressed? | 577 cpfslt ADRESH ; pressed? |
| 566 bra get_analog_switches_3 ; NO | 578 bra get_analog_switches_3 ; NO - continue with other button |
| 567 ;bra get_analog_switches_sw2_pressed ; YES (left button inverted) | 579 ;bra get_analog_switches_sw2_pressed ; YES - (left button inverted) |
| 568 | 580 |
| 569 get_analog_switches_sw2_pressed: | 581 get_analog_switches_sw2_pressed: |
| 570 bsf analog_sw2_pressed ; set left button as pressed | 582 bsf analog_sw2_pressed ; set left button as pressed |
| 571 | 583 |
| 572 get_analog_switches_3: | 584 get_analog_switches_3: |
| 573 movlw b'00101001' ; power on ADC, select AN10 | 585 movlw b'00101001' ; power on ADC, select AN10 |
| 574 rcall wait_adc | 586 rcall wait_adc ; wait on ADC |
| 575 movff ADRESH,WREG | 587 |
| 576 addwf analog_sw1_raw+0 | 588 movlw .250 ; set upper limit |
| 577 movlw .0 | 589 cpfslt ADRESH ; above upper limit? |
| 578 addwfc analog_sw1_raw+1 | 590 movff analog_sw1,ADRESH ; YES - use (last) average instead |
| 591 | |
| 592 movf ADRESH,W ; get ADC result (high byte) | |
| 593 addwf analog_sw1_raw+0 ; add to averaging register | |
| 594 movlw .0 ; ... | |
| 595 addwfc analog_sw1_raw+1 ; ... | |
| 579 tstfsz analog_counter ; continue averaging? | 596 tstfsz analog_counter ; continue averaging? |
| 580 bra get_analog_switches_3a ; YES | 597 bra get_analog_switches_3a ; YES |
| 581 bcf STATUS,C ; NO - done, compute average | 598 |
| 582 rrcf analog_sw1_raw+1 | 599 ; compute average - divide by 16 |
| 583 rrcf analog_sw1_raw+0 ; /2 | 600 swapf analog_sw1_raw+1,F ; swap nibbles in high byte |
| 584 bcf STATUS,C | 601 movlw b'11110000' ; keep only upper nibble |
| 585 rrcf analog_sw1_raw+1 | 602 andwf analog_sw1_raw+1,W ; ... |
| 586 rrcf analog_sw1_raw+0 ; /4 | 603 movwf analog_sw1 ; copy ex lower nibble of high byte to upper nibble of result |
| 587 bcf STATUS,C | 604 swapf analog_sw1_raw+0,F ; swap nibble in low byte |
| 588 rrcf analog_sw1_raw+1 | 605 movlw b'00001111' ; keep only lower nibble |
| 589 rrcf analog_sw1_raw+0 ; /8 | 606 andwf analog_sw1_raw+0,W ; ... |
| 590 bcf STATUS,C | 607 iorwf analog_sw1,F ; copy ex upper nibble of low byte to lower nibble of result |
| 591 rrcf analog_sw1_raw+1 | 608 |
| 592 rrcf analog_sw1_raw+0 ; /16 | 609 clrf analog_sw1_raw+1 ; reset average registers |
| 593 movff analog_sw1_raw+0,analog_sw1 | 610 clrf analog_sw1_raw+0 ; ... |
| 594 clrf analog_sw1_raw+1 | 611 |
| 595 clrf analog_sw1_raw+0 ; reset average registers | 612 ; set up averaging counter for next call-up |
| 596 movlw .16 | 613 movlw .16 ; 16 averaging rounds |
| 597 movwf analog_counter ; only once... | 614 movwf analog_counter ; ... |
| 598 | 615 |
| 599 get_analog_switches_3a: | 616 get_analog_switches_3a: |
| 600 bcf analog_sw1_pressed ; default to not pressed | 617 bcf analog_sw1_pressed ; default to not pressed |
| 601 movff opt_cR_button_right,WREG ; 20-100 | 618 movff opt_cR_button_right,WREG ; 20-100 |
| 602 bcf STATUS,C | 619 bcf STATUS,C ; clear carry bit |
| 603 rrcf WREG ; /2 -> 10-50 | 620 rrcf WREG ; /2 -> 10-50 |
| 604 bcf STATUS,C | 621 bcf STATUS,C ; clear carry bit |
| 605 rrcf WREG ; /2 -> 5-25 | 622 rrcf WREG ; /2 -> 5-25 |
| 606 decf WREG,W ; -1 | 623 decf WREG,W ; -1 |
| 607 decf WREG,W ; -1 | 624 decf WREG,W ; -1 |
| 608 decf WREG,W ; -1 -> 2-22 | 625 decf WREG,W ; -1 -> 2-22 |
| 609 btfss button_polarity,0 ; (1= normal, 0=inverted) | 626 btfss button_polarity,0 ; (1= normal, 0=inverted) |
| 610 bra get_analog_switches_sw1_inv | 627 bra get_analog_switches_sw1_inv ; 0 |
| 611 addwf analog_sw1,W ; average (~128) | 628 addwf analog_sw1,W ; 1 - add to average (~128) |
| 612 cpfsgt ADRESH ; pressed? | 629 cpfsgt ADRESH ; - pressed? |
| 613 bra get_analog_switches_4 ; NO | 630 bra get_analog_switches_4 ; NO - continue with clean-up |
| 614 bra get_analog_switches_sw1_pressed ; YES (right button normal) | 631 bra get_analog_switches_sw1_pressed ; YES - (right button normal) |
| 615 | 632 |
| 616 get_analog_switches_sw1_inv: | 633 get_analog_switches_sw1_inv: |
| 617 subwf analog_sw1,W ; average (~128) | 634 subwf analog_sw1,W ; subtract from average (~128) |
| 618 cpfslt ADRESH ; pressed? | 635 cpfslt ADRESH ; pressed? |
| 619 bra get_analog_switches_4 ; NO | 636 bra get_analog_switches_4 ; NO - continue with clean-up |
| 620 ;bra get_analog_switches_sw1_pressed ; YES (right button inverted) | 637 ;bra get_analog_switches_sw1_pressed ; YES - (right button inverted) |
| 621 | 638 |
| 622 get_analog_switches_sw1_pressed: | 639 get_analog_switches_sw1_pressed: |
| 623 bsf analog_sw1_pressed ; set right button as pressed | 640 bsf analog_sw1_pressed ; set right button as pressed |
| 624 | 641 |
| 625 get_analog_switches_4: | 642 get_analog_switches_4: |
| 626 bsf ADCON2,ADFM ; restore to right justified | 643 bsf ADCON2,ADFM ; restore to right justified |
| 627 bcf adc_is_running | 644 bcf adc_is_running ; flag ADC not in use any more |
| 645 | |
| 628 banksel common ; back to bank common | 646 banksel common ; back to bank common |
| 647 | |
| 629 btfsc analog_sw1_pressed ; right button pressed? | 648 btfsc analog_sw1_pressed ; right button pressed? |
| 630 return ; YES - done | 649 return ; YES - done |
| 631 btfsc analog_sw2_pressed ; left button pressed? | 650 btfsc analog_sw2_pressed ; left button pressed? |
| 632 return ; YES - done | 651 return ; YES - done |
| 633 setf TMR1H ; NO to both - no button pressed, set timer1 to overflow quickly | 652 setf TMR1H ; NO to both - no button pressed, set timer1 to overflow quickly |
| 634 return | 653 setf TMR1L ; - ... |
| 654 return ; - done | |
| 655 | |
| 656 ;----------------------------------------------------------------------------- | |
| 635 | 657 |
| 636 END | 658 END |
