Summary of Connect Matrix Keypad with PIC Controller Code
The mikroC PRO for PIC offers a library to interface with 4x4, 4x1, 4x2, or 4x3 keypads. It initializes ports and provides functions like Keypad_Init, Keypad_Key_Press, and Keypad_Key_Click to detect and process inputs. The example demonstrates converting key codes to ASCII and displaying them on an LCD while counting presses.
Parts used in the mikroC PRO for PIC Keypad Project:
- 4x4 Keypad (or 4x1, 4x2, 4x3)
- PIC Controller
- LCD Display
- Keypad Library
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; |
Library Routines
Keypad_Init
| Prototype | void Keypad_Init(void); |
|---|---|
| Returns | Nothing. |
| Description | Initializes port for working with keypad. |
| Requires | Global variable :
must be defined before using this function. |
| Example |
// Keypad module connections char keypadPort at PORTD; // End of keypad module connections ... Keypad_Init(); |
Keypad_Key_Press
| 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. |
| Example |
char kp; ... kp = Keypad_Key_Press(); |
Keypad_Key_Click
| 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. |
| Example |
char kp; ... kp = Keypad_Key_Click(); |
Code Example
- What is the purpose of the Keypad_Init function?
It initializes the port for working with the keypad. - How does Keypad_Key_Press behave when no key is pressed?
It returns 0 if no key is pressed. - Can this library be used with keypads other than 4x4?
Yes, it can also be used with 4x1, 4x2, or 4x3 keypads. - What happens if multiple keys are pressed simultaneously in Keypad_Key_Click?
The function waits until all pressed keys are released before returning the code of the first pressed key. - What range of values does Keypad_Key_Click return?
It returns a value from 1 to 16 depending on the key. - Which variable must be defined before using the Keypad library?
The global variable keypadPort must be defined. - Does Keypad_Key_Click wait for a specific action?
Yes, it is a blocking call that waits until a key is pressed and then released. - How are key codes transformed in the provided code example?
The returned codes are transformed into ASCII codes [0..9,A..F] and displayed on the LCD.

