Connect Matrix Keypad with PIC Controller Code

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. For connections explanation see schematic at the bottom of this page.

External dependencies of Keypad Library

The following variable must be defined in all projects using Keypad Library: Description : Example :
extern sfr char keypadPort; Keypad Port. char keypadPort at PORTD;

Connect Matrix Keypad with PIC Controller CodeLibrary Routines

  • Keypad_Init
  • Keypad_Key_Press
  • Keypad_Key_Click


Prototype void Keypad_Init(void);
Returns Nothing.
Description Initializes port for working with keypad.
Requires Global variable :
  • keypadPort – Keypad port

must be defined before using this function.

// Keypad module connections
char keypadPort at PORTD;
// End of keypad module connections


Prototype char Keypad_Key_Press(void);
Returns The code of a pressed key (1..16).
If no key is pressed, returns 0.
Description Reads the key from keypad when key gets pressed.
Requires Port needs to be initialized for working with the Keypad library.
char kp;
kp = Keypad_Key_Press();


Prototype char Keypad_Key_Click(void);
Returns The code of a clicked key (1..16).
If no key is clicked, returns 0.
Description Call to Keypad_Key_Click is a blocking call: the function waits until some key is pressed and released. When released, the function returns 1 to 16, 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.
Requires Port needs to be initialized for working with the Keypad library.
char kp;
kp = Keypad_Key_Click();

Connect Matrix Keypad with PIC Controller Code schematichCode Example

This is a simple example of using the Keypad Library. It supports 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,A..F] and displayed on Lcd. In addition, a small single-byte counter displays in the second Lcd row number of key presses.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us: