This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Suggest changes to the code for matrix keypad

I wrote this code for displaying the key on the LCD that is pressed in the matrix keypad. The program is not working in the hardware platform.Kindly suggest changes to the code:

#include<reg51.h>
#include<stdio.h>
sbit RS=P3^0; //define the Register Select pin of LCD
sbit RW=P3^1; //define the Read(1)/write(1) pin of LCD
sbit EN=P3^2; //defines the Enable pin of LCD
char ch,R,C;

//function for delay
void delay(unsigned int itime)
{ int i,j; for(i=0;i<itime;i++) for(j=0;j<1275;j++);
}

//function for writing commands to LCD
void lcdcmd(unsigned int icmd)
{ P1=icmd; RS=0; RW=0; EN=1; delay(10); EN=0;
}

//function for writing data to LCD
void lcddata(unsigned int idat )
{ P1=idat; RS=1; RW=0; EN=1; delay(10); EN=0;
}

//function for entering string to lCD
void lcdstring(char *str)
{ int i=0; while(str[i]!='\0') { lcddata(str[i]); i++; }
} void keyscan()
{ P2=0xF0; while(P2==0xF0); //for row if (P2==0xE0) R=3; else if(P2==0xB0) R=1; else if(P2==0xD0) R=2; else if(P2==0x70) R=0;

P2=0x0F; while(P2==0x0F); //for column if (P2==0x0E) C=3; else if(P2==0x0B) C=1; else if(P2==0x0D) C=2; else if(P2==0x07) C=0;
}

//main program starts here
void main()
{ char key[4][4]={'1','2','3','4','5','6','7','8','9','0','A','B','+','-','=','/'}; while(1) {

lcdcmd(0x38); // Function Set: 8-bit, 2 Line, 5x7 Dots delay(50); lcdcmd(0x01); // Clear Display (also clear DDRAM content) delay(50); lcdcmd(0x06); // No shift and auto incremement entry mode delay(50); lcdcmd(0x80); // starting location of the first row of LCD delay(50); lcdstring("Keypad"); //display string on the first row delay(50); lcdcmd(0x01); // clear LCD delay(50); keyscan(); //calling the keyscan function to check which key is pressed ch=key[C][R]; lcddata(ch); //Passing the key pressed to be displayed to the LCD delay(50); }

}

Parents Reply Children
No data