0% found this document useful (0 votes)
19 views17 pages

Class 17 - 8051 Interfacing

The document provides an overview of hardware interfacing with a focus on keypad interfacing, specifically 4x4 and 3x3 keypads. It explains the working principle of key detection, including challenges like debouncing and key release, and presents an embedded C program for the 8051 microcontroller to identify key presses and display corresponding values. The document includes details on the physical layout, algorithm for key detection, and example code for implementation.

Uploaded by

suraj.k2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views17 pages

Class 17 - 8051 Interfacing

The document provides an overview of hardware interfacing with a focus on keypad interfacing, specifically 4x4 and 3x3 keypads. It explains the working principle of key detection, including challenges like debouncing and key release, and presents an embedded C program for the 8051 microcontroller to identify key presses and display corresponding values. The document includes details on the physical layout, algorithm for key detection, and example code for implementation.

Uploaded by

suraj.k2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Hardware Interfacing

Introduction

Keypad

Seven Segment LED Display

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.

• Used for user input (like PINs, password entry, etc.).

• 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;
}

while (P1 != 0xFF);


}
}
#include <reg51.h>
Program 1
void main() {
unsigned char key = 0xFF;
unsigned char row, col;
unsigned char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D’} };
P1 = 0xFF;
while(1) {
for (row = 0; row < 4; row++) {
P1 = 0xFF;
P1 &= ~(0x01 << row);
for (col = 0; col < 4; col++) {
if (!(P1 & (0x10 << col))) {
key = keys[row][col];
P2 = key;
while (P1 != 0xFF); } } } }
}

You might also like