Accelerating the pace of engineering and science

# Documentation

## Debounce Signals

### 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 using a process called debouncing.

For example, 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. Instead, you can design a Stateflow debouncer that uses temporal logic to determine whether the switch is really on or off.

### The Debouncer Model

The model sf_debouncer illustrates a design pattern that uses temporal logic to isolate transient signals.

The Debouncer chart contains the following logic:

### Key Behaviors of Debouncer Chart

The key behaviors of the Debouncer chart are:

#### Intermediate Debounce State Isolates Transients

In addition to the states On and Off, 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 of time. The logic works as follows.

If the input signal...Then this state...Transitions to...And the...

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

#### Temporal Logic Determines True State

The debouncer design pattern uses temporal logic to:

• Determine whether the input signal is normal or transient

• Give transient signals time to recover and return to normal state

Use Absolute-Time Temporal Logic.  The debouncer design uses the after(n, sec) operator to implement absolute-time temporal logic (see Operators for Absolute-Time Temporal Logic). The keyword sec defines simulation time that has elapsed since activation of a state.

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) operator (see Operators for Event-Based Temporal Logic). The keyword tick specifies and implicitly generates a local event when the chart awakens (see Control Chart Execution Using Implicit Events).

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.

Absolute Time-Based LogicEvent-Based Logic
after ( 0.1, sec )after ( 100, tick )
after ( 0.3, sec )after ( 300, tick )
after ( 1, sec )after ( 1000, tick )

### Run the Debouncer

To run the sf_debouncer model, follow these steps:

1. Open the model by clicking sf_debouncersf_debouncer or typing sf_debouncer at the MATLAB® command prompt.

2. Open the Stateflow chart Debouncer and the Scope block.

3. Simulate the chart.

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

 Note:   To debounce the signals using event-based logic, change the Debouncer chart as described in Use Event-Based Temporal Logic and simulate the chart again. You should get the same results.