You are currently viewing How to Interface Keypad with dsPIC30F4011 – dsPIC Development Board

How to Interface Keypad with dsPIC30F4011 – dsPIC Development Board

Spread the love

The DsPIC30F4011 Development Board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. So,The kit supports in system programming (ISP) which is done through USB port.

Microchip’s dsPIC30F (dsPIC30F4011), Development Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 16-bit Microcontrollers.


A keypad is a set of buttons arranged in a block or “pad” which usually bear digits, symbols and usually a complete set of alphabetical letters. If it mostly contains numbers then it can also be called a numeric keypad. Here we are using 4 X 4 matrix keypad.

Interfacing keypad

Fig. 1 shows how to interface the 4 X 4 matrix keypad to two ports in microcontroller. The rows are connected to an output port and the columns are connected to an input port.

To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, and then it reads the columns. If the data read from the columns is D3-D0=1111, no key has been pressed and the process continues until a key press is detected. However, if one of the column bits has a zero, this means that a key press has occurred. For example, if D3-D0=1101, this means that a key in the D1 column has been pressed.

After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row D0 only; then it reads the columns.

If the data read is all 1s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to.

interfacing keypad with dspic
Fig. 1 Interfacing keypad to Microcontroller

Interfacing keypad with dsPIC30F4011

We now want to scan a keypad in DsPIC30F4011 Development Board. In case of 4X4 matrix Keypad both the ends of switches are connected to the port pin i.e. four rows and four columns. So in all sixteen switches have been interfaced using just eight lines.

1Keypads arranged by matrix format, each row and column section pulled by high or low by selection JP15, all row lines(PORTB.0 – PORTB.3) and column lines(PORTB.4 to PORTB.7) connected directly by the port pins.

Pin Assignment with dsPIC30F4011

pin assignment for interface keypad with dspic

Circuit Diagram to Interface keypad with dsPIC

circuit diagram for Interface Keypad with dsPIC

Source Code

The Interfacing keypad with dsPIC30F4011 program is very simple and straight forward, so that scan a keypad rows and columns. When the rows and columns are detected then it will display in PC through UART1. The C program is developed in Mplab software & compiled with Microchip C30 C Compiler.

C Program to 4 X 4 matrix keypad using dsPIC

Title : Program to keypad interfacing

#include "p30f4011.h"
#include "delay.c"


#define XTFREQ 10000000
#define FOSC XTFREQ/4
#define BAUDRATE 9600
#define BRGVAL ((FOSC/BAUDRATE)/16)-1 void SerialInit(void);

void ScanCol(void); void ScanRow(void);
unsigned int KeyArray[4][4]= {'1','2','3','4','5','6','7', '8','9','A','B','C','D','E','F','0'};
unsigned int Count[4][4]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; unsigned int Col=0,Row=0,count=0,i,j;
unsigned int temp,k;

 int main()
TRISB = 0x0000;
ADPCFG = 0xffff;
printf("Press any key:\n\r");

LATB = 0x000f;k=0;k++;
TRISB = 0x000f;

while((PORTB & 0x000f)==0x000f);
LATB = 0x00f0;
TRISB = 0x00f0;

while((PORTB & 0x00f0)==0x00f0);
printf("[%c]:Key Entry:%d\n\r", KeyArray[Row][Col],Count[Row][Col]);

void ScanRow()


temp=PORTB & 0x000F; switch(temp)

case 0x0007: Row=3;
case 0x000b: Row=2;
case 0x000d: Row=1;
case 0x000e: Row=0;

void ScanCol()
temp=PORTB & 0x00F0;
case 0x0070: Col=3;
case 0x00b0: Col=2;
case 0x00d0: Col=1;
case 0x00e0: Col=0;


void SerialInit()
U1MODE = 0x8000;
U1STA = 0x8400;

void putch(unsigned int SendDat)

To compile the above C code you need the Mplab software & Microchip C30 C Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without DsPIC30F4011 Development Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC dsPIC30F4011 through USB.

Testing the Keypad with dsPIC30F4011

Give +9V power supply to DsPIC30F4011 Development Board; the serial cable is connected between the DsPIC30F4011 Development Board and PC. Open the Hyper Terminal screen, select which port you are using and set the default settings. Now the screen should show some text messages & it display which key is pressed in keypad.

If you are not reading any text from UART1, then you just check the jumper connections & just check the serial cable is working. And also check the keypad keys are properly working or not. Otherwise you just check the code with debugging mode in Mplab.

If you want to see more details about debugging just see the videos in below link.

  • How to create & Debug a Project in Mplab using dsPIC30F.

General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.