title "Bounce - Debounce the Button input using polling." ; ; This Code Simply Lights an LED and polls a button and toggles ; the state of another LED when it is pressed (and debounced). ; ; ; 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.01 ; 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 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 btfss Button ; Is the Button Pressed? 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 btfss Button ; If Button Pressed, Wait Again for it 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 decfsz Dlay goto $ - 5 else decfsz Dlay ; Else, Short Loop goto $ - 3 endif btfsc Button ; Is the Button Pressed? 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 btfsc Button ; If Button Pressed, Wait Again for it 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 decfsz Dlay goto $ - 5 else decfsz Dlay ; Else, Short Loop goto $ - 3 endif 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