Previous page

Table of contents

Chapter overview

Next page

 

Keyboard


Keyboards are mechanical devices used to execute a break or make connection between two points. They come in different sizes and with different purposes. Keys that are used here are also called "dip-keys". They are soldered directly onto a printed board and are often found in electronics. They have four pins (two for each contact) which give them mechanical stability.

Example of connecting keys to microcontroller pins.


Key function is simple. When we press a key, two contacts are joined together and connection is made. Still, it isn't all that simple. The problem lies in the nature of voltage as an electrical dimension, and in the imperfection of mechanical contacts. That is to say, before contact is made or cut off, there is a short time period when vibration (oscillation) can occur as a result of unevenness of mechanical contacts, or as a result of the different speed in pressing a key (this depends on person who presses the key). The term given to this phenomena is called SWITCH (CONTACT) DEBOUNCE. If this is overlooked when program is written, an error can occur, or the program can produce more than one output pulse for a single key press. In order to avoid this, we can introduce a small delay when we detect the closing of a contact. This will ensure that the press of a key is interpreted as a single pulse. The debounce delay is produced in software and the length of the delay depends on the key, and the purpose of the key. The problem can be partially solved by adding a capacitor across the key, but a well-designed program is a much-better answer. The program can be adjusted until false detection is completely eliminated.  
In some case a simple delay will be adequate but if you want the program to be attending to a number of things at the same time, a simple delay will mean the processor is "doing-nothing" for a long period of time and may miss other inputs or be taken away from outputting to a display.
The solution is to have a program that looks for the press of a key and also the release of a key. The macro below can be used for keypress debounce.

The above macro has several arguments that need to be explained:

TESTER macro HiLo, Port, Bit, Delay, Address 

HiLo can be '0' or '1' which represents rising or falling edge where service subprogram will be executed when you press a key. 
Port is a microcontroller's port to which a key is connected. In the case of a PIC16F84 microcontroller, it can be PORTA or PORTB.
Bit is port's pin to which the key is connected.
Delay is a number from 0 to 255, used to assign the time needed for key debounce detection - contact oscillation - to stop. It is calculated as TIME = Delay x 1ms.
Address is the address where the micro goes after a key is detected.  The sub-routine at the address carries out the required instruction for the keypress. 

Example 1: TESTER 0, PORTA, 3, .100, Tester1_above

Key-1 is connected to RA0 (the first output of port A) with a delay of 100 microseconds and a reaction to logic zero. Subprogram that processes key is found at address of label Tester1_above.

Example2: TESTER 0, PORTA, 2, .200, Tester2_below

Key-2 is connected to RA1 (the second output of port A) with 200 mS delay and a reaction to logic one. Subprogram that processes key is found at address of label Tester2_below.

The next example shows the use of macros in a program. TESTER.ASM turns LED on and off. The LED is connected to the seventh output of port B. Key-1 is used to turn LED on. Key-2 turns LED off.

 

Previous page

Table of contents

Chapter overview

Next page

 

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.