I discovered this curious method of debounce/noise filtering on the arduino forum this week . the idea translated to pbp easily and makes my worst cheap and nasty tactile switches perform as smooth as silk.
here is a little demo (might add there is no cap used at all on the switch )
Code:
'****************************************************************
'* Name : debounce.BAS *
'* Author : richard *
'* Date : 2/17/2016 *
'* Version : 1.0 *
'* Notes : pic16f1825 *
'* : switch debounce *
'****************************************************************
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _CP_OFF & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CLKOUTEN_OFF
__config _CONFIG2, _PLLEN_ON & _LVP_OFF
#ENDCONFIG
OSCCON=$70
DEFINE OSC 32
ANSELA=0
ANSELC=0
TRISA = %011010
TRISC = 0
led1 var lata.5 ; lit when sw operated
led2 var lata.2 ; toggle when sw operated
sw var porta.3 ; the switch (active low ie has pull up resistor)
old_sw_state var bit
sw_state var bit
sw_buff var byte
clear
main :
gosub ck_sw
if sw_state != old_sw_state then ; the sw has changed either on or off
led2 = !led2
if !sw_state then led1 = !led1 ; if state is changed to on
old_sw_state=sw_state ;remember state
endif
goto main
ck_sw:
sw_buff= (sw_buff<<1) | sw
if (sw_buff=$7f ) and sw then
sw_state=1
elseif (sw_buff=$80 )and !sw then
sw_state=0
endif
return
Bookmarks