/* Processor: AT89C51 */
/* Compiler: Keil for 8051*/
/* LCD 16*2 and keypad 4*4 */
#include <reg51.h>
#define Port P0 //keypad connected to port P0
#define C1 P1^0
#define C2 P0^1
#define C3 P0^2
#define C4 P0^3
char get_key()
{
unsigned char i, k, key=0;
k = 1;
for( i = 0; i < 4; i++) //loop for 4 rows
{
keyport &=~ (0x80 >> i); //to make rows low 1 by 1
if(!C1) //check if key1 is pressed
{
key = k+0; //set key number
while(!C1); //wait for release
return key; //return key number
}
if(!C2) //check if key2 is pressed
{
key = k+1; //set key number
while(!C2); //wait for release
return key; //return key number
}
if(!C3) //check if key3 is pressed
{
key = k+2; //set key number
while(!C3); //wait for release
return key; //return key number
}
if(!C4) //check if key4 is pressed
{
key = k+3; //set key number
while(!C4); //wait for release
return key; //return key number
}
k = k + 4; //next row key number
keyport |= 0x80 >> i; //make the row high again
}
return 0; //return 0 if no key pressed
}
void main()
{
unsigned char keypress;
Port =0xff; //all keypad pins high
while(1)
{
keypress=get_key();
if(keypress)
Lcd_Data(keypress);
}
}
Hi,
Without schematic....hard to say.
But with a quick view...
* I see no debouncing software (this is not the source of your problem)
* and I see no "wait" for the signals to settle (usually this is needed with a matrix keypad)
Additionally it seems you tried to write the code as a whole...without dividing it into smaller pieces that can be verified individually.
Klaus
A couple of questions about the code. In the defines, C0 is defined as P1^0,. should this be P0^0 ?.
In main, PORT is set to all 1"s. In function get_key, keyport is used, I don't see this variable declared or defined. Is it "PORT" ?
#include <reg51.h>
#define keyport P0 //keypad connected to port P0
#define C1 P0^0
#define C2 P0^1
#define C3 P0^2
#define C4 P0^3
char get_key()
{
unsigned char i, k, key=0;
k = 1;
for( i = 0; i < 4; i++) //loop for 4 rows
{
keyport &=~ (0x80 >> i); //to make rows low 1 by 1
if(!C1) //check if key1 is pressed
{
key = k+0; //set key number
while(!C1); //wait for release
return key; //return key number
}
if(!C2) //check if key2 is pressed
{
key = k+1; //set key number
while(!C2); //wait for release
return key; //return key number
}
if(!C3) //check if key3 is pressed
{
key = k+2; //set key number
while(!C3); //wait for release
return key; //return key number
}
if(!C4) //check if key4 is pressed
{
key = k+3; //set key number
while(!C4); //wait for release
return key; //return key number
}
k = k + 4; //next row key number
keyport |= 0x80 >> i; //make the row high again
}
return 0; //return 0 if no key pressed
}
void main()
{
unsigned char keypress;
keyport =0xff; //all keypad pins high
while(1)
{
keypress=get_key();
if(keypress)
Lcd_Data(keypress);
}
}
PORT 0 of 89C51 is open collector. Are there pull up resistors wired to pull PORT 0 hIgh ?
Hi,
are you sure R1, C3 connection is correct?
Klaus
R1, C3 have nothing to do with keypad. I doubt that you connected it correctly. Give link to where you found the circuit.I am sure connection is correct. I searched on internet for keypad function. it's look correct
Hi,
R1, C3 have nothing to do with keypad. I doubt that you connected it correctly. Give link to where you found the circuit.
Klaus
In opposite to the link schematic your RESET is connected to VCC directely. If you modify the design you should be aware of what you do.
I don´t know if your circuit is correct for your microcontroller.
The code I posted was just an example. if you read post#1 there you can see I am having problem with function not port. if you are saying to connect keypad to port P1. i will connect where you will say to connect.In addition to the schematic showing incorrect reset wiring, it does not agree with the code you posted above. The schematic shows the keypad on port 1 instead of port 0 shown in the code, and shows
the LCD on port 3 instead of port 1. Is this really the schematic for your circuit ?
This is like bringing my neighbour´s car to the garage when my car fails.The code I posted was just an example.
Hi,
This is like bringing my neighbour´s car to the garage when my car fails.
It´s annoying when one tries to help .. but one can not rely on the given informations. Neither schematic, nor code.
I leave this thread.
Klaus
What's the wrong ? I wanted to make function that can return number so I just did google and found the some example. I checked that design that was working fine with the code given in link so I thought design is okay but there is fault in my program. then I wrote routine for keypad. I asked is this a better way to read the keypad. My question was, what would be a good keypad routine in c.
The 8051 family microcontroller has P0,1,2,3 Ports with slightly different circuitry, so if you do not provide accurate information, indicating what is connected to where, no one can assess whether your problem lies in hardware or firmware scope.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?