Interfacing Matrix Keypad with PIC Microcontroller – MikroC
Figure 1: A 3×4 Matrix Keypad
Keypads are small keyboards that are used to enter numeric, alphanumeric or select configuration data to microcontroller systems. Keypads are available in a variety of sizes. The common sizes are 3×3, 4×3 and 4×4 keypads.
Keypads are widely used input devices with lots of application in our everyday life, devices like telephone, ATM, electronic lock, Calculator, timers just to name a few, all have some sort of a keypad.
A matrix keypad is basically a combination of push-buttons in a way to form rows and columns. In this way the number of input/output pins necessary for their connection to a microcontroller is reduced. A 4×3 keypad requires 7 input/output pins instead of 12 and a 4×4 will require 8 input/output pins instead of 16 pins. In the matrix keypad switches are connected in a special manner as shown in figure 2 below.
Figure 2: 4×4 Matrix Keypad structure
The values of each key could be mapped according to ones specific project application. Figure 1, figure 3 and figure 4 show a typical keys mapping for a 3×4 and 4×4 Matrix keypads.
Figure 3 Keys mapping with letters Figure 4 Calculator Keys mapping
Detecting a pressed Key
Figure 5: A 3×4 Keypad connected to PORTB
Assuming that the keypad is connected to PORTB as shown on figure 5, the steps to determine which key is pressed are as follows:
- A logic 1 is applied to the first column via RB0.
- Row pins RB4 – RB7 are read. If RB4 is 1, then it means key 1 is pressed, if RB5 is 1, key 4 is pressed and RB6 is 1, key 7 is pressed and if RB7 is 1, key * is pressed.
- A logic 1 is applied to the second column via RB1.
- The rows of pins RB4 – RB7 are read again. If RB4 is 1, key 2 is pressed, if RB5 is 1, key 5 and so on.
- The above process is repeated for all the three columns continuously.
MikroC Pro for PIC Library
The MikroC PRO for PIC provides a library for working with 4×4 keypad. The library routines can also be used with 4×1, 4×2, or 4×3 keypad. Below is a quick descriptions of the Keypad library, for more information, please visit online the mikroC pro for PIC Keypad Library page.
NB: This following variable must be defined in all projects using Keypad Library:
1 |
extern sfr char keypadPort; // It define the keypad Port. Example: char keypadPort at PORTD; |
Keypad_Init
This function initializes the port for working with keypad.
The global variable keypadPort must be defined first before using this function.
Example:
1 2 3 4 |
// Keypad module connections at PORTC char keypadPort at PORTC; // Initialize Keypad Keypad_Init(); |
Keypad_Key_Press
This function reads the key from keypad when key gets pressed. The keypad has to be initialized first before calling this function.
Example:
1 2 |
char kp; kp = Keypad_Key_Press(); |
Keypad_Key_Click
This function waits until some key is pressed and released (this is a blocking call). When released, the function returns 1 to 16 (note it’s 1 to 16 and not 0 to 15), depending on the key. If more than one key is pressed simultaneously the function will wait until all pressed keys are released. After that the function will return the code of the first pressed key. The keypad has to be initialized first before calling this function.
Example:
1 2 |
char kp; kp = Keypad_Key_Click(); |
Example
This is a simple example of using the Keypad Library. It can support keypads with 1..4 rows and 1..4 columns. The code being returned by Keypad_Key_Click() function is in range from 1..16. In this example, the code returned is transformed into ASCII codes [0..9,*#] and displayed on Lcd. Figure 5 shows the circuit diagram of the example, a 3×4 keypad is used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
/* * Project name: 3x4 Keypad_Test * Copyright: (c) www.studentcompanion.co.za, 2014. * Test configuration: MCU: PIC18F45K22 http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf Oscillator: HS-PLL, 32.00000 MHz */ unsigned short kp = 0; char txt[6]; // Keypad module connections char keypadPort at PORTB; // End Keypad module connections // LCD module connections sbit LCD_RS at LATD4_bit; sbit LCD_EN at LATD5_bit; sbit LCD_D4 at LATD0_bit; sbit LCD_D5 at LATD1_bit; sbit LCD_D6 at LATD2_bit; sbit LCD_D7 at LATD3_bit; sbit LCD_RS_Direction at TRISD4_bit; sbit LCD_EN_Direction at TRISD5_bit; sbit LCD_D4_Direction at TRISD0_bit; sbit LCD_D5_Direction at TRISD1_bit; sbit LCD_D6_Direction at TRISD2_bit; sbit LCD_D7_Direction at TRISD3_bit; // End LCD module connections void main() { Keypad_Init(); // Initialize Keypad ANSELD = 0; // Configure AN pins as digital I/O ANSELB = 0; Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1, 1, "3x4 Keypad"); Lcd_Out(2, 1, "Press any key..."); delay_ms(2000); //2s delay Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out(1, 1, "Key :"); // Write message text on LCD do { kp = 0; // Reset key code variable // Wait for key to be pressed and released do // kp = Keypad_Key_Press(); // Store key code in kp variable kp = Keypad_Key_Click(); // Store key code in kp variable while (!kp); // Prepare value for output, transform key to it's ASCII value switch (kp) { case 1: kp = 49; break; // 1 case 2: kp = 50; break; // 2 case 3: kp = 51; break; // 3 // case 4: kp = 65; break; // A case 5: kp = 52; break; // 4 case 6: kp = 53; break; // 5 case 7: kp = 54; break; // 6 // case 8: kp = 66; break; // B case 9: kp = 55; break; // 7 case 10: kp = 56; break; // 8 case 11: kp = 57; break; // 9 // case 12: kp = 67; break; // C case 13: kp = 42; break; // * case 14: kp = 48; break; // 0 case 15: kp = 35; break; // # // case 16: kp = 68; break; // D } Lcd_Chr(1, 10, kp); // Print key ASCII value on LCD } while (1); } |
You can download the full project files (MikroC source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).
MikroC Source code: Interfacing Keypad with MikroC
Proteus Schematic: Keypad Proteus Schematic design