Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C Program to Interface Keypad Using 8051

Status
Not open for further replies.

Shan Ahmed

Newbie level 2
Newbie level 2
Joined
Sep 23, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
I wanna make a program in c which will scan the keypad and send the result to first serial port.
P1.0 to P1.3 stands for columns and P2.0 to P2.3 stands for rows. Serial port should have a band 56k, 8 bit and 1 stop bit.
Help me do + understand it.. Thanx
A beginner is here..
 

The coding method I followed is implementing small functions: column_scan, row_scan and then the main. You may also need a 2-D array as lookup for getting the value of the key that is pressed.

Once you get the keypad value, sending it through serial port is easy.
 
The coding method I followed is implementing small functions: column_scan, row_scan and then the main. You may also need a 2-D array as lookup for getting the value of the key that is pressed.

Once you get the keypad value, sending it through serial port is easy.

Thanx for your reply. I have tried this one? what you say about it?? What possible amendments should be made in it for my requirement??
Code:
[CODE]#include <stdio.h> 		//Define I/O Functions
#include <reg51.h> 		//Define 8051 Registers
#define DATA P1 		//Define DATA to Port1
void Key_Scan(void); 	//KeyScan Function
void DelayMs(int); 		//DelayMs Function
sbit RS = P3^5; 		//Register Select
unsigned char R,C,ch;
unsigned int i=0;
unsigned char Key[4][4] = {'1','2','3','4', 
                           '5','6','7','8',
                           '9','0','A','B',
                           'C','D','E','F',
                           };
  // Main Function
void main()
{ 
while(1)
{
Key_Scan();
ch = Key[C][R]; 	//Assign Key value to ch;
SBUF=ch; 			//[C][R] denotes Column 
DelayMs(35); 		//and Row Value of Keypad
DelayMs(35);
}
}
  // Key Scan Funcion
void Key_Scan(void) 
{
unsigned int i = 0;
 					//Scanning for Row Value 
P2 = 0x0F; 			//Initialize Port2 to 0FH
while(P2 == 0x0F);
if(P2 == 0x0E) 		//Checking for Row0
R = 0;
else if(P2 == 0x0D) 	//Checking for Row1
R = 1;
else if(P2 == 0x0B) 	//Checking for Row2
R = 2;
else if(P2 == 0x07) 	//Checking for Row3
R = 3;
 					//Scanning for Column Value
P1 = 0x0F; 			//Initialize Port1 to 0FH
while(P2 == 0x0F);
if(P2 == 0x0E) 		//Checking for Column0
C = 0;
else if(P2 == 0x0D) 	//Checking for Column1
C = 1;
else if(P2 == 0x0B) 	//Checking for Column2
C = 2;
else if(P2 == 0x07) 	//Checking for Column3
C = 3;
DelayMs(50);
}
//Delay Function
void DelayMs(int k)
{
unsigned int a;
for(a=0;a<=k;a++);
}
[/CODE]
 

In the program you made, the rows are upper/lower nibble..?? You didn't specify this..

Also, while interfacing keypad, the rows are normally output pins and the columns are input pins.
So finding out columns is easy. Just check which column is low and thats the correct column.

But for row you have a different procedure.
You need to make only one row low(remaining high) at a time (the row & col where row is pressed will be short and giving high to row makes column also high)and check whether the status of the columns. If all columns are high then the row which was low is not the required row.
Then we shift to the next row. The row for which all the columns are not high is the correct row.
Getting the row is not as easy as column, but its not that difficult too..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top