4X4 KEYPAD INTERFACING WITH 8051 MICROCONTROLLER
- We know that Keypads are very common amongst the widely used technology present in today’s generation.
- They are very important as they are used to interface with various electronic devices employed in security purpose.
- Examples of keyboard interfacing devices:-
- Automatic teller machine.
- So basically there will be 16 pin input connection as a total of 16 switches will be interfaced with the Liquid Crystal display via the 8051 microcontroller device.
#define display_port P2 //Data pins connected to port 2 on microcontroller
sbit rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3
sbit e = P3^4; //E pin connected to pin 4 of port 3
sbit C4 = P1^0; // Connecting keypad to Port 1
sbit C3 = P1^1;
sbit C2 = P1^2;
sbit C1 = P1^3;
sbit R4 = P1^4;
sbit R3 = P1^5;
sbit R2 = P1^6;
sbit R1 = P1^7;
sbit DB0 = P2^0;
sbit DB1 = P2^1;
sbit DB2 = P2^2;
sbit DB3 = P2^3;
sbit DB4 = P2^4;
sbit DB5 = P2^5;
sbit DB6 = P2^6;
sbit DB7 = P2^7;
void msdelay(unsigned int time) // Function for creating delay in milliseconds.
unsigned i,j ;
void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
display_port = command;
void lcd_data(unsigned char disp_data) //Function to send display data to LCD
display_port = disp_data;
void lcd_init() //Function to prepare the LCD and get it ready
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
lcd_cmd(0x0F); // turn display ON, cursor blinking
lcd_cmd(0x01); //clear screen
lcd_cmd(0x81); // bring cursor to position 1 of line 1
void row_finder1() //Function for finding the row for column 1
void row_finder2() //Function for finding the row for column 2
void row_finder3() //Function for finding the row for column 3
void row_finder4() //Function for finding the row for column 4
KEYPAD CONNECTION DIAGRAM:
- Initially all switches are assumed to be released.
- When any of the switch is pressed the corresponding row and column are short circuited.
- Write all logic 0’s to all the rows whereas logic 1’s to all the columns.
BLACK –>Logic 0
Red –> Logic 1
- The job of the software is to scan the pins connected to that position. If a key is pressed logic 0 is driven to C2 due to short circuiting with the logic 1 of the corresponding row( R2) to the pressed keypad switch.
- As soon as the key has been pressed in column C2, the software’s job is to write the logic 1’s to each row sequentially until C2 becomes high.
- The value written to that row will be reflected in that column as short circuit will happen at that junction.
- The logic in C2 remains high until high logic from a row meets the pressed button in C2.
- Therefore the pressed key was detected at position (2,2) of the matrix keypad.
- After successful detection of the pressed key it’s our job to assign some corresponding value which may be either a numeral or an ASCiI character.
8051 FEATURES AND SPECIFICATIONS:
- 10 kilo ohm resistor and 10 microfarad will provide the necessary Power on reset (POR) for the 8051 mc.
- Secondly the 12 MHz crystal oscillator provides the necessary clock signal to the microcontroller.
- According to the connections I have given, the keypad connections were given to Port 1 of the 8051 microcontroller.
- Secondly the three pins namely Register select, REad/Write and Enable pins are given to the pins P3.2, P3.3 , P3.4 respectively.
- Finally last but not the least the data pins namely DB0-DB7 of the Liquid Crystal Display(LCD) are connected to the port P2 (P2.0-P2.7).
- The port 0 has no internal pull up resistors so it has to be pulled by external 10 kilo ohm resistor.
Click here to see a short video of Cursor Blinking.
Click here to see a short of Displaying Numeral 1 Through LCD: