[  Home -- Blog -- Reference -- About  ]


        65C02 Assembly Snippets
        =======================

;***************************************
; DEBOUNCE - Debounced button routine
; State: UNTESTED
;
; This routine should be called at a re-
; gular interval (1ms period or so).  If
; #PIN in  PORT is read low  eight times
; consecutively,  this routine will call
; HANDLER once.
;
; - Clobbers A
; - Requires one variable: TEMP

DEBOUNCE:
    LDA PORT    ; Load port value
    AND #PIN    ; Isolate pin bit
    CMP #PIN    ; Set C to pin state
    ROL TEMP    ; Rotate into TEMP
    BNE :+      ; Any 1s left? No edge
    BCC :+      ; Was already clear?
    JMP HANDLER ; No: Edge detected!
:   RTS

; NOTES
; -----
; If PIN is "(1<<0)" or "(1<<7)" (top or
; bottom bit),  the AND and CMP instruc-
; tions can be simplified  to just a ROR
; or a ROL respectivly.
; If  the handler  is close enough,  the
; BCC and JMP can be  replaced by just a
; BCS to the handler directly.

; Assuming the routine is contained on a
; single page, it takes 23 cycles if the
; button is  not pressed  (or bouncing),
; 25 cycles if  the button is held down,
; and 21 cycles when it detects an edge.
;
;***************************************


v > Discussion No comments yet. == Add a Comment ==
[ Home -- Blog -- Reference -- About ] ---------------------------------------- 99v.no (c)2026