An alphabetic keypad is similar to a numeric keypad, but it provides the complete set of alphabetical letters instead of numbers 0-9. This article presents a way to interface a keypad with 8051 microcontroller (AT89C51) to display alphabetical characters on an LCD module. Such kind of systems is commonly used in mobile phones to write an SMS and other texts. It can have further applications in displaying instant messages on other display systems such as LED matrices or other multi-segment displays.
- A 4x3 matrix keypad and a 16x2 LCD has been used here. Keypad and LCD are very commonly used input & output devices, respectively.The connections in the circuit are as following: port P2 of microcontroller AT89C51 is used as data input port which is connected to data pins (7-14) of LCD. P3^0, P3^1 and P3^2 pins of microcontroller are connected to control pins RS, RW and EN of LCD. Port P0 is used to take input from keypad. Also refer to Keypad interfacing with AT89C51.The alphabetic pattern or letter mapping used in this system is depicted in the following figure:
Every key is assigned with a variable to count the number of times it has been pressed. The count is set to zero initially and increases by one when the same key is pressed again.
On first count, the first alphabet of the key from its letter map is displayed on LCD. For example, if Key1 is pressed when its count is 0, then ‘a’ appears on the LCD screen. After this operation, the count increases to 1.The second letter of the alphabetic pattern is shown on the display module when the same key is pressed again. This time the count increases again to 2 and second alphabet is displayed at same position. Thus, we get ‘b’ on display if Key1 is used twice.When the same key is pressed third time, its count value changes to 3. This time the third alphabet of the letter pattern gets displayed on screen replacing the previous alphabet. For the considered case, ‘c’ appears on the LCD screen when Key1 is pressed thrice in a row.The count is reset to zero after it reaches to a value of 3. After this execution, the position on LCD is shifted to right to allow the second letter to be printed there.Therefore, pressing the same key four times consecutively, the first alphabet from its letter map reappears in succession to the last letter. Here, ‘a’ gets displayed on LCD after ‘c’ when Key1 is used four times.In other words, if a key is pressed more than thrice in a row, the count resets to zero and it’ll behave as a new alphabetic key. Thus the same modes of operations continue on repeated access of the keypad buttons.Key0 of this keypad is set for to provide a blank space ‘ ’ while Key* provides the functionality of backward movement while writing a text on LCD. Key# of the keypad is configured for forward movement.The 16x2 LCD can display only 16 characters in a line. Therefore, after 16th letter appears on screen, the cursor position is moved and 17th letter gets displayed on the first position of 2nd line of LCD. Similarly, 23rd letter appears back on the first line and so on.
CODE:
// Program to make alphabetical keypad similar to the one in mobiles
#include<reg51.h>
#define port P1
#define dataport P2 //Data port for LCD
#define key P0 // Port for Keypad
#define sec 100
sbit rs = port^1;
sbit rw = port^2;
sbit en = port^3;
sbit col1=key^4;
sbit col2=key^5;
sbit col3=key^6;
sbit row1=key^0;
sbit row2=key^1;
sbit row3=key^2;
sbit row4=key^3;
unsigned char position=128,i=0;
int check_again[10]={0,0,0,0,0,0,0,0,0,0};
void delay(unsigned int msec) // Time delay function
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char item)
{
dataport = item;
rs= 0;
rw=0;
en=1;
delay(1);
en=0;
return;
}
void lcd_data(unsigned char item)
{
lcd_cmd(0x0f);
delay(10);
dataport = item;
rs= 1;
rw=0;
en=1;
delay(1);
en=0;
return;
}
void num_check(int a , int b) //Check function
{
int j;
for(j=0;j<10;j++)
{
if(j!=a)
{
check_again[j]=0;
}
}
if(check_again[a]==3)
check_again[a]=0;
check_again[11]=1;
check_again[12]=1;
}
void display(int a ,int b) // Display function
{
switch(b)
{
case 1:
{
switch(a)
{
case 1:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('a');
delay(5);
num_check(1 ,check_again[1]);
break;
}
case 2:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('d');
delay(5);
num_check(2 ,check_again[2]);
break;
}
case 3: {
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('g');
delay(5);
num_check(3 ,check_again[3]);
break;
}
case 4:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('j');
delay(5);
num_check(4 ,check_again[4]);
break;
}
case 5:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('m');
delay(5);
num_check(5 ,check_again[5]);
break;
}
case 6:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('p');
delay(5);
num_check(6 ,check_again[6]);
break;
}
case 7:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('s');
delay(5);
num_check(7 ,check_again[7]);
break;
}
case 8:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('v');
delay(5);
num_check(8 ,check_again[8]);
break;
}
case 9:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('y');
delay(5);
num_check(9 ,check_again[9]);
break;
}
case 0:{
lcd_cmd(position+i);
lcd_data(' ');
delay(5);
num_check(10 ,check_again[10]);
break;
}
case 11:{
if((position+i)>129)
{
i=i-2;
delay(5);
lcd_cmd(position+i+1);
num_check(11 ,check_again[11]);;
delay(5);
}
break;
}
case 12:{
delay(5);
lcd_cmd(position+i);
num_check(12 ,check_again[12]);
delay(5);
break;
}
}
break;
}
case 2:
{
switch(a)
{
case 1:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('b');
delay(5);
num_check(1 ,check_again[1]);
break;
}
case 2:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('e');
delay(5);
num_check(2 ,check_again[2]);
break;
}
case 3: {
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('h');
delay(5);
num_check(3 ,check_again[3]);
break;
}
case 4:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('k');
delay(5);
num_check(4 ,check_again[4]);
break;
}
case 5:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('n');
delay(5);
num_check(5 ,check_again[5]);
break;
}
case 6:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('q');
delay(5);
num_check(6 ,check_again[6]);
break;
}
case 7:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('t');
delay(5);
num_check(7 ,check_again[7]);
break;
}
case 8:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('w');
delay(5);
num_check(8 ,check_again[8]);
break;
}
case 9:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('z');
delay(5);
num_check(9 ,check_again[9]);
break;
}
case 0:{
lcd_cmd(position+i);
lcd_data('-');
num_check(10 ,check_again[10]);
break;
}
case 11:{
i--;
lcd_cmd(0x10);
delay(5);
break;
}
}
break;
}
case 3:
{
switch(a)
{
case 1:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('c');
delay(5);
num_check(1 ,check_again[1]);
break;
}
case 2:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('f');
delay(5);
num_check(2 ,check_again[2]);
break;
}
case 3: {
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('i');
delay(5);
num_check(3 ,check_again[3]);
break;
}
case 4:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('l');
delay(5);
num_check(4 ,check_again[4]);
break;
}
case 5:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('o');
delay(5);
num_check(5 ,check_again[5]);
break;
}
case 6:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('r');
delay(5);
num_check(6 ,check_again[6]);
break;
}
case 7:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('u');
delay(5);
num_check(7 ,check_again[7]);
break;
}
case 8:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('x');
delay(5);
num_check(8 ,check_again[8]);
break;
}
case 9:{
if(check_again[a]>1)
i--;
lcd_cmd(position+i);
lcd_data('z');
delay(5);
num_check(9 ,check_again[9]);
break;
}
case 0:{ lcd_cmd(position+i);
lcd_data('-');
num_check(10 ,check_again[10]);
break;
}
case 11:{
i=i-2;
lcd_cmd(position+i);
delay(5);
break;
}
}
break;
}
}
}
void check_col1() //Check column one
{
row1=row2=row3=row4=1;
row1=0;
if(col1==0)
{
delay(10);
check_again[1]++;
display(1,check_again[1]);
}
row1=1;
row2=0;
if(col1==0)
{
delay(10);
check_again[4]++;
display(4,check_again[4]);
}
row2=1;
row3=0;
if(col1==0)
{
delay(10);
check_again[7]++;
display(7,check_again[7]);
}
row3=1;
row4=0;
if(col1==0)
{
delay(10);
display(11,1); //FOR *
}
row4=1;
}
void check_col2() //Check column two
{
row1=row2=row3=row4=1;
row1=0;
if(col2==0)
{
delay(10);
check_again[2]++;
display(2,check_again[2]);
}
row1=1;
row2=0;
if(col2==0)
{
check_again[5]++;
display(5,check_again[5]);
}
row2=1;
row3=0;
if(col2==0)
{
delay(10);
check_again[8]++;
display(8,check_again[8]);
}
row3=1;
row4=0;
if(col2==0)
{
delay(10);
check_again[0]++;
display(0,1);
}
row4=1;
}
void check_col3() //check column three
{
row1=row2=row3=row4=1;
row1=0;
if(col3==0)
{
delay(10);
check_again[3]++;
display(3,check_again[3]);
}
row1=1;
row2=0;
if(col3==0)
{
delay(10);
check_again[6]++;
display(6,check_again[6]);
}
row2=1;
row3=0;
if(col3==0)
{
delay(10);
check_again[9]++;
display(9,check_again[9]);
}
row3=1;
row4=0;
if(col3==0)
{
delay(10);
display(12,1); //FOR #
}
row4=1;
}
void keypad1()
{
if((position+i)>144 && i>16)
{
position=192;
i=0;
}
if((position+i)>203 && i>12 )
{
lcd_cmd(0X01);
i=0;
position=128;
}
i++;
lcd_cmd(position+i);
if(col1==0)
check_col1();
else
if(col2==0)
check_col2();
else
if(col3==0)
check_col3();
delay(10);
}
void main()
{
col1=col2=col3=1;
lcd_cmd(0x38);
lcd_cmd(0x0e);
lcd_cmd(0x01);
delay(sec);
while(1)
{
row1=row2=row3=row4=0;
while(col1==1 && col2==1 && col3==1);
{
keypad1();
}
}
}