(No difference)

Revision as of 10:07, 19 August 2016


The keypad matrix or hex keypad comprising of 16 keys arranged as 4x4 matrix is one of the common interfaces done to a micrcontroller.

These are similar to a telephone keypad that we normally come across. In this tutorial we will see about interfacing this keypad to 8051. We will write the code to scan the keys and display the pressed key on the LCD.

Video Tutorial

Keypad Scan Algorithm-Part 1

Keypad, Code and Demo- Part 2

Code and Demo

Basics

Working


For us human beings it takes a few seconds to press a key, but the controller can read the port pin and eventually the keypressed in milliseconds. Hence it is possible to scan a four or more rows and columns while the key is being pressed! In fact, when we will test the code we will realize that a single key press is detected multiple times.

  • KeyBounce: Multiple keypress is also resulted out of mechanical construction of the keys. When we press and release a key, it not just presses once but debounces several times.

There are various ways to eliminate debounce, however we will use time delay to neglect the effect of keybounce.

Schematic:

Figure below shows the schematic of a keypad interfaced with 8051 micrcontroller. We have tested it with P89V51RD2, you may wish to try in on any other 8051 family MCUs.
{{#widget:Iframe |url=SCH_Image.PNG |width=700 |height=400 |border=5 }} Download Schematic pdf

Code


As with all our other projects, we divide the files according to interfaces, hence we have lcd_4_bit.c as file for displaying messages on LCD, keypad.c has functions to scan and return a key pressed. The main code demonstrates how to read a key and display on the LCD.

/* Reg51.h contains the defnition of all ports and SFRs */
#include <reg51.h> 
 
#include "lcd.h"	//User defined LCD library which conatins the lcd routines
#include "keypad.h" //User defined library which conatins the keypad routines
 
 
/* start the main program */
void main() 
{
	unsigned char key;
  /* Initilize the lcd before displaying any thing on the lcd */
    LCD_Init();
 
  /* Initilize the keypad port pins */
   KEYPAD_Init();
 
  LCD_DisplayString("Key Press: ");
 
  /*Go to second line and display the key pressed*/
  LCD_GoToLineTwo();
 
  while(1)
   {
      /* Read the key from Matrix Keypad */ 
	   key=KEYPAD_GetKey();
 
	   /* Display the Key Pressed */   	  
	   LCD_DataWrite(key);
	 }
  }

Downloads

Below are the links to download the old and new codes along with schematic diagrams.
New Code
Old Code and Schematics

Feedback

Your comments motivate us to do better, your doubts and questions help us learn! Feel free to comment below