für Interessierte, welche noch in MASM-Assembler programmieren, möchte hier mal die Entprellroutine vorstellen, welche ich schon immer zum Entprellen von bis zu 8 Tasten verwendet habe. Diese Routine stammt nicht von mir, sondern von Scott Dattalo, der diese ca. 1999 für PIC-MCU's geschrieben und auf seiner leider nicht mehr existenten Homepage veröffentlicht hat. Funktion: Im wiederkehrenden Aufruf der ISR wird der Status der Portpins auf Veränderung geprüft. Die Veränderung wird für den nächsten Durchlauf gespeichert. Bleibt der Status an den Pins, an welchen die Tasten angeschlossen sind 4x unverändert, wird diese Änderung in die Variable "KeyPress" übertragen. Im Hauptprogramm lässt sich dann jedes Bit in "KeyPress" als Flag abfragen. So kann die Auswertung des Tastendrucks erfolgen. Ein "Polling", des Tastenzustandes erübrigt sich somit. ---------------------------------------------------------------------- Constants KEY_PORT,w ;select only input-Port-pins (push buttons) KEYMASK KEYn:0 ;keys to be debounced Vaiables cnt4ms ;zählt isr intervalle Flags ;Flag register KeyState ;the actual state of inputs in ISR KeyPress ;b3:0->KEY3:0 =1 Key pushed cnt1,cnt0 ;vertical counters in ISR Init: BANKSEL OPTION_REG ;bank1 S.176 movlw b'10000011' ;Interval = 4ms, Presc. 16:1 Preset 6 movwf OPTION_REG ;(adapt to needs) BANKSEL 0 ;********************************************************************* ; 16F1827 Debounce.INC ;-------------------------------- ; File 02 Debounce_ISR.INC ; Author: Ottmar ; Date: 2024.05.10 ;********************************************************************* ISR: ; fsc=4MHz, actual ISR-interval=4000us (TMR0 Pres. 16:1), ; 32working cycles (wc), of which the actual debouncing routine only ; requires 17wc isr_tmr0: ;INTCON,GIE is automatically cleared bcf INTCON,T0IE ;b5=0 TMR0 overflow is disabled bcf INTCON,T0IF ;b2=0 overflow flag is cleared GOTO $+1 ;4x28 cycles to adjust the ISR interval GOTO $+1 ; to 4000/10000 working cycles GOTO $+1 GOTO $+1 movlw .7 ;16:1 ISR-Intervall=4000wc->4ms movwf TMR0 ;Preset TMR0 for overflow after 4000 cycles ; isr_debounce: ;needs 15 working cycles (wc) ; Take over current button state into the debounce routine movf KEY_PORT,w ;select only input-Port-pins (push buttons) andlw KEYMASK movwf KeyState ;the actual state of inputs comf KeyState,w ; Increment the vertical counter movf cnt1,W xorwf cnt0,F comf cnt1,F ; See if any changes occurred movf KeyState,W xorwf KeyPress,W ; Reset the counter if no change has occurred andwf cnt1,F andwf cnt0,F ; Determine the counter's state movf cnt1,W iorwf cnt0,W ; Clear all bits that are filtered-or more accurately, save ; the state of those that are being filtered andwf KeyPress,F xorlw 0xff ; Re-write the bits that haven't changed. andwf KeyState,W iorwf KeyPress,F ;the debounced keybits are LOW as long as ; ;the button is down. ; insert other ISR functions here isr_end: bsf INTCON,T0IE ;b5=0 TMR0 overflow now enabled ;INTCON,GIE is automatically set RETFIE