0% found this document useful (0 votes)
203 views8 pages

Experiment No. 7 Interfacing Keys, Keypad To Microcontroller

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Experiment No.

7
Interfacing Keys, Keypad to Microcontroller
-------------------------------------------------------------------------------------------------------------------Name of Student:
-------------------------------------------------------------------------------------------------------------------Day & Date of Submission:
Checked and verified by:
--------------------------------------------------------------------------------------------------------------------

Department of Instrumentation & Control


College of Engineering Pune

Experiment No. 7
Aim: To study, understand operation of a keys, Keypad, Problems and interfacing of
Keys, keypad to 8051 Microcontroller.

Objectives:
i.

To understand the basic operation of a keys such as push button, key pads
and different types of Keys and keyboards

ii.

To understand debouncing mechanism of keys and elimination menthods

iii.

To write algorithm, flowchart and program for interfacing keypad to


Microcontroller

iv.

To explore keypad interfacing of keypad to various microcontrollers

Theory:
Keyboards andLCD's are the most widely used input/output devices of the 8051.At the
lowest level, keyboards are organized in a matrix of rows and columns.The CPU
accesses both rows and columns through ports;therefore,with two 8-bit ports,an 8 x 8
matrix of keys can be connected toa microprocessor.When a key is pressed,a row and a
column make a contact; otherwise, there is no connection between rows and columns.
The rows are connected to an output port and the columns are connected to an input
port.If no key has been pressed ,reading the input port will yield 1s for all columns
since they are all connected to high (Vcc).If all the rows are grounded and a key is
pressed, one of the column will have 0 since the key pressed provides the path to
ground.It is the function of the microcontroller to scan the keyboard continuously to
detect and identify the key pressed.
In IBM PC keyboards, a single microcontroller (consisting ofa microprocessor, RAM
and EPROM, and several ports all on a single chip) takes care of hardware and software
interfacing of the keyboard.In such systems, it is the function of programs stored in the

EPROM of the microcontroller to scan the keys continuously, identify which one has
been activated, and presentit to the motherboard.

Matrix Keyboard Connection to Ports

Problem Statements:
1. Interface the keypad with 8051 and display the pressed key on the LCD .

Solution: (Design, Algorithm, Program and result)


Program:
#include<reg51.h>
sbit r1=P2^0;
sbit r2=P2^1;
sbit r3=P2^2;
sbit r4=P2^3;

sbit c1=P2^4;
sbit c2=P2^5;
sbit c3=P2^6;
sbit c4=P2^7;
sbit rs=P0^0;
sbit rw=P0^1;
sbit e=P0^2;

unsigned int i,j,count;


void delay();
void lcdcmd();
void lcddata();
void enable();
void lcd_init();
void lcd_display(char x);
void delay()
{

for(i=0;i<6000;i++);

void ms_delay(int k)
{

for(j=0;j<k;j++)
{ for(i=0;i<922;i++); }

void lcdcmd()
{

rs=0; //RS
rw=0; //RW}

void lcddata()
{ rs=1; rw=0;

void enable()
{

e=0; // ENABLE
e=1;e=0;

void lcd_init()
{

lcdcmd();
P1=0x38;

//2 lines and 5x7 matrix

enable();delay();
P1=0x06;

//Increment cursor (shift cursor to right)

enable();delay();
P1=0x0e;

//Display on, cursor blinking

enable();delay();
P1=0x01;

//Clear display screen

enable();delay();

void lcd_display(char x)
{

lcddata();P1=x;
enable();delay();

void main (void)


{

lcd_init();

count=0;

while(1)
{ P2=0xfe;
if(c1==0)
{ms_delay(30);lcd_display('7');count++; }
if(c2==0)
{ms_delay(30);lcd_display('8');count++;}
if(c3==0)
{ms_delay(30);lcd_display('9');count++;}
if(c4==0)
{ms_delay(30);lcd_display('/');count++;}

P2=0xfd;
if(c1==0)
{ms_delay(30);lcd_display('4');count++; }
if(c2==0)
{ms_delay(30);lcd_display('5');count++;}
if(c3==0)
{ms_delay(30);lcd_display('6');count++;}
if(c4==0)
{ms_delay(30);lcd_display('*');count++;}

P2=0xfb;
if(c1==0)
{ms_delay(30);lcd_display('1');count++; }
if(c2==0)
{ms_delay(30);lcd_display('2');count++;}
if(c3==0)
{ms_delay(30);lcd_display('3');count++;}
if(c4==0)
{ms_delay(30);lcd_display('-');count++;}

P2=0xf7;
if(c1==0)
{ms_delay(30);lcd_display('C');count++; }
if(c2==0)
{ms_delay(30);lcd_display('0');count++;}
if(c3==0)
{ms_delay(30);lcd_display('=');count++;}
if(c4==0)
{ms_delay(30);lcd_display('+');count++;}

if(count==16)
{lcdcmd();
P1=0xc0;
enable();
delay();}
}
}
Design:

Conclusion:

//Force cursor to beginning of second line

What did you learn?

You might also like