Simple Debounce Only


;debounce the switches
	movf sw_inputs_0,w
	andwf sw_inputs_0_last,w
	movwf switches_debounced_0
	movf sw_inputs_0,w
	movwf sw_inputs_0_last

Detect New Keypresses Too


;----- DEBOUNCED SWITCHES -----
#define	SW_LCD_BRIGHTNESS_UP_PRESSED	b0_switches_debounced_0,0
#define	SW_LCD_BRIGHTNESS_DOWN_PRESSED	b0_switches_debounced_0,1

;----- NEW KEYPRESSES -----
#define	SW_LCD_BRIGHTNESS_UP_NEW	b0_switches_new_0,0
#define	SW_LCD_BRIGHTNESS_DOWN_NEW	b0_switches_new_0,1




;***********************************
;***********************************
;********** READ SWITCHES **********
;***********************************
;***********************************
read_switches
	clrf	b0_switches_new_0
	
	btfss	b0_do_sw_read			;Set by heartbeat timer every 10mS
	return
	bcf	b0_do_sw_read

;Get switch input states
	clrf	b0_temp
	btfss	porta,3
	bsf	b0_temp,0
	btfss	porta,4
	bsf	b0_temp,1
;Debounce
	movf	b0_temp,w
	andwf	b0_sw_inputs_0_last,w
	movwf	b0_switches_debounced_0
;Flag new button presses
	xorwf	b0_switches_debounced_last_0,w
	andwf	b0_switches_debounced_0,w
	movwf	b0_switches_new_0
;Store last registers
	movf	b0_switches_debounced_0,w
	movwf	b0_switches_debounced_last_0
	movf	b0_temp,w
	movwf	b0_sw_inputs_0_last

	return
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *