### INTERFACING 5X5 MATRIX KEYPAD PROJECT

We have seen tutorial on  5x5 matrix keypad in our last post, in this post we will see a project by make use of matrix keypad and 16x2 LCD. The difference between previous LCD interfacing post and this post is that we dint used any library functions in our previous LCD post and in this we use library functions of MikroC for interfacing LCD. My main objective of this post is to make you clear how a matrix keypad can be interfaced.
WHAT YOU WILL LEARN FROM THIS PROJECT?

1. Interfacing matrix keypad having 25 keys.
2. Using Lcd library functions.

AIM: Aim of the project is to display characters/numbers on to the Lcd screen when corresponding key is pressed.

CIRCUIT DIAGRAM:

All other connections are same i.e pin 13, 14 =crystal, 12,31=gnd, 1,11,32=power +5v.
Note PORTC connected to DATA PINS of LCD
RS=portd.f0
R/W=portd.f1
E=portd.f2

Points to be noted from above fig:
• Rows are connected to PortA pins, each row is connected to  RA0, RA1,RA2,RA3 and RA5(note we dint used RA4 pin) respectively.
• Columns are connected to PortB pins from RB0 to RB4
• LCD is connected to PortC.
ALGORITHM:

• Configure the input and output ports, initialize the LCD
• Make portb=0b00000001 i.e portb.f0=1 and check all ROW pins i.e porta pins
• If porta.f0 is 1 then key 'A' is pressed, if porta.f1=1 then key 'B' is pressed, if porta.f2=1 => 'C' is pressed, porta.f3=1 => 'D' is pressed, porta.f4=1 => 'E' is pressed, if non of the porta pin is high then
• Left shift portb i.e portb=0b00000010 i.e portb.f1=1 and check all Rows i.e porta pins
• If porta.f0=1 => key 'F' is pressed, Porta.f1=1 => key 'G' , porta.f2=1 => key 'H' , porta.f3= key 'I' , porta.f4=1 then key 'J' , if non of the porta pin is 1 then
• Left shift portb i.e portb=0b00000100 and check porta pins
• Repeat these steps until all rows been checked.
• When we detected the key then send corresponding ascii value to PortC i.e to LCD.
Note:In tutorial we have scanned the columns , but in this project we are scaning rows. There is no much difference between them.

PROGRAM:
//COMPILER: MIKROC FOR PIC
//MICRO CONTROLLER: PIC18F452
//CRYSTAL FREQ: 4MHZ

// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections

//Function to detect pressed key
{
unsigned char i,key=0,flag=0;

PORTB=0x01;//First column=1
while(1)//infinite loop
{

{
flag=1;//set the flag if any key is pressed
if(PORTA.F0==1)break;//check first row
key=key+5;
if(PORTA.F1==1)break;//check 2nd row
key=key+5;
if(PORTA.F2==1)break;//check 3rd row
key=key+5;
if(PORTA.F3==1)break;//check 4th row
key=key+5;
if(PORTA.F5==1)break;//check 5th row
}
if(flag==1)break;//if key is detected break
key++;// increment the variable 'key'
PORTB<<=1;//left shift the portb
if(key==5)
{
key=0;PORTB=0X01;
}
}

//Below two lines are used to check whether user is still hold the pressed key
PORTB=0b00011111;//make all columns one
while((PORTA&mask)!=0);//check all rows, if not equal to 0 then enter into while loop and come out of the loop if rows is '0' i.e come out of loop if person released the pressed key.
delay_ms(20);
return(key);//return key number

}

//Main function
void main()
{
unsigned char mykey;
TRISC=0;TRISA=0b000101111;TRISB=0X0;
ADCON1=7;//Disable analog of porta to use porta for digital use
//configure LCD
Lcd_Init();//Library function for LCD initiate
Lcd_Cmd(_LCD_CLEAR);//function for LCD clear
Lcd_Out(1,1,"WELCOME");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
while(1)
{
Lcd_Chr_Cp(mykey+65);//add key number and Ascii value of 'A'
}
}

Explanation:

Everything has been explained in  code comments but few things need to be cleared, i'll do this in this section.

Look at the main function, when function call 'getkeypad()' is called then control will be shifted to the function 'unsigned char getkeypad() ' , PORTB=0x01 i.e first column is 1, then we checked if any key is pressed using 'if' statement i.e if((PORTA&mask)!=0)
'Mask' default value   00101111 (#define mask 0b00101111 ) if key is not pressed then porta will be 00000000 obviously then

porta:            00000000
--------------
AND:            00000000
---------------
If no key is pressed then (PORTA&mask) will be zero.
If any key is pressed then (PORTA&mask) will be non zero.
So, if((PORTA&mask)!=0) means enter into 'if loop' if any key is pressed.
Look statements after 'If loop', "PORTB<<=1;"  if any key is not pressed then shift portB left side i.e PORTB=0b00000010 , in next iteration PORTB=0b00000100,in next iteration PORTB=0b00001000, in next iteration PORTB=0b00010000. During every iteration variable 'key' will be incremented, after it reaches 5 then  in next iteration PORTB=0b00000001 due to this statement
if(key==5)
{
key=0;PORTB=0X01;
}

Analyze the above program every iteration carefully to understand clearly. I hope i have make you cleared but still there might be many doubts in your mind, it will be cleared only when you check program and understand it. If you still have any doubts then comment below.

AUTHOR: YOUSTRON SIC

Anonymous said...

Hey Youstron, you are doing the right thing man. Everything is cul on your website. the tutorials and everthing. though can you enable the copying capability on the website please. Cheers..

Anonymous said...

Hi,i am goldy i need your help. can you please tell me how to get digits more than 1 in lcd display using keypad.Actually i have made a projects in which only one digits is displayed in lcd and i want it to display atlest four or five digits.kindly reply for this solution.

Unknown said...

how do you get the value of the key which was pressed, please explain the expression key = key + 5;

csyan said...

I bought this watch for my dad on this website on Father's Day. replica omega watchesThis watch has been bought since my father has been wearing it.fake omega speedmaster watches It has been running and working very hard. The key is that it looks no different from genuine and the design is perfect. He likes it very much.