De Bounce A Switch
De Bounce A Switch
De Bounce A Switch
Mechanical switches are one of the most common interfaces to a uC. Switch inputs are asynchronous to the uC and are not electrically clean. Asynchronous inputs can be handled with a synchronizer (2 FF's). Inputs from a switch are electrically cleansed with a switch debouncer. What is switch bounce? - he non-ideal beha!ior of the contacts that creates multiple electrical transitions for a single user input.
Debouncing Switches
Switch bounce from a single depress"release of mega#$% pushbutton switches
Debouncing Switches
he problem is that the uC is usually fast enough to see all the transitions -uC acts on multiple transitions instead of a single one he oscilloscope traces showed bounce durations of #&-'&&us -our mega#$% uC runs at ($.)ns per instruction -a #&uS bounce *short+ is *#,#&-)"($.),#&--+ #(& instructions long. -a #&&uS bounce could be sampled as a !alid true or false #&&/s of times -results are incorrect beha!ior as seen by user Characteristics of switch bounce0 -nearly all switches do it -the duration of bouncing and the period of each bounce !aries -switches of e,actly the same type bounce differently -bounce differs depending on user force and speed -typical bounce fre1uency is .#-)ms 2ffecti!e switch debouncing can also re3ect 2MI and static charge effects -2MI can be periodic *so don4t sample synchronously.+ -false triggering on static electricity is li5e a single random input
Debouncing Switches
Solutions -Analog filtering -usually an 6C delay to filter out the rapid changes in switch output -tas5 is to choose 6 and C such that the input threshold is not crossed while bouncing is still occurring in in out 7th &7 out
&7
Debouncing Switches
Solutions -Cross coupled gates *MC#8&88+ -logic gates loc5 in one transition with a single-pole9 double-throw switch -both switch *:'.(-+ and chip *:&.'%+ are e,pensi!e -momentary clic5 switches *mega#$% board+ are *:&.#$+
7d d
7d d
Debouncing Switches
Solutions0 -Software -need to minimi;e C<= usage -independent of cloc5 speed -do not connect to interrupt pins9 only programmed I"> -multiple interrupts will tie up processor -don4t scan synchronously to noisy de!ices -identify initial switch closure 1uic5ly *#&&mS ma,+ - wo approaches *of many+ ris5y. -Count based *identify initial closure A?D wait A?D chec5 for same !alue+ >6 *identify initial closure A?D chec5 for same !alue for @ cycles+ -watch for safer. -C<= speed dependencies *use constant defines+ -loop e,ecution times *suggesting the use of timer interrupts+ -Digital filter based -mimics an analog filter with first-order recursi!e low pass filter -includes a software schmitt trigger -good 2MI filtering9 1uic5 response
Debouncing Switches
Solutions0 -Count based -from Aansel4s BAuide to DebouncingC -routine called from timer interrupt or delay loop -chec5 <ort D9 bit $9 pushbutton depression9 *bit will be grounded+ -returns B#4 only once per button push9 Bpulsed outputC -loo5s for a falling edge
int8_t debounce_switch() { static uint16_t state = 0; //holds present state state = (state << 1) | (! bit_is_clear(PIND, 2)) | 0xE000; if (state == 0xF000) return 1; return 0;
} first pass after reset0 second pass after reset0 after #$ false passes0 after D true passes0 after #$ true passes0 after many true passes0 after ) false passes0 value of state ###& &&&& &&&& &&&# ###& &&&& &&&& &&## #### #### #### #### #### #### #&&& &&&& #### &&&& &&&& &&&& ###& &&&& &&&& &&&& ###& &&&& &&&# #### return & return & return & return & return # return & return &
Debouncing Switches
Solutions0 -Digital filter based -acts li5e analog 6C filter followed by schmitt trigger -nearly continuous output li5e an analog circuit - &.$)E &,'F9 &.D)E&,C&9 #.& E &,FF
uint8_t output=0; //external variable indicating switch state
uint8_t debounce_switch2() { static uint8_t y_old=0, flag=0; uint8_t temp; //digital filter part y_old = x_new*0.25 + y_old*0.75 temp = (y_old >> 2); //this gives y_old/4 y_old = y_old temp; //do (y_old*0.75) by subtraction //if button is pushed, add 0.25 (3F) of new value (1.0) if(bit_is_clear(PIND, 2)){y_old = y_old + 0x3F;} //
//software schmitt trigger if((y_old > 0xF0)&&(flag==0)){flag=1; output=1;} if((y_old < 0x0F)&&(flag==1)){flag=0; output=0;} }
Debouncing Switches
Geha!ior of the digital filter debounce with schmitt trigger
Debouncing Switches
ypes of debouncer output
Sometimes we want a continuous output9 e.g.9 organ 5eyboard. >ther times we want a pulsed output9 e.g. increment hour alarm. he first counting algorithm *Gansel+ gi!es a pulsed output. he digital filter algorithm gi!es a continuous output.
button push pulsed output continuous output
Debouncing Switches
Con!erting between types of debouncer output
o get a pulsed output9 from a continuous debouncer0
output E # pushedE# output E &
pushed
always output E &
idle
pushedE&
waiting
Debouncing Switches
Con!erting between types of debouncer output
o get a continuous output9 from a pulsed output0
idle
pushed
pushedEfalling edge