> Date: Wed, 16 Feb 2000 12:46:40 -0600
> From: Scott Dattalo <
spam_OUTscottTakeThisOuT
DATTALO.COM>
> Reply-To: pic microcontroller discussion list <
.....PICLISTKILLspam
@spam@MITVMA.MIT.EDU>
> To:
PICLIST
KILLspamMITVMA.MIT.EDU
> Subject: Re: debounce
>
> I'll cc this to the pic list since this thread is currently being
> discussed.
>
> On Wed, 16 Feb 2000, sep wrote:
>
> > Hi,
> > I think that your idea for debouncing the button is really cool. I am just a
> > little unsure of how to apply that to de-bouncing a switch directly.
> >
> > I am using the 16f84 and so I want to debounce 4 of the pins (a switch
> > attached on each). And then when the buttons are pressed to goto:
> >
> > button1touched
> > button2touched
> > button3touched
> > button4touched
> >
> >
> > Is there any snippets you have or any help you could give me. Sorry I am
> > just a beginner trying to get through these (probably easy) things!
> >
> > I appreciate your help.
>
>
> There are probably dozens of ways going about this. Let me pick the one
> I'd most probably choose. In most pic programs I write, there's usually an
> interrupt process (or several processes) like the TMR0 interrupt and then
> there's a foreground polling loop. The interrupt process responds as fast
> as possible to the needs of the hardware and does as little as possible.
> (Basically, just clear the pending interrupts and setting flags to notify
> the foreground that there's work to be done.) The foreground loop does the
> bulk of the work.
>
> Since you only have four switches, you only need four bits of the 8 bits
> in the variables used in the filters. I'd snatch one as a flag and do
> this:
>
>
> every 20 to 50 or so milliseconds (possibly in an interrupt routine),
> sample the I/O port that is attached to your switches. I'll assume that
> it's the lower four bits in PORTA:
>
> movf PORTA,w ;Read the current switch setting
> iorlw 0x80 ;Set the msb - this is 'we have data' flag
> movwf csa ;This is the name of the variable used in
> ;the debounce routine
>
>
> Then within your main fore-ground loop
>
> btfss csa,7 ;If there's a sample, process it
> goto next_polled_item
>
>
> #ifndef RICH_LEGGIT ; :)
> call de_bounce
> #else
> movf csa,w
> movwf cva
> #endif
>
> bcf csa,7 ;Clear the flag
>
> movf last_sample,w ;get the previous sample
> xorwf cva,w ;compare to the current
> xorwf last_sample,f ;save the current sample as the last
> andwf last_sample,w ;Monitor only changes that went high
>
> movwf temp
>
> btfsc temp,0
> call button1touched
>
> btfsc temp,1
> call button2touched
>
> btfsc temp,2
> call button3touched
>
> btfsc temp,3
> call button4touched
>
>
> The useful trick is:
>
> A ^ B ^ A = B
>
> Scott
>