Document 7673928

Download Report

Transcript Document 7673928

Debouncing a Switch
A Design Example
ECEn 224
20 Debounce
Page 1
© 2003-2008
BYU
Background and Motivation
ECEn 224
20 Debounce
Page 2
© 2003-2008
BYU
When you throw a switch
(button or two-pole switch)…
• It often bounces…
ECEn 224
20 Debounce
Page 3
© 2003-2008
BYU
Another switch…
switch
after
inversion
ECEn 224
20 Debounce
Page 4
© 2003-2008
BYU
Yet Another…
ECEn 224
20 Debounce
Page 5
© 2003-2008
BYU
Still Yet Another…
ECEn 224
20 Debounce
Page 6
© 2003-2008
BYU
Causes
• Switches and buttons are mechanical
– Spring loaded
• Contacts literally bounce
– Not an instant, once-only, on↔off change
ECEn 224
20 Debounce
Page 7
© 2003-2008
BYU
Source of Errors
• Consider a 100 MHz system clock, which has a 10 ns period
• Each ms would be 100,000 system clock cycles
• Downstream circuitry will see every bounce as an input change!
1 ms = 100,000 clock
cycles at 100 MHz
ECEn 224
20 Debounce
Page 8
© 2003-2008
BYU
FSM-Based Solution
ECEn 224
20 Debounce
Page 9
© 2003-2008
BYU
Solutions
• Single-output switch
– Since all you see is bouncing value, timingbased solution can be employed
• There are other solutions but they require a
different kind of switch
ECEn 224
20 Debounce
Page 10
© 2003-2008
BYU
Timing-Based Solution
• Only declare an input change after signal has been
stable for at least 5ms
5ms
ECEn 224
20 Debounce
Page 11
© 2003-2008
BYU
FSM Solution
• Simple enough that an FSM might not be
required
– Easy to concoct a sequential circuit to do this
with a counter and a single FF
• Let’s do it with an FSM
– If solution requires only a counter and a single
FF, we will find that solution
ECEn 224
20 Debounce
Page 12
© 2003-2008
BYU
Draw a Simplified Timing Diagram
5ms
5ms
noisy
debounced
time
ECEn 224
20 Debounce
Page 13
© 2003-2008
BYU
Draw a System Block Diagram
noisy
debounced
Finite
State
Machine
clk
clrTimer
timerDone
Timer
(5ms)
clk
reset
Very reminiscent of our car wash controller…
ECEn 224
20 Debounce
Page 14
© 2003-2008
BYU
The Design of the FSM
ECEn 224
20 Debounce
Page 15
© 2003-2008
BYU
Draw a State Graph
noisy’
S0
clrTimer
noisy
noisy’•timerDone
noisy’
noisy’•timerDone’
S3
S1
debounced
noisy•timerDone’
noisy
noisy•timerDone
noisy’
S2
debounced
clrTimer
noisy
ECEn 224
20 Debounce
Page 16
© 2003-2008
BYU
Draw a State Graph
noisy’
S0
Debounced output
is low…
clrTimer
noisy
noisy’•timerDone
noisy’
noisy’•timerDone’
S3
S1
debounced
noisy•timerDone’
noisy
noisy•timerDone
noisy’
S2
debounced
clrTimer
noisy
ECEn 224
20 Debounce
Page 17
© 2003-2008
BYU
Draw a State Graph
noisy’
Debounced output
is high…
S0
clrTimer
noisy
noisy’•timerDone
noisy’
noisy’•timerDone’
S3
S1
debounced
noisy•timerDone’
noisy
noisy•timerDone
noisy’
S2
debounced
clrTimer
noisy
ECEn 224
20 Debounce
Page 18
© 2003-2008
BYU
An Improved State Graph
Looks like the FSM
can be implemented
with just a single FF
noisy’/clrTimer
Do you see why there is no
need for a reset input?
S0
noisy•timerDone’
noisy•timerDone
noisy’•timerDone
noisy’•timerDone’
S1
debounced
noisy/clrTimer
ECEn 224
As mentioned, Mealy
machines often require
fewer states…
20 Debounce
Page 19
© 2003-2008
BYU
Reduce FSM to Logic
S0 = CS’
S1 = CS
noisy = N
timerDone = T
NT
CS
00
01
0
1
11
10
1
1
1
1
NS = noisy•timerDone + CS•timerDone’
clrTimer = noisy’•CS’ + noisy•CS
debounced = CS
ECEn 224
20 Debounce
Page 20
© 2003-2008
BYU
Reduce FSM to Logic
NS = noisy•timerDone + CS•timerDone’
clrTimer = noisy’•CS’ + noisy•CS
debounced = CS
noisy
D Q
timerDone
debounced
CS
clrTimer
noisy
ECEn 224
20 Debounce
Page 21
© 2003-2008
BYU
Input “noisy” is Asynchronous
If pulse shorter than period, FSM may not see it
• Very small pulses may be missed by FSM
– This is not a real problem so we will live with it
ECEn 224
20 Debounce
Page 22
© 2003-2008
BYU
More on Asynchronous “noisy” Input
noisy
debounced
Finite
State
Machine
clk
clrTimer
timerDone
Timer
(5ms)
clk
reset
• This is the classic asynchronous input problem:
– FSM may see input change and change state
– Timer may not see input change and not clear timer
• Or vice versa
• Will this cause incorrect operation?
ECEn 224
20 Debounce
Page 23
© 2003-2008
BYU
Asynchronous Input Problem
noisy’/clrTimer
noisy•timerDone’
S0
noisy•timerDone
If you determine that a
problem may result, that is
easiest way to solve the
problem?
noisy’•timerDone
noisy’•timerDone’
Look at the transitions. Will
previous slide’s problem
cause a malfunction?
S1
debounced
noisy/clrTimer
ECEn 224
20 Debounce
Page 24
© 2003-2008
BYU
More on Asynchronous “noisy” Input
• What about metastability?
– Most buttons aren’t pushed very often
– Chance of metastability is very low
• We could eliminate all our asynchronous
problems by adding flip flops in series
– Avoid detailed analysis
– Play it safe and avoid possibility of mistakes
ECEn 224
20 Debounce
Page 25
© 2003-2008
BYU
Design of the Timer
ECEn 224
20 Debounce
Page 26
© 2003-2008
BYU
Timer Calculations
•
•
•
•
Assume system runs at 50 MHz (20 ns period)
5ms/20ns = 250,000 system clock cycles
We could design a MOD-250,000 counter
A simple 18-bit counter will work
– 218 is a bit longer than 250,000 (262,144)
– It is close enough to 5 ms for our purposes
ECEn 224
20 Debounce
Page 27
© 2003-2008
BYU
Design the Timer
• 19 input state machine
– 18 CS bits + 1 clrTimer bit
– Very, very large truth table
• A better approach:
– Register that selects between CS+1 and 0
– This is the technique of Chapter 12 (registers)
ECEn 224
20 Debounce
Page 28
© 2003-2008
BYU
Timer Structure
18
18
0
+1
18
18
0
D Q
18
18-input
AND
timerDone
1
clrTimer
clk
What can we do to simplify this circuit?
ECEn 224
20 Debounce
Page 29
© 2003-2008
BYU
Timer Structure
18
18
0
+1
18
18
0
D Q
18
18-input
AND
timerDone
1
clrTimer
clk
What can we do to simplify this circuit?
ECEn 224
20 Debounce
Page 30
© 2003-2008
BYU
Improved Timer Structure
18
18
+1
18
18
18
D Q
18-input
AND
timerDone
clrTimer
clk
This is a simpler way to
conditionally generate zeroes.
A synthesis tool likely would have
generated this from Verilog or
VHDL code containing a MUX
ECEn 224
20 Debounce
Page 31
© 2003-2008
BYU
Timer Structure
18
18
0
+1
18
18
0
D Q
18
18-input
AND
timerDone
1
clrTimer
clk
What can we do to simplify this circuit?
ECEn 224
20 Debounce
Page 32
© 2003-2008
BYU
Improved Timer Structure
18
18
+1
18
18
D Q
clrTimer
Cout
clk
timerDone
Use the carry out of the adder to detect rollover.
Output timerDone is delayed by one cycle, but this is
not a problem in our system.
ECEn 224
20 Debounce
Page 33
© 2003-2008
BYU
Building the +1 Circuit – Version #1
count[17:0]
“000000000000000001”
output
The adder could be built as outlined back in
Chapter 8 using full adder blocks.
However, half the full adder inputs will be ‘0’.
There ought to be a better way!
Hint: Any time a circuit has constant inputs
(0 or 1) then the circuit can be simplified!
ECEn 224
20 Debounce
Page 34
© 2003-2008
BYU
A Full-Adder with ‘0’ Inputs
Full Adder
A
‘0’
Cin
Half Adder
A
S
S
Cin
A
‘0’
‘0’
‘0’
Cin
A
Cin
‘0’
Cout
A
Cin
Cout
“Half Adder” adds two
bits instead of three
ECEn 224
20 Debounce
Page 35
© 2003-2008
BYU
Building the +1 Circuit - Version #2
A2
C2
Half
Adder
S2
A1
C1
Half
Adder
S1
ECEn 224
A0
C0
Half
Adder
S0
‘1’
Carry-in of ‘1’
gives us the +1
20 Debounce
Page 36
© 2003-2008
BYU
Building an 18-Bit AND
This is one way…
CAD tools are good at building
structures like this from lowerlevel building blocks.
Just describe the AND in Verilog
or VHDL and CAD tools will make a
good choice.
If target technology has special
structures for wide logic, CAD
tools likely will use it
ECEn 224
20 Debounce
Page 37
© 2003-2008
BYU
Debouncer Summary
• Structure is timer + FSM
• 2-state FSM makes NS logic trivial
• Asynchronous input “noisy” means we must be sure
our system works with any input timing
– If desired/needed, synchronize “noisy” input using one
or more flip flops
• Counter too large for conventional techniques
– Use MUX + register technique of Chapter 12
• Systems can usually be greatly simplified beyond the
obvious design by using careful analysis
ECEn 224
20 Debounce
Page 38
© 2003-2008
BYU