Class 17 - 8051 Interfacing
Class 17 - 8051 Interfacing
Introduction
Keypad
LCD Interfacing
Keypad
4 × 3 Keypad
Keypad
4 × 4 Keypad
Overview of Keypad
• A 4x4 keypad consists of 16 keys arranged in a matrix of 4 rows and 4
columns.
• Row and Column Pins: 4 rows and 4 columns, 8 total I/O pins required.
• Physical Layout:
Rows: R1, R2, R3, R4
Columns: C1, C2, C3, C4
4 × 4 Keypad
3 × 3 Keypad
4 × 4 Keypad
4 × 4 Keypad
Working Principle
• Each row is connected to a microcontroller I/O pin.
• Each column is connected to another set of I/O pins.
• The microcontroller outputs a low signal (logic 0) to one row at a time.
• It checks each column to see if any key is pressed (if there’s a logic low
signal).
• If a key is pressed, the corresponding row and column combination is
identified. The microcontroller then reads the key and performs the
required operation.
Algorithm to Detect a Key Press
While the basic concept of detecting a keypress using a matrix keypad is
clear, implementing it introduces a few challenges that the algorithm must
address to ensure accurate detection.
1. Debouncing
• Problem: Switches take time to complete the circuit, causing noisy
signals.
• Solution: Use a software delay to allow the signal to stabilize
before detecting the keypress.
Algorithm to Detect a Key Press
2. Ensuring Key Release
• Problem: The microcontroller must confirm that the previous key
has been released before detecting a new keypress.
• Solution: After detecting a keypress, make all rows logic zero and
check if all columns are logic one. If they are, no key is pressed,
and the system can look for a new keypress.
3. Keypress Detection
Process:
• The microcontroller scans one row at a time, outputting a low
signal.
• It checks all columns for a logic low signal, indicating a keypress.
Algorithm to Detect a Key Press
4. Key Encoding
• Problem: After detecting a key, the information must be converted
into a readable format (e.g., displaying on an LCD).
• Solution: Encode the keypress based on its row and column position
and perform the corresponding action.
Program 1
• Write an embedded C program for the 8051 microcontroller to identify
the key press on a 4x4 matrix keypad and display the corresponding
value on a display connected to one of the microcontroller's ports (e.g.,
Port 2).
#include <reg51.h> r2 = 0;
sbit r1 = P1^0;
Program 1
if (c1 == 0) key = '4';
else if (c2 == 0) key = '5';
sbit r2 = P1^1; else if (c3 == 0) key = '6';
sbit r3 = P1^2; else if (c4 == 0) key = 'B';
sbit r4 = P1^3; r2 = 1;
sbit c1 = P1^4;
sbit c2 = P1^5; r3 = 0;
sbit c3 = P1^6; if (c1 == 0) key = '7';
sbit c4 = P1^7; else if (c2 == 0) key = '8';
else if (c3 == 0) key = '9';
void main() { else if (c4 == 0) key = 'C';
unsigned char key = 0xFF; r3 = 1;
P1 = 0xFF; r4 = 0;
if (c1 == 0) key = '*';
while(1) { else if (c2 == 0) key = '0';
r1 = 0; else if (c3 == 0) key = '#';
if (c1 == 0) key = '1'; else if (c4 == 0) key = 'D';
else if (c2 == 0) key = '2'; r4 = 1;
else if (c3 == 0) key = '3';
else if (c4 == 0) key = 'A';
r1 = 1;
Program 1
r4 = 0;
if (c1 == 0) key = '*';
else if (c2 == 0) key = '0';
else if (c3 == 0) key = '#';
else if (c4 == 0) key = 'D';
r4 = 1;
if (key != 0xFF) {
P2 = key;
key = 0xFF;
}