throbscottle
Well-Known Member
I've managed to get press or double-press detection of a switch using TMR0. What happens is, the timer is tested periodically during the main loop. When a switch is pressed once, it sets a countdown, which is changed every time the timer overflows a set value. If the switch is pressed again within that time, it counts as a double press. If the counter reaches zero, it counts as another single press.
This is where it gets interesting. When I look at tutorials such as Gooligum, the timer is always tested for a specific value using XOR. This can't work in my case, because the amount of instruction cycles in-between tests can be different depending on which switch was pressed, so the timer can be at various values when it hits the overflow. I've used subtraction instead - if the timer gets over 200 the counter increments.
So the solution works, but the timer can't be accurate. What's a better way?
This is where it gets interesting. When I look at tutorials such as Gooligum, the timer is always tested for a specific value using XOR. This can't work in my case, because the amount of instruction cycles in-between tests can be different depending on which switch was pressed, so the timer can be at various values when it hits the overflow. I've used subtraction instead - if the timer gets over 200 the counter increments.
So the solution works, but the timer can't be accurate. What's a better way?
Code:
; test and read inputs up to here
; tactile press timer
movlw 0xC8 ; test the timer for double press events
subwf TMR0,W ; carry will be clear if under 200
btfss STATUS,C ; carry will be set if it's over 200
goto CLR_C ; next stage
movf tact_counter,f ; counter is set when single press occurs
btfss STATUS,Z ; don't decrement the counter past zero
decfsz tact_counter,f ; double press timer
goto CLR_C
bcf pre_fl ; flag that is set when single press occurs
CLR_C
; start reading tactile data
; loop back