Aim:
Matrix Keypads are commonly used in calculators, telephones etc where a 
number of input switches are required. A keypad is a set of buttons 
arranged in a block or “pad” which usually bear digits, symbols and 
usually a complete set of alphabetical letters. If it mostly contains 
numbers then it can also be called a numeric keypad. Keypads are found 
on many alphanumeric keyboards and on other devices such as calculators,
 push-button telephones, combination locks, and digital door locks, 
which require mainly numeric input.
Matrix Keypads are commonly used in calculators, telephones etc where a 
number of input switches are required. A keypad is a set of buttons 
arranged in a block or “pad” which usually bear digits, symbols and 
usually a complete set of alphabetical letters. If it mostly contains 
numbers then it can also be called a numeric keypad. Keypads are found 
on many alphanumeric keyboards and on other devices such as calculators,
 push-button telephones, combination locks, and digital door locks, 
which require mainly numeric input.

Description:
We know that matrix keypad is made by arranging push button switches in 
row and columns. In the straight forward way to connect a 4×4 keypad (16
 switches) to a microcontroller we need 16 inputs pins. But by 
connecting switches in the following way we can read the status of each 
switch using 8 pins of the microcontroller.
The status of each keys can be determined by a process called Scanning. 
For the sake of explanation lets assume that all the column pins (Col1 –
 Col4) are connected to the inputs pins and all the row pins are 
connected to the output pins of the microcontroller. In the normal case 
all the column pins are pulled up (HIGH state) by internal or external 
pull up resistors. Now we can read the status of each switch through 
scanning.
- A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
 
- Now each Column is scanned. If any switch belongs to 1strow is pressed corresponding column will pulled down (logic LOW) and we can detect the pressed key.
 
- This process is repeated for all rows.
 
The keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3 
and C4.  R1 to R4 represents the four rows and C1 to C4 represents the 
four columns. When a particular key is pressed the corresponding row and
 column to which the terminals of the key are connected gets shorted. 
For example if key 1 is pressed row R1 and column C1 gets shorted and so
 on. The program identifies which key is pressed by a method known as 
column scanning. In this method a particular row is kept low (other rows
 are kept high) and the columns are checked for low. If a particular 
column is found low then that means that the key connected between that 
column and the corresponding row (the row that is kept low) is been 
pressed. For example if  row R1 is initially kept low and column C1 is 
found low during scanning, that means key 1 is pressed.
We know that matrix keypad is made by arranging push button switches in 
row and columns. In the straight forward way to connect a 4×4 keypad (16
 switches) to a microcontroller we need 16 inputs pins. But by 
connecting switches in the following way we can read the status of each 
switch using 8 pins of the microcontroller.

The status of each keys can be determined by a process called Scanning. 
For the sake of explanation lets assume that all the column pins (Col1 –
 Col4) are connected to the inputs pins and all the row pins are 
connected to the output pins of the microcontroller. In the normal case 
all the column pins are pulled up (HIGH state) by internal or external 
pull up resistors. Now we can read the status of each switch through 
scanning.
- A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
 - Now each Column is scanned. If any switch belongs to 1strow is pressed corresponding column will pulled down (logic LOW) and we can detect the pressed key.
 - This process is repeated for all rows.
 
The keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3 
and C4.  R1 to R4 represents the four rows and C1 to C4 represents the 
four columns. When a particular key is pressed the corresponding row and
 column to which the terminals of the key are connected gets shorted. 
For example if key 1 is pressed row R1 and column C1 gets shorted and so
 on. The program identifies which key is pressed by a method known as 
column scanning. In this method a particular row is kept low (other rows
 are kept high) and the columns are checked for low. If a particular 
column is found low then that means that the key connected between that 
column and the corresponding row (the row that is kept low) is been 
pressed. For example if  row R1 is initially kept low and column C1 is 
found low during scanning, that means key 1 is pressed.
Block Diagram
Schematic

Code
// *********************************************************
// Project: Interfacing keypad to 8051 microcontroller
// Author: Code Bloges
// Module description: Operate keypad
// *********************************************************
#include<reg51.h>
//Function declarations
void cct_init(void);
void delay(int);
void Return(void);
char READ_SWITCHES(void);
char get_key(void);
sbit RowA = P1^0;     //RowA
sbit RowB = P1^1;     //RowB
sbit RowC = P1^2;     //RowC
sbit RowD = P1^3;     //RowD
sbit C1   = P1^4;     //Column1
sbit C2   = P1^5;     //Column2
sbit C3   = P1^6;     //Column3
sbit C4   = P1^7;     //Column4
int main(void)
{
   char key;                // key char for keeping record of pressed key
   cct_init();              // Make input and output pins as required
   while(1)
   {
     P2 = get_key();       // Get pressed key
   }
}
void cct_init(void)
{
        P0 = 0x00;   //not used
        P1 = 0xf0;   //used for generating outputs and taking inputs from Keypad
        P2 = 0x00;   //used as data port for LCD
        P3 = 0x00;   //used for RS and E  
}
void delay(int a)
{
   int i;
   for(i=0;i<a;i++);   //null statement
}
char READ_SWITCHES(void)     
{     
        RowA = 0; RowB = 1; RowC = 1; RowD = 1;      //Test Row A
        if (C1 == 0) { delay(10000); while (C1==0); return 0x06; }
        if (C2 == 0) { delay(10000); while (C2==0); return 0x5B; }
        if (C3 == 0) { delay(10000); while (C3==0); return 0x4F; }
        if (C4 == 0) { delay(10000); while (C4==0); return 0x77; }
        RowA = 1; RowB = 0; RowC = 1; RowD = 1;      //Test Row B
        if (C1 == 0) { delay(10000); while (C1==0); return 0x66; }
        if (C2 == 0) { delay(10000); while (C2==0); return 0x6D; }
        if (C3 == 0) { delay(10000); while (C3==0); return 0x7D; }
        if (C4 == 0) { delay(10000); while (C4==0); return 0x7C; }
        RowA = 1; RowB = 1; RowC = 0; RowD = 1;      //Test Row C
        if (C1 == 0) { delay(10000); while (C1==0); return 0x07; }
        if (C2 == 0) { delay(10000); while (C2==0); return 0x7F; }
        if (C3 == 0) { delay(10000); while (C3==0); return 0x6F; }
        if (C4 == 0) { delay(10000); while (C4==0); return 0x39; }
        RowA = 1; RowB = 1; RowC = 1; RowD = 0;      //Test Row D
        if (C1 == 0) { delay(10000); while (C1==0); return 0x5E; }
        if (C2 == 0) { delay(10000); while (C2==0); return 0x3F; }
        if (C3 == 0) { delay(10000); while (C3==0); return 0x79; }
        if (C4 == 0) { delay(10000); while (C4==0); return 0x71; }
        return 'n';                 // Means no key has been pressed
}
char get_key(void)           //get key from user
{
        char key = 'n';              //assume no key pressed
        while(key=='n')              //wait untill a key is pressed
                key = READ_SWITCHES();   //scan the keys again and again
        return key;                  //when key pressed then return its value
}
// ********************************************************* // Project: Interfacing keypad to 8051 microcontroller // Author: Code Bloges // Module description: Operate keypad // ********************************************************* #include<reg51.h> //Function declarations void cct_init(void); void delay(int); void Return(void); char READ_SWITCHES(void); char get_key(void); sbit RowA = P1^0; //RowA sbit RowB = P1^1; //RowB sbit RowC = P1^2; //RowC sbit RowD = P1^3; //RowD sbit C1 = P1^4; //Column1 sbit C2 = P1^5; //Column2 sbit C3 = P1^6; //Column3 sbit C4 = P1^7; //Column4 int main(void) { char key; // key char for keeping record of pressed key cct_init(); // Make input and output pins as required while(1) { P2 = get_key(); // Get pressed key } } void cct_init(void) { P0 = 0x00; //not used P1 = 0xf0; //used for generating outputs and taking inputs from Keypad P2 = 0x00; //used as data port for LCD P3 = 0x00; //used for RS and E } void delay(int a) { int i; for(i=0;i<a;i++); //null statement } char READ_SWITCHES(void) { RowA = 0; RowB = 1; RowC = 1; RowD = 1; //Test Row A if (C1 == 0) { delay(10000); while (C1==0); return 0x06; } if (C2 == 0) { delay(10000); while (C2==0); return 0x5B; } if (C3 == 0) { delay(10000); while (C3==0); return 0x4F; } if (C4 == 0) { delay(10000); while (C4==0); return 0x77; } RowA = 1; RowB = 0; RowC = 1; RowD = 1; //Test Row B if (C1 == 0) { delay(10000); while (C1==0); return 0x66; } if (C2 == 0) { delay(10000); while (C2==0); return 0x6D; } if (C3 == 0) { delay(10000); while (C3==0); return 0x7D; } if (C4 == 0) { delay(10000); while (C4==0); return 0x7C; } RowA = 1; RowB = 1; RowC = 0; RowD = 1; //Test Row C if (C1 == 0) { delay(10000); while (C1==0); return 0x07; } if (C2 == 0) { delay(10000); while (C2==0); return 0x7F; } if (C3 == 0) { delay(10000); while (C3==0); return 0x6F; } if (C4 == 0) { delay(10000); while (C4==0); return 0x39; } RowA = 1; RowB = 1; RowC = 1; RowD = 0; //Test Row D if (C1 == 0) { delay(10000); while (C1==0); return 0x5E; } if (C2 == 0) { delay(10000); while (C2==0); return 0x3F; } if (C3 == 0) { delay(10000); while (C3==0); return 0x79; } if (C4 == 0) { delay(10000); while (C4==0); return 0x71; } return 'n'; // Means no key has been pressed } char get_key(void) //get key from user { char key = 'n'; //assume no key pressed while(key=='n') //wait untill a key is pressed key = READ_SWITCHES(); //scan the keys again and again return key; //when key pressed then return its value }
Downloads:
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
Further Reading suggestions:
 You may also like,
- Interfacing with UART of 8051 controller
 
- Interfacing SPI communication with 8051
 
- 8051 Displaying Custom Characters on LCD 
 
- 8051 Graphical LCD
 
- RTC interfacing using I2C in 8051
 
- Interfacing ultrasonic sensor with 8051
 
- Interfacing GPS Modu with 8051
 
- Interfacing GSM Module with 8051
 
- ADC Module interfacing with 8051
 
- Interfacing PWM in 8051
 
- Interfacing with UART of 8051 controller
 - Interfacing SPI communication with 8051
 - 8051 Displaying Custom Characters on LCD
 - 8051 Graphical LCD
 - RTC interfacing using I2C in 8051
 - Interfacing ultrasonic sensor with 8051
 - Interfacing GPS Modu with 8051
 - Interfacing GSM Module with 8051
 - ADC Module interfacing with 8051
 - Interfacing PWM in 8051
 

No comments:
Post a Comment