I wrote this for toggle switches. It first looks for a change of state. If seen, it counts loops to be constantly in that state, any reversal reloads counter. Exit is by loop countout: consistent state change OR too many reloads. In effect, it soft delays out the bounce. The loop counts set debounce delay and excessive reload countout. My std rules: free to use, But put "uMax/Sarge" associated with it somewhere. TNX <<<)))
Code:
dbict equ $10 ;debounce Inactive count
dbact equ $20 ;debounce Active count
swtank ds.b 1 ;btns/sws input tank
swprev ds.b 1 ;previous sw state for debouncing
Sw_In: psha
mov PTAD,swtank ;get current Sw state
lda swtank
cmp swprev ;current=previous: Xit
beq SWIXit
mov #dbict,dbIctr ;load debounce Inactive counter
SwDbLL: mov #dbact,dbActr ;load debounce Active counter
mov swtank,swprev ;put current->previous
dbnz dbIctr,SwDbLD ;only load active debounce N times
bra SWIXit ;Not full active == off
SwDbLD: bsr SwRead ;get current Sw state
lda swtank
cmp swprev ; :: previous
bne SwDbLL ;Any bounce to different state resets counter
dbnz dbActr,SwDbLD ;must stay on for #dbact counter loops
SWIXit: pula
rts