praveen_palaparthi
Full Member level 1
hi friends i tried to interface 4x4 matrix keypad to pic 16f877 can any give me the source code in C to check which key is pressed
this is the code i have tried in hitech pic C complier
/*****************MAIN PROGRAM*****************/
main()
{
unsigned char i;
unsigned char key_pad[4][4]={ {'D','E','F',0},{9,'A','B','C'},{5,6,7,8},{1,2,3,4} };
OPTION=0x00; //enable pull up resistors by individual latch values
INTCON=0xC8; // enable Port B change interrupt
lcd(); //LCD intialization
keypad_matrix(); //4x4 matrix keypad intialization
while(1)
{
PORTB=0xF0; //All inputs are pulled to high
RA4=1;
PORTE= 0x07; // All outputs are pulled high
for(i=0;i<=3;i++)
{
switch(i)
{
case 0:
RA4=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][0]);
else if(RB5==0)
lcd_display(key_pad[1][0]);
else if(RB6==0)
lcd_display(key_pad[2][0]);
if(RB7==0)
lcd_display(key_pad[3][0]);
break;
case 1:
RE0=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][1]);
else if(RB5==0)
lcd_display(key_pad[1][1]);
else if(RB6==0)
lcd_display(key_pad[2][1]);
if(RB7==0)
lcd_display(key_pad[3][1]); //checking input is low
break;
case 2:
RE1=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][2]);
else if(RB5==0)
lcd_display(key_pad[1][2]);
else if(RB6==0)
lcd_display(key_pad[2][2]);
if(RB7==0)
lcd_display(key_pad[3][2]); //checking input is low
break;
case 3:
RE2=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][3]);
else if(RB5==0)
lcd_display(key_pad[1][3]);
else if(RB6==0)
lcd_display(key_pad[2][3]);
if(RB7==0)
lcd_display(key_pad[3][3]); //checking input is low
break;
}
}
}
}
void interrupt portb_change(void)
{
if(RBIF)
{
RBIF=0;
PORTB=0xF0; //All inputs are pulled to high
RA4=1;
PORTE= 0x07; // All outputs are pulled high
}
}
/************ROUTINES*********************************/
#include<pic.h>
#include "key.h"
#include "delay.h"
#define LCD_RS RB4
#define LCD_RW RB5
#define LCD_E RA2
#define LCD_DATA PORTB
#define LCD_STROBE ((LCD_E=1),(LCD_E=0))
__CONFIG(XT & WDTDIS & LVPDIS & UNPROTECT);
void lcd(void)
{
TRISB=0xC0;
PORTB=0;
lcd_init();
lcd_clear();
}
void lcd_init(void)
{
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
DelayMs(40);
LCD_DATA=0x03;
DelayMs(5);
LCD_DATA=0x03;
DelayUs(100);
LCD_DATA=0x03;
DelayUs(40);
LCD_DATA=0x02;
DelayUs(40);
lcd_write(0x20);
DelayUs(40);
lcd_write(0x08);
DelayUs(40);
lcd_write(0x0F);
lcd_write(0x06);
DelayUs(40);
}
void lcd_clear(void)
{
LCD_RS=0;
lcd_write(0x01);
LCD_STROBE;
}
void lcd_write(unsigned char c)
{
DelayUs(40);
LCD_DATA = ((c>>4) & 0x0F);
LCD_STROBE;
LCD_DATA = (c & 0x0F);
LCD_STROBE;
}
void keypad_matrix(void)
{
TRISB=0xF0; //RB7:RB4 are taken as input
TRISA=0xEF; //RA4 is taken as output
TRISE=0xF8; // RE2:RE0 is taken as output
}
void lcd_display(unsigned char c) // displays data on LCD
{
lcd_goto(0x07);
DelayMs(5);
LCD_DATA = 0;
LCD_RS = 1;
LCD_DATA |= ( ( c >> 4 ) & 0x0F );
LCD_STROBE; //high to low pulse must be applied in order for the LCD to latch in the data
LCD_DATA &= 0xF0;
LCD_DATA |= ( c & 0x0F );
LCD_STROBE;
LCD_RS=0;
}
void lcd_goto(unsigned char pos)
{
LCD_RS=0;
lcd_write(0x80+pos);
}
this is the code i have tried in hitech pic C complier
/*****************MAIN PROGRAM*****************/
main()
{
unsigned char i;
unsigned char key_pad[4][4]={ {'D','E','F',0},{9,'A','B','C'},{5,6,7,8},{1,2,3,4} };
OPTION=0x00; //enable pull up resistors by individual latch values
INTCON=0xC8; // enable Port B change interrupt
lcd(); //LCD intialization
keypad_matrix(); //4x4 matrix keypad intialization
while(1)
{
PORTB=0xF0; //All inputs are pulled to high
RA4=1;
PORTE= 0x07; // All outputs are pulled high
for(i=0;i<=3;i++)
{
switch(i)
{
case 0:
RA4=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][0]);
else if(RB5==0)
lcd_display(key_pad[1][0]);
else if(RB6==0)
lcd_display(key_pad[2][0]);
if(RB7==0)
lcd_display(key_pad[3][0]);
break;
case 1:
RE0=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][1]);
else if(RB5==0)
lcd_display(key_pad[1][1]);
else if(RB6==0)
lcd_display(key_pad[2][1]);
if(RB7==0)
lcd_display(key_pad[3][1]); //checking input is low
break;
case 2:
RE1=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][2]);
else if(RB5==0)
lcd_display(key_pad[1][2]);
else if(RB6==0)
lcd_display(key_pad[2][2]);
if(RB7==0)
lcd_display(key_pad[3][2]); //checking input is low
break;
case 3:
RE2=0;
if(RB4==0) //checking input is low
lcd_display(key_pad[0][3]);
else if(RB5==0)
lcd_display(key_pad[1][3]);
else if(RB6==0)
lcd_display(key_pad[2][3]);
if(RB7==0)
lcd_display(key_pad[3][3]); //checking input is low
break;
}
}
}
}
void interrupt portb_change(void)
{
if(RBIF)
{
RBIF=0;
PORTB=0xF0; //All inputs are pulled to high
RA4=1;
PORTE= 0x07; // All outputs are pulled high
}
}
/************ROUTINES*********************************/
#include<pic.h>
#include "key.h"
#include "delay.h"
#define LCD_RS RB4
#define LCD_RW RB5
#define LCD_E RA2
#define LCD_DATA PORTB
#define LCD_STROBE ((LCD_E=1),(LCD_E=0))
__CONFIG(XT & WDTDIS & LVPDIS & UNPROTECT);
void lcd(void)
{
TRISB=0xC0;
PORTB=0;
lcd_init();
lcd_clear();
}
void lcd_init(void)
{
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
DelayMs(40);
LCD_DATA=0x03;
DelayMs(5);
LCD_DATA=0x03;
DelayUs(100);
LCD_DATA=0x03;
DelayUs(40);
LCD_DATA=0x02;
DelayUs(40);
lcd_write(0x20);
DelayUs(40);
lcd_write(0x08);
DelayUs(40);
lcd_write(0x0F);
lcd_write(0x06);
DelayUs(40);
}
void lcd_clear(void)
{
LCD_RS=0;
lcd_write(0x01);
LCD_STROBE;
}
void lcd_write(unsigned char c)
{
DelayUs(40);
LCD_DATA = ((c>>4) & 0x0F);
LCD_STROBE;
LCD_DATA = (c & 0x0F);
LCD_STROBE;
}
void keypad_matrix(void)
{
TRISB=0xF0; //RB7:RB4 are taken as input
TRISA=0xEF; //RA4 is taken as output
TRISE=0xF8; // RE2:RE0 is taken as output
}
void lcd_display(unsigned char c) // displays data on LCD
{
lcd_goto(0x07);
DelayMs(5);
LCD_DATA = 0;
LCD_RS = 1;
LCD_DATA |= ( ( c >> 4 ) & 0x0F );
LCD_STROBE; //high to low pulse must be applied in order for the LCD to latch in the data
LCD_DATA &= 0xF0;
LCD_DATA |= ( c & 0x0F );
LCD_STROBE;
LCD_RS=0;
}
void lcd_goto(unsigned char pos)
{
LCD_RS=0;
lcd_write(0x80+pos);
}