You are on page 1of 4

26-Jun-12

Objectives
Explain the basic operation of a keypad. Describe the key press and detection mechanisms. Interface a keypad to the PIC18F4550 microcontroller.

Input and Output Interfacing: Keypad

Keypad Interfacing
Keypad or keyboards are the most widely used I/O devices. Most of microcontroller applications require a keypad for users to enter numbers and commands. A matrix keypad provides a simple data entry device for microcontroller systems.

Basic Operation
Organized in a matrix of rows and columns. Consists of an array of momentary pushbuttons switch or key. There are 4 column lines and 4 row lines. Each pushbutton has two terminals; one is connected to a column line and other to a row line.

Internal Connection of a 4 x 4 Keypad

Internal Structure of 4x4 Matrix Keypad.

26-Jun-12

Basic Operation
Initially, there are not direct connection between row and column. When a key is pressed, the adjacent row and column are connected, thus a row and column make a contact; otherwise there is no connection between rows and columns. For example if key 8 is pressed, Row 3 and Column 2 will connect to each other.

Key Press and Detection Mechanisms


In programming there are 2 processes: Key press detection Interrupt method Scanning method only this method will be covered Key identification

Scanning Method for Key Press Detection


As example:
If Row 1 is set to LOW, when switch 3 is pressed, the Column 3 will become LOW

Flowchart for Scanning Method


To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, then it reads the columns. If the data read from the columns are equal to 1111, no key has been pressed, and the process continues until a key press has occurred. If one of the column bits has a zero, this means that a key press has occurred. After a key press is detected, the microcontroller will go through the process of identifying the key.

26-Jun-12

Flowchart for Scanning Method


Starting with the top row, the microcontroller grounds it by providing a LOW to the first row only; then it reads the columns. If the data read is all 1s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed.

Keypad Interfacing PTK40A Schematic


Keypad can directly being connected to MCU or used keypad decoder IC. MCU accesses both rows and columns through ports; therefore with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microcontroller.

Keypad Interfacing PTK40A Schematic


The keypads pins need to be pulled up or pulled down to avoid floating cases 4 x 4 matrix keypad connected to PORTB and PORTD Rows PORTB.Low(RB3 - RB0) Columns PORTB.High (RB7 - RB4)

Keyboard Debounce

Noise

Stable

26-Jun-12

Keypad Programming
Steps (based on connection in Slide 3):
Make Row 1 = 0 Delay Check every column
If Column 1 = 0, return 1 If Column 2 = 0, return 2 If Column 3 = 0, return 3 If Column 4 = 0, return A

Keypad Programming (continued)


unsigned char Read_Keypad() { KP_R1 = 0; KP_R2 = 1; KP_R3 = 1; KP_R4 = 1; __delay_us(30); if(KP_C1==0) return 1; if(KP_C2==0) return 2; if(KP_C3==0) return 3; if(KP_C4==0) return A; return 0xFF; } //indicates nothing being pressed //return pressed key value //make Row1=0 //the rest =1

The process continue by making Row2, Row3, and Row 4 low one by one and then check the column.