Line 29: Line 29:
 
}}
 
}}
 
='''Code'''=
 
='''Code'''=
 +
{{Box|type=l_green_light|text=<br />
 
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.
 
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.  
 
The main code demonstrates how to read a key and display on the LCD.  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 67: Line 68:
 
<\syntaxhighlight>
 
<\syntaxhighlight>
 
=Downloads=
 
=Downloads=
{{{DISQUS}}}
+
 
 +
Please feel free ask questions, give feed back about this article below.
 +
{{DISQUS}}

Revision as of 19:30, 26 March 2014

Basics


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.

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=http://xplorelabz.com/wiki/images/c/cc/Keypad_8051.pdf

|width=700

|height=400

|border=5

}}

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);
	 }
  }
 
<\syntaxhighlight>
=Downloads=
 
Please feel free ask questions, give feed back about this article below.
{{DISQUS}}