[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++);
}