Keypad interfaces:Keypad interface

Keypad interface

Context

● You are developing an embedded application using one or more members of the 8051 family of microcontrollers.

● The application has a time-triggered architecture, constructed using a scheduler.

● You are creating the user interface for your application.

Problem

How do you connect a small keypad, similar to that illustrated in Figure 20.1, to your application?

image

Background

See Chapter 19 for background information on the reading of single switches.

Solution

Basics

We are concerned here with keypads made up of a matrix of switches, in an arrange- ment similar to that illustrated in Figure 20.2.

Some key points to note are as follows:

● The matrix arrangement is used to save port pins. If we have R rows and C columns of keys, we need R + C pins if we use a matrix arrangement and R × C pins if we use individual switches. If you need six or more keys, then the matrix arrangement requires fewer pins.

image

[Note: The pull-up resistors may be omitted, as usual, if the port has internal pull-ups.]

Many keypads have 12 keys (‘0’–‘9’ plus two function keys – typically ‘#’ and ‘*’). Using a matrix arrangement, this requires seven port pins.

● The keys may bounce, when pressed and released.

● The duration of the key press will generally be at least 500 ms.

● The keys will not generally be allowed to ‘auto repeat’: this can be very confusing for users.

● We may wish the user to be able to press one or more ‘function keys’ in combina- tion with other keys.

Keypad scanning

At the heart of any keypad code is a scanning function: this will typically go through each column in turn and identify if any key in that column has been pressed.

Consider, for example, the keypad shown in Figure 20.3.

In Figure 20.3, the numbers adjacent to the rows and column indicate the port pins to which the keypad should be connected. Pins 0, 1 and 2 (the columns) will be referred to here as the output pins: these are written to during the scanning process. Pins 3, 4, 5 and 6 will be referred to as the input pins: these are read during the scan- ning process.

image

Suppose we wish to see if the key ‘1’ is pressed. We can proceed as follows:

● We set the corresponding output pin (in this case, Pin 6) to a Logic 0 value and the remaining output pins to a Logic 1 value.

● We read the required input pin (in this case, Pin 0).

If this pin is a Logic 1, then the key ‘1’ is not pressed: the Logic 1 value is, instead, obtained via the pull-up resistor on the port pin.

If this pin is at Logic 0, then the key is being pressed (subject to debounce con- siderations): the Logic 0 voltage reading results from the Logic 0 voltage output on Pin 6.

● We must repeat the reading (say) 200 ms later, to allow for switch bounce.

We need to repeat this process for every key. Listing 20.1 illustrates one way of per- forming this scanning for the whole keypad.

image

image

image

Function keys

Note that more than one key may be pressed at the same time. The ‘function keys’ (‘#’ and ‘*’) in Listing 20.1 illustrate how we can make use of multiple key depressions.

The main code example presented with this pattern illustrate the use of function keys.

Buffer arrangements

In most scheduled keypad routines, it is useful to have a small buffer, so that key presses are not lost if the system is unable to process them immediately.

Numerous buffer arrangements are possible: the main code example presented with this pattern illustrates one possibility.

Leave a comment

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