Reduce Transient Signals by Using Debouncing Logic

Why Debounce Signals

When a switch opens and closes, the switch contacts can bounce off each other
before the switch completely transitions to an on or off state. The bouncing action
can produce transient signals that do not represent a true change of state.
Therefore, when modeling switch logic, it is important to filter out transient
signals by using debouncing algorithms.

If you model a controller in a Stateflow® chart, you do not want your switch logic to overwork the controller by
turning it on and off in response to every transient signal it receives. To avoid
this, design a Stateflow controller that uses temporal logic to debounce your input signals and
determine whether a switch is actually on or off.

The initial state for this model is Off. Using the
duration operator, you can control what state the model is in
based on how long the switch signal, sw, has been greater or less
than zero. Once sw has been greater than or equal to zero for
longer than 0.1 seconds, the switch moves from state Off to state
On. Then, if sw has been less than zero
for longer than 0.01 seconds, the switch moves from state On to
state Off.

State Logic

The debouncer has two states, On and
Off. The duration operator controls which
state is active. The logic works as described in this table.

Run the Debouncer

The scope shows how the debouncer isolates transient signals from the
noisy input signal.

Debounce Signals with Fault Detection

The model sf_debouncer
illustrates a design pattern that uses temporal logic and an intermediate state to
isolate transient signals. With this design pattern, you can also include logic to
detect faults and allow your system time to recover.

The Debouncer chart contains this logic.

The debouncer design uses the after(n, sec)
statement to implement absolute-time temporal logic. The keyword
sec defines simulation time that has elapsed since activation
of a state.

State Logic

The debouncer chart contains an intermediate state called Debounce. The
Debounce state isolates transient inputs by checking whether the signals retain
their positive or negative values, or fluctuate between zero crossings over a
prescribed period. The logic works as shown in this table.

Input Signal

State

Transitions

Result

Retains positive value for 0.1
second

Debounce.On

On

Switch turns on

Retains negative value for 0.1
second

Debounce.Off

Off

Switch turns off

Fluctuates between zero crossings for 0.3
second

Debounce

Off.Fault

Note

The Debounce to Off.Fault transition comes from a
higher level in the chart hierarchy and overrides
the transitions from the Debounce.Off and
Debounce.On substates.

Chart isolates the input as a transient signal and
gives it time to recover.

Run the Debouncer

The scope shows how the debouncer isolates transient signals from the
noisy input signal.

Use Event-Based Temporal Logic

As an alternative to absolute-time temporal logic, you can apply event-based
temporal logic to determine true state in the Debouncer chart by using the
after(n, tick) statement. The keyword
tick specifies and implicitly generates a local event when
the chart awakens.

The Error Generator block in the sf_debouncer model generates a
pulse signal every 0.001 second. Therefore, to convert the absolute-time temporal
logic specified in the Debouncer chart to event-based logic, multiply the
n argument by 1000, as follows.

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic. By continuing to use this website, you consent to our use of cookies. Please see our Privacy Policy to learn more about cookies and how to change your settings.