title "Bounce2 - Debounce the Button input with macros." ; ; 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. ; ; ; 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 Dlay ; High Byte of Delay Variable 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 HiLo, Port, Bit if HiLo == Lo btfss Port,Bit ; Is the Button Pressed? else btfsc Port,Bit endif goto $ - 1 ; Yes - Wait for it to be Released movlw 0x00C ; Wait for Release to be Debounced movwf Dlay ; Have to Delay 20 msecs movlw 0x0D7 if HiLo == Lo btfss Port,Bit ; If Button Pressed, Wait Again for it else btfsc Port,Bit endif goto $ - 6 ; to be Released ifndef Debug ; Skip Small Loop if "Debug" Defined addlw 1 ; Increment the Delay Count btfsc STATUS, Z ; If Low Byte (in w) Not Equal to Zero, then Loop Around else nop ; Match the Number of Instructions nop endif decfsz Dlay goto $ - 5 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 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 movlw 0x07F ; Enable Internal Pull-Ups movwf OPTION_REG ^ 0x080 bcf STATUS, RP0 Loop Debounce Lo, Button ; Wait for the Button to go Low Debounce Hi, Button 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