Interfacing 4x4 Matrix Keypad with PIC16F877A using MM74C922 Encoder

PabasaraRathnayake
5 min readApr 29, 2021
Photo by cottonbro from Pexels

As an IT undergraduate, this is one of my contributions towards the 1st year Hardware Project. The task is to Interface a 4x4 Matrix keypad to PIC16F877A Microcontroller. Today I will share with you what I did.

Background needed

First, you need Proteus 8 professional installed in your computer in order to simulate the key pad.

You need to install the compiler MPLAB XC8.

Download and Install here.

Keypad

I’m using 4x4 matrix keypad, you can use any other keypad but, you should define the PORTs according to the switches.

In this keypad there are 8 output pins. If we are not using the encoder, we have to allocate 8 pins from the PIC for keypad.

Internal Structure of 4x4 Keypad Module

Internal structure of 4x4 matrix keypad

After understanding the internal process of the keypad, we can interface with the PIC microcontroller.

As mentioned in the keypad picture, there are 8 output pins. But when you are implemeting the whole machine, the pins of the PIC mocrocontroller might not be enough. In other words, you have to minimise the number of the pins in PIC used for keypad. The solution to this problem is to use an encoder.

MM74C922 Encoder

The MM74C922 chip is available in 18-pin DIP and 20-pin SOIC packages. And this project uses the DIP version which has 18 pins, but in proteus we find the encoder with 16pins as configured in the image. When you are designing the PCB diagram, you will find the 18 pin encoder. (To create a PCB view EasyEDA)

Function Table of Keypad and Encoder

A,B,C,D and E are the output pins of the encoder that are to be connected to the PIC. (RB1, RB2, RB3, RB5, RB6 respectively). The PORTs of the PIC can be differ according to your PORT definition of the code.

//for keypad_encoder
#define A RB1
#define B RB2
#define C RB3
#define D RB5
#define E RB6

4x4 Switch Encoded Keypad Diagram

Proteus Simulation

Code

Other than the main.c file, a lcd.h file is needed as we are using a LCD display. We cannot edit the lcd.h file and you can find it from the google. What you should edit is the main.c file.

main.c

Simulate with Proteus 8 Professional

Code Explanation

These are the header files needed. Most of them are auto generated and we need to change the crystal frequency as we want (20MHz) #define _XTAL_FREQ 20000000

Defining PORTs

Ports of the PIC with Encoder and LCD display are defined here.

main function

This is the main function. The main program loop is an infinite while loop. You can pass required parameter for __delay_ms() function. There are few functions called in the main function. They are defined as follows.

Basically, there are 4 functions other than the main function

system_init();       // called before all the functions
initKeypad(); // called inside system_init()
switchPressScan(); // called inside main()
Lcd_Start(); // called inside system_init()
Function definition and calling

If you are interested, you can create the PCB view of your working keypad using any online tool. This is the PCB view I created using easyEDA.

PCB views 2D &3D

Problems and Solutions

These are a few of the problems and solutions I found.

  1. There can be many problems when you are simulating. Most of them can be solved by starting the project, as visible in the video guide.
  2. MPLAB XC8 compiler must be installed on your computer before starting the project. If it’s not installed, PMLAB XC8 (not configured) is displayed when selecting the compiler.
  3. Suppose you are adding files other than the main.c file, they should be saved in the exact location where the Proteus file is saved. After adding a new file, you need to close and restart the project.
  4. There can be problems when opening a project created using a different version of Proteus. This can be solved using the latest version or the same version of Proteus.
  5. The program file might be missing and give simulation errors. (You can check whether the program file is available by double-clicking on PIC) To solve this, you can simulate the project just after creating the project. (shown in the video).
  6. If you cannot add components, try closing the project and start with administrator privileges. (run as administrator).

--

--