keypads are seen in telephones and other commonly seen applications. Here in this chapter, we discuss in detail about how a matrix keypad is interfaced to a PIC microcontroller. Matrix Keypad interfacing
• The advantage of a matrix keypad is that the use
of it will allow the programmer to reduce the number of pins to be used. • In a 4×4 matrix keypad, there are four rows and four columns connected to 16 push button switches. • It may look like one needs 16 pins for the microcontroller to be connected to the matrix keypad, but practically 16 inputs of keypad interface are possible with the 8 pins of a microcontroller port. • First, we input HIGH on row pins. When a key is pressed, the corresponding row and column get shorted. • In the second step, a software scans the pins connected to the columns. If it detects a HIGH on any particular column, then it is found that the key press has been made on a key in that column. • The third step is to figure out which key is pressed exactly. For this, the software writes logic high on row pins sequentially. The pin of the column on which the pressed key is situated will become high. Principle of working
4×4 keypads can be connected in three modes.
• Normal mode • Pull up mode • Pull down mode In normal mode, rows are connected to a microcontroller without any pull-up or pull-down resistors. Pull-up and pull-down modes will have a default state on input pins depending on the modes. Normal mode Procedure of working
• Connect four column pins and four-row pins
to the microcontroller port. • In this example, we use port D of PIC microcontroller. • Define column pins as input and row pins as output • Here, we use the first four bits of port D as column pins and last four bits as row pins. • Wait for a key press (initial scanning) • Make all the row pins high, then keep scanning the rows till a key press get detected. • Detect and identify the pressed key • Check all the possible combinations. For example, if we give a high on row 3 and reads the status of column 1 as high, that means, S9 has got pressed. • PORTD = 0x04; // PORDbits -> 00000100, D2 is high • PORTD = 0x01; // PORTDbits -> 0000001, D0 is high • if (PORTDbits.RD4)&&(PORTDbits.RD2) • if (PORTDbits.RD4)&&(PORTDbits.RD0) • { // S3 is the pressed key • { // S1 is the pressed key • key = "S3"; • key = "S1"; • } • } • elsif (PORTDbits.RD5)&&(PORTDbits.RD2) • elsif (PORTDbits.RD5)&&(PORTDbits.RD0) • { // S7 is the pressed key • { // S5 is the pressed key • key = "S7"; • key = "S5"; • } • } • elsif (PORTDbits.RD6)&&(PORTDbits.RD2) • elsif (PORTDbits.RD6)&&(PORTDbits.RD0) • { // S11 is the pressed key • { // S9 is the pressed key • key = "S11"; • key = "S9"; • } • } • elsif (PORTDbits.RD7)&&(PORTDbits.RD2) • elsif (PORTDbits.RD7)&&(PORTDbits.RD0) • { // S15 is the pressed key • { // S13 is the pressed key • key = "S15"; • key = "S13"; • } • } • • • PORTD = 0x08; // PORDbits -> 00001000, D3 is high • PORTD = 0x02; // PORDbits -> 00000010, D1 is high • if (PORTDbits.RD4)&&(PORTDbits.RD3) • if (PORTDbits.RD4)&&(PORTDbits.RD1) • { // S4 is the pressed key • { // S3 is the pressed key • key = "S4"; • key = "S2"; • } • } • elsif (PORTDbits.RD5)&&(PORTDbits.RD3) • elsif (PORTDbits.RD5)&&(PORTDbits.RD1) • { // S8 is the pressed key • { // S6 is the pressed key • key = "S8"; • key = "S6"; • } • } • elsif (PORTDbits.RD6)&&(PORTDbits.RD3) • elsif (PORTDbits.RD6)&&(PORTDbits.RD1) • { // S12 is the pressed key • { // S10 is the pressed key • key = "S12"; • key = "S10"; • } • } • elsif (PORTDbits.RD7)&&(PORTDbits.RD3) • elsif (PORTDbits.RD7)&&(PORTDbits.RD1) • { // S16 is the pressed key • { // S14 is the pressed key • key = "S16"; • } • key = "S14"; • } • Debouncing
• These are spurious spikes generated during key press
events. Normally several spikes get generated during a key press because of the mechanical contacts in the switch and it causes multiple spikes, which may be detected as multiple key press events instead of a single event • We can filter these spikes with the help of either a hardware circuit or a software routine. • Hardware methods need to use low pass filters in each line which increases the number of components in the circuit. Here we use the easiest way, the software. • We are going to use time window for reading the port status. Introduce a delay between detection and scanning. if (PORTDbits.RD4)&&(PORTDbits.RD0) { // S1 is the pressed key delay_ms(100); // 100ms delay for avoiding spike detection if (PORTDbits.RD4)&&(PORTDbits.RD0) // re-check the key press { key = "S1"; // It is S1 } else{} // It is a bounce, **else** part is for understanding, not needed in real case } Pull-up mode Pull-down mode