Switch interfaces

Introduction

Reading the status of one or more push-button switches is a very common require- ment in embedded applications. The four patterns in this chapter describe different solutions to this problem.

The patterns are as follows:

SWITCH INTERF ACE ( SOFTW ARE ) [page 399]

Uses a minimum of external hardware. Reads a single switch, debounces it and reports its status

SWITCH INTERF ACE ( HARDW ARE ) [page 410]

Use external hardware to perform switch debouncing. Increased cost (compared with SWITCH INTERF ACE ( SOFTW ARE ) ), but much higher level of protection against ESD, malicious damage etc.

ON OFF SWITCH [page 414]

Building on SWITCH INTERF ACE ( SOFTW ARE ) or SWITCH INTERF ACE ( HARD

W ARE ) this pattern provides, through software, the following behaviour:

Assume that the switch is in the OFF state. It remains in this state until it is pressed. When pressed, the state changes to ON. It remains in this state until it is pressed. When pressed, the state changes to OFF.

This type of behaviour can be used, for example, to allow a single (non-latching) switch to control a piece of machinery.

MUL TI ST A TE SWITCH [page 423]

Building on SWITCH INTERF ACE ( SOFTW ARE ) or SWITCH INTERF ACE ( HARD

W ARE ) this pattern provides, through software, the following behaviour:

Assume that the switch state is in the OFF state. The switch is pressed and briefly held. The switch state changes to ‘ON STATE 1’. The user continues to depress the switch. The switch state becomes ‘ON STATE 2’, etc. Releasing the switch (at any time) puts the switch back in the OFF state.

That is, the switch state depends on the duration of the switch press. Any number of ‘ON’ states may be supported (although having more than around three is generally very confusing for the user).

This type of behaviour can be useful when, for example, setting time on a clock. Pressing the set switch will initially cause the displayed time to change slowly; sus- tained depressions will cause the displayed time to increase more rapidly.

Note: For reasons discussed in SWITCH INTERF ACE ( SOFTW ARE ) , in ‘Safety and reliability’, we are only concerned with push-button switches in these patterns: ‘toggle’ or ‘latching’ switches are not considered (and the use of such switches is not recommended).

Leave a comment

Your email address will not be published. Required fields are marked *