[help]how to initialize a port of at89c2051 when using 4x3 keypad..

Thread Starter

romel_emperado

Joined Jul 23, 2009
5
hi all, i just want to ask a little thing about this blog i found on the net..
it is all about 4X3 Matrix-Keypad interfacing.

my concern is how to initialize an input in c program with keil to have this function.

this is the article that i found in the net.

Scanning and Identifying the Key Pressed

Figure1 shows a 4X3 matrix connected to port 1. The rows (R1 through R4) are connected to an output port and the columns (C1 through C3) are connected to an input port. Note that, we only use 7 pins of port 1.


To detect a pressed key, first, the microcontroller set pin 0 through 7 of port 1 and initiate the variable ‘Digit’ to zero. The value of ‘Digit’ represents the digit of key pressed.
Then it sends 0111 to R1 R2 R3 R4 and it reads the columns. If the data read from the columns is C1 C2 C3 =111, no key has been pressed and the process continues to next step.
If the data read from the columns is C1 C2 C3 =011, this means that a key in the R1 row and C1 column has been pressed. That is ‘1’.
In the subroutine ‘CheckColumn’, the value of ‘Digit’ is increased. The value of ‘Digit’ represents the key pressed. Before leave this subroutine, microcontroller set ‘KeyPressed’ to indicate there is a key pressed.
If the data read from the columns is C1 C2 C3 =101, this means that a key in the R1 row and C2 column has been pressed. That is ‘2’. Table 1 represents the meaning of each combination of data received at C1 C2 C3.




if i want to use port 1 as my input then i would declare P1=0xFF to initialize P1.0 to P1.7 as my input then How would i get a low or zero input if let say i want to press keypad #1?

how would i get this input like in the table below?
 

Thread Starter

romel_emperado

Joined Jul 23, 2009
5
helo i have now the code from a friend with LCD display.. my problem is i want to have a password then when the password is correct p3.4 will go high and the key # is the enter in the keypad..

heres the code:


#include <REG2051.H>
#define KEYPAD P1
#define display P3

void delay(int k)
{
int i;
TR0=0;
for(i=0; i<k; i++)
{
TH0=0x3c;
TL0=0xb0;
TF0=0;
TR0=1;
while (TF0==0);
TR0=0;
}
}

void DelayMs(unsigned int) ;
char x;

unsigned char keypad[4][3]= { 1 , 2 , 3 ,
4 , 5 , 6 ,
7 , 8 , 9 ,
15, 0 , 15 } ;






void main()
{

unsigned char colloc,rowloc;
P3=0x00;
TMOD=TMOD&0xF0;
TMOD=TMOD|0x01;

while(1)
{
do
{
KEYPAD=0xF0;
colloc=KEYPAD & 0xF0;
}
while(colloc ==0xF0); // if any key pressed
DelayMs(1); // some delay
do
{
colloc=KEYPAD;
colloc &=0xF0;
}while(colloc==0xF0); // to verify is really key pressed

KEYPAD=0xFE;
colloc=KEYPAD & 0xF0;
if(colloc !=0xF0)
{
rowloc=0;
goto next;
}

KEYPAD=0xFD;
colloc=KEYPAD & 0xF0;
if(colloc !=0xF0)
{
rowloc=1;
goto next;
}
KEYPAD=0xFB;
colloc=KEYPAD & 0xF0;
if(colloc !=0xF0)
{
rowloc=2;
goto next;
}

KEYPAD=0xF7;
colloc=KEYPAD & 0xF0;
rowloc=3;
goto next;

next:
if (colloc==0xE0)

display=(keypad[rowloc][0]);

else if(colloc==0xD0)
display=(keypad[rowloc][1]);
else if(colloc==0xB0)
display=(keypad[rowloc][2]);








}
}




//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count)
{ // mSec Delay 11.0592 Mhz
unsigned int i;
while(count) {
i = 115;
while(i>0) i--;
count--;
}
}
 
Top