;******************************************
;** POLLSWITCH -- Poll pushbutton switch **
;******************************************
pollswitch
bcf fswp ;clear pressed flag
bcf fswr ;clear released flag
bcf fswpr ;clear press and release flag
bcf fswph ;clear press and hold flag
bcf fswrp ;clear press and repeat flag
bcf fswdc ;clear double click flag
retbc tc8ms ;process switch every 8ms
movf swdtimer,f,1 ;double click timer running?
skpz
decf swdtimer,f,1 ;yes, update it
clrc ;pre-clear carry
btfss button ;switch pressed?
setc ;yes, set carry
rlcf swhist,w,1 ;rotate into history
andlw b'1111' ;keep only 4 tests
movwf swhist,1 ;save history
bz pswup ;z flag set if swithc up for history
xorlw b'1111' ;test for pushed whole history
bz pswdn ;z if switch down for history
;position either up or down from last pass
pswtran bbc fswst,pswsup ;switch still up (released)
bra pswsdn ;switch still down (pressed)
;position up in current pass
pswup bbc fswst,pswsup ;switch still up
;switch just transitioned to up (released) position
bcf fswst ;clear switch current state
bsf fswr ;indicate switch released
movf swhtimer,f,1 ;key press time running?
skpz ;z if not
bsf fswpr ;yes, indicate press and release (otherwise, was press and hold)
clrf swhtimer,1 ;clear switch press and hold timer
clrf swrtimer,1 ;clear switch press and repeat timer
return ;done (no timer to process)
;switch is still released (no change since last pass)
pswsup return ;nothing to do
;position down in current pass
pswdn bbs fswst,pswsdn ;switch still down
;switch just transistioned to down (pressed position)
bsf fswst ;set switch current state
bsf fswp ;indicate switch just pressed
bsf fswrp ;first pulse of repeat, also
movlfb swhtimer,cswpht ;set up press and hold timer
movf swdtimer,f,1 ;double-click timer running?
skpz
bsf fswdc ;yes, signal double click
movlfb swdtimer,cswdct+1 ;and reset double-click timer
return ;and get out
;switch is still pressed (no change since last pass)
pswsdn movf swhtimer,f,1 ;is hold timer already zero?
bz pswrpt ;yes, process repeat timer
decfsz swhtimer,f,1 ;decrement hold timer
return ;nothing to do if not expired
bsf fswph ;indicate switch pressed and held
bsf fswrp ;and second pulse of repeat
bra pswsrpt ;go set repeat timer
pswrpt decfsz swrtimer,f,1 ;decrement repeat timer
return ;nothing to do if not expired
bsf fswrp ;nth pulse of repeat
pswsrpt movlfb swrtimer,cswprt ;reload press & repeat timer
return
unsigned char key_tst(unsigned char k){
if(k==0){if(!(v_leds&0x04))k++; return(k);}
if(k==1){if(v_leds&0x04)k++; return(k);}
}
by Duane Benson
by Duane Benson
by Don Wilcher
by Jake Hertz