A keypad is an organized matrix of switches in rows and columns and it comes in the various form like numeric keypad, alphanumeric keypad. It also comes in different size like 4×3, 4×4 etc.
Linux sshd config. In real life, the keypad has a lot of application like, calculator, electronic lock, mobile phone and many more electronic appliances.In this article, we will learn the interfacing of the keypad with 8051 micro-controller.
Note: Numeric keypad contains mostly numeric numbers.
Connection of keypad with 8051 microcontroller
In below image, I have described the connection of keypad with 8051 microcontrollers. Here I have connected the column and rows of keypad corresponding to lower nibble of port-2 and the upper nibble of port-2.
Similarly to get started with 8051 Microcontroller, LED interfacing is a basic thing in Microcontroller interfacing programming. Each Microcontroller is different in its architecture, but the interfacing concept almost all same for all Microcontroller.
- Interfacing Keypad with 8051 Microcontroller using Keil C 10KΩ resistor and 10μF will provide the required Power On Reset (POR) signal to the 8051 microcontroller. 12MHz crystal is used to provide required clock for the microcontroller and 22pF capacitors will stabilize the oscillations of the crystal.
- Interface lcd and keypad with 8051 microcontroller in C. This code is written in such a way that, when we press any key on the keypad then that value is displayed on the LCD. Interface lcd and keypad using AT89C51 A crystal of 11.0592 MHz is used here. You can use any crystal value from 3 to 24MHz with 8051.
Algorithm to write a sample program for Keypad Interfacing
Here we will create an interface between 8051 microcontrollers and a 4×3 numeric keypad. Here I am not considering the switch debouncing.
There are following steps to write a program for keypad INTERFACING
- Firstly you have to connect pin of rows and columns of the keypad with microcontroller pins and make the row pins as output and column pins as input.
NOTE: Whenever in keypad any of the keys pressed then corresponding row and column get connected and status of the row will be reflected in the corresponding column.
- Make all row pins high except the row1 pin and check the status of col1,col2, and col3. If anyone of them become low i.e. any one of the keys pressed in row1. If there is no column low in row1 i.e. no key pressed on the keypad.
e.g.
Suppose in above scenario col1 become low when row1 is the low i.e. first key of col1 would be pressed on the keypad.
- Just repeat the above scenario for row2, row3, and row4 and check the status of col1,col2, and col3.In below image, I have made the row2 pins low and check the status of col1,col2, and col3 pins.
In below image, I have made the row3 pins low and check the status of col1,col2, and col3 pins.
In below image, I have made the row4 pins low and check the status of col1,col2, and col3 pins.
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.
Sample program to describe the Interfacing of a keypad with 8051 microcontroller
I have written a sample program to reads the user input and display it on the 16×2 LCD. You can find here more about the interfacing of LCD and 8051 micro-controller. Kitserver pes 2011 download.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 | -------- VDD - +5V RW - P1.1 LCD_D1 - P3.1 LCD_D3 - P3.3 LCD_D4 - P3.4 LCD_D6 - P3.6 LED- - GND KEYPAD COL_1 - P2.0 COL_3 - P2.2 ROW_1 - P2.4 ROW_3 - P2.6 #include<string.h> #define COL1 0 #define COL3 2 #define MAX_ROW 3 //LCD sbit rw=P1^1;/*Configure R/W pin*/ #define KEYPAD P2 sbit COL_1=P2^0; sbit COL_3=P2^2; sbit ROW_1=P2^4; sbit ROW_3=P2^6; /*Function to write command on Lcd*/ voidLcdData(constcharcData); voidDisplayMessage(constchar*pszMessage); /*Function To Initialize Lcd*/ voidDelay(unsignedint); /*Reset The Row*/ charCheck_Column(void); /*Read Col Status*/ charucData=0; LCD=0x00;//Make Lcd O/p LcdData(ucData); LcdCommand(0x80);/*Address of DDRAM*/ LcdCommand(0xc0);/*Address of DDRAM*/ } return0; void rw=0; LCD=cCommand; e=0; } /*Function to write command on Lcd*/ LcdData(constcharcData){ rs=1; e=1; Delay(1); void pszMessage++; } /*Function to Provide Delay*/ Delay(unsignedinti){ for(j=0;j<i;j++) } /*Initialise the LCD_DATA*/ LcdInit(void){ LcdCommand(0x01); LcdCommand(0x38); LcdCommand(0x06); LcdCommand(0x0c); } charReadColStatus(void){ charucData='p'; while('p'ucData){ ucData=Check_Column(); } } charCheck_Column(void){ shortsiColNumber=0; {'1','2','3'}, {'7','8','9'}, }; for(siColNumber=0;siColNumber<=MAX_ROW;siColNumber++){ DoResetRow(siColNumber);/*Reset the row*/ if(COL_10){ returnucaKeyPad[siColNumber][COL1]; if(COL_20){ returnucaKeyPad[siColNumber][COL2]; if(COL_30){ returnucaKeyPad[siColNumber][COL3]; } } void case0: break; ROW_2=0; case2: break; ROW_4=0; } |
Embedded
In this post we will discuss logic and interface of a matrix keypad (4x4 for this post) with microcontroller to reduce the number of port pins required to read a certain number of inputs (digital). The same logic applies to any matrix keypad of order NxN. Where, N is the order of the matrix.
Why Matrix Keypad?
Typically one port pin is required to read a digital input into the controller. When there are a lot of digital inputs that have to be read, it is not feasible to allocate one pin for each of them. This is when a matrix keypad arrangement is used to reduce the pin count.
Therefore, the number of pins that are required to interface a given number of inputs decreases with increase in the order of the matrix.
Example: If the matrix is 2x2, you will need 2 pins for the rows and 2 pins for the columns. In such a case there is no difference in the cost of reading that many inputs. But if you consider a 10x10 matrix you will just need 20 pins (10 for the rows and 10 for the columns) to read 100 digital inputs.
How is it wired up internally?
Here is how the matrix keypad is wired internally.
From the circuit you can see that when one of the 16 buttons are pressed, a pair of pins are connected together. We will use this feature to detect the button that was pressed in the following sections.
Keypad Interfacing With 8051 Pdf
Matrix Keypad Interface Logic
Initially all switches are assumed to be released. So there is no connection between the rows and columns. When any one of the switches are pressed, the corresponding row and column are connected (short circuited). This will drive that column pin (initially high) low. Using this logic, the button press can be detected. The colors red and black is for logic high and low respectively. Here are the steps involved in determining the key that was pressed.
Step 1:
The first step involved in interfacing the matrix keypad is to write all logic 0’s to the rows and all logic 1’s to the columns. In the image, black line symbolizes logic 0 and red line symbolizes logic 1.
For now let us assume that, the circled key is pressed and see how the key press can be detected by a software routine.
Step 2:
Now the software has to scan the pins connected to columns of the keypad. If it detects a logic 0 in any one of the columns, then a key press was made in that column. This is because the event of the switch press shorts the C2 line with R2. Hence C2 is driven low.
Note: color of the lines indicate the logic values they return.
Step 3:
Once the column corresponding to the key pressed is located, the next thing that the software has to do is to start writing logic 1’s to the rows sequentially (one after the other) and check if C2 becomes high. The logic is that if a button in that row was pressed, then the value written to that row will be reflected in the corresponding column (C2) as they are short circuited. Note: color of the lines indicate the logic values they return.
Step 4:
The procedure is followed till C2 goes high when logic high is written to a row. In this case, a logic high to the second row will be reflected in the second column.
Note: color of the lines indicate the logic values they return.
Keypad Interfacing With 8051 Microcontroller
We already know that the key press happened at column 2. Now we have detected that the key is in row 2. So, the position of the key in the matrix is (2,2)
Once this is detected, its up to us to name it or provide it with a task on the event of the key press.
Keypad Interfacing With 8051 Pdf Software
Implementation in C
Now lets see how the above logic can be implemented in embedded C. Here is the program I wrote to test it. This code is for PIC microcontrollers with c18 lite version compiler. I as usual, used a lot of macros so if you are an Arduino user you could easily make some alterations to the code and use it. The basic concept for keypad scan is inside the while(1) loop.
Here is a video demonstration for the interface of the 4x4 matrix keypad using the above code.
The above program is done with polling and utilizes the entire time of the controller to scan the keypad and display the data on the 7 segment displays. There is a cool feature on Microcontrollers called as the Interrupt on Change (IOC). As the name suggests, the controller will interrupt if it finds any change in a port. In PIC the whole of PORT B has this feature. By using the feature without any change in the hardware setup we can scan the keypad in the ISR and have more of the controller’s time to do something useful.
Lcd And Keyboard Interfacing With 8051 Pdf
In my upcoming posts I will use the IOC feature to interface the keypad.