title "Bounce3 - Debounce the Button input with Interrupt." ; ; This Code Simply Lights an LED and polls a button and toggles ; the state of another LED when it is pressed (and debounced). ; ; This is an update to the original application with using a ; Macro for the code instead of using straight assembly language ; source. The Macro References the falling edge of the RB0/Int ; Pin Interrupt Request. ; ; ; Hardware Notes: ; Reset is tied directly to Vcc and PWRT is Enabled. ; The PIC is a 16F84 Running at any speed. ; PortB.0 is Connected to a Button that pulls down to Ground when Pressed ; PortB.1 is Connected to an LED which is Pulled to Ground ("On" LED) ; PortB.2 is Connected to an LED which is Pulled to Ground ("Button" LED) ; ; Myke Predko ; 99.06.02 ; LIST P=16F84, R=DEC errorlevel 0,-305 INCLUDE "p16f84.inc" ; Register Usage CBLOCK 0x00C ; Start Registers at End of the Values ENDC #define Button PORTB,0 ; Define the "Button" Pin #define Debugt ; Use "Debug" To Reduce Polling Loops #define Lo 0 #define Hi 1 ; Macro Debounce macro movlw 0x090 ; Enable RB0 Interrupt movwf INTCON movlw 0 iorlw 0 ; Wait for Interrupt to Make w != 0 btfsc STATUS, Z goto $ - 2 clrf INTCON ; Disable the Interrupts endm PAGE __CONFIG _CP_OFF & _XT_OSC & _PWRTE_ON & _WDT_OFF ; Note that the WatchDog Timer is OFF ; Bounce - Turn on LED and poll the Button to toggle another LED. org 0 goto MainLine ; Skip Over the Interrupt Handler ; Interrupt Handler org 4 Int ; Depending on Interrupt btfss INTCON, INTF ; Button Low Received goto IntTimerOF ; Else, Overflow - Button Pressed IntTMR0Reset ; Reset the Interrupt OverFlow Timer movlw 256 - 0x09C ; Reset the Timer to Overflow in 20 msec movwf TMR0 movlw 0x030 ; Enable the Timer Interrupt movwf INTCON clrw ; Make Sure "w" is reset retfie IntTimerOF ; Overflow - Debounce Completed btfsc PORTB, 0 ; If the Button is High, Reset the Wait goto IntTMR0Reset clrf INTCON movlw 1 ; Make "w" on Return != 0 retfie ; Return from Interrupt ; Bounce3 Mainline MainLine movlw 0x0FD ; Turn on LED on RB1 When Output Enabled movwf PORTB bsf STATUS, RP0 bcf TRISB ^ 0x080, 1 ; Enable RB1 for Output bcf TRISB ^ 0x080, 2 ; Enable RB2 for Output ifdef Debug movlw 0x010 ; Debug Delay (2x Only) else movlw 0x016 ; Enable Internal Pull-Ups endif movwf OPTION_REG ^ 0x080 ; Along with Interrupts on the Falling bcf STATUS, RP0 ; Edge of RB0 and the Internal Clock ; TMR0 Source ; Prescaler Assigned to Interrupt ; Handler with an 8x Delay Loop Debounce ; Wait for the Button to go Low btfsc PORTB, 2 ; Is the Button Pressed? goto ButtonUp ; No, Light Off bsf PORTB, 2 ; Turn Light On goto Loop ButtonUp ; Button Up, Turn Light Off bcf PORTB, 2 goto Loop end