Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

problem with key pad matrix interfacing

Status
Not open for further replies.

sachin.kolkar

New Member
hi,
i m facing problem with 4X4 keypad matrix interfacing with PIC16F72 uc
When i run the program with simulator it works fine... one port is i am using for the key value using LED display .
when i connect the led & key board matrix , LED's are toggles continuesly in random manner
here is my code
Code:
#define keyport PORTB           //Keypad Port
#define keyportddr TRISB      //Data Direction Register
static char keytab[17]={0,1,2,3,10,4,5,6,11,7,8,9,12,13,0,14,15};
unsigned char keyval;   //A variable
void key_init();
unsigned char get_key();
 unsigned char i,key=1,j;
 void compare();
int main()
{
 DDRD=0x00;
 PORTD=0x00;
 DDRA=0xC0;
 PORTA=0xFF;
while(1)
{
 key_init();

get_key();


}
}

/*
+---------------------------------------+
| Prototype: void key_init(void);       |
| Return Type: void                     |
| Arguments: None                       |
| Description: Initialize ports and     |
|              Keypad.                  |
+---------------------------------------+
*/
void key_init(){
        keyportddr = 0xF0;
        keyport = 0xFF;
       

}

/*
+-----------------------------------------------+
| Prototype: unsigned char get_key(void);       |
| Return Type: unsigned char                    |
| Arguments: None                               |
| Description: To read key from the keypad      |
+-----------------------------------------------+
*/
unsigned char get_key(){
       key=1;
        for(i=0;i<4;i++){               //Loop for 4 rows
                keyport &=~(0x01<<i);   //Make rows low one by one
                        if(PORTB.F4==0){
                         //check COL1
                                while(!(keyportpin & (1<<col1)));
                                 
                                //wait for release
                                PORTD=keytab[key];
                                return key;
                                //return pressed key value
                        }
                        if((PORTB.F5==0))){
                         //Check COL2
                                key += 1;
                                //Second key pressed
                                while(!(keyportpin & (1<<col2)));
                                //wait for release
								PORTD=keytab[key];
                                return key;
                                //return key value
                        }
                        if(PORTB.F6==0){
                         //Check COL3
                                key += 2;
                                //Third key pressed
                                while(!(keyportpin & (1<<col3)));
                                //Wait for release
								               PORTD=keytab[key];
                                return key;
                                //Return value
                        }
                        if(PORTB.F7==0){
                         //check COL4
                                key += 3;
                                //Fourth key pressed
                                while(!(keyportpin & (1<<col4)));
                                //Wait for release
								PORTD=keytab[key];
                                return key;
                                //Return key value
                        }
                key +=4;        //Next row
                keyport |= 0x01<<i;
                //make read row high again
        }
        return 0;   //return false if no key pressed
}


PLZ help me guys......
 
A quick look at your code suggests you have only posted part of it. There are no definitions for col1, col2, ddrd(???) etc. I also have no idea if you are including the right files and setting the config value. Could you also post which compiler you are using.

Mike.
 
sorry, here is the code
Code:
#define keyport PORTB           //Keypad Port
#define keyportddr TRISB      //Data Direction Register
static char keytab[17]={0,1,2,3,10,4,5,6,11,7,8,9,12,13,0,14,15};
unsigned char keyval;   //A variable
void key_init();
unsigned char get_key();
 unsigned char i,key=1,j;
 void compare();
int main()
{
 TRISC=0x00;
 PORTC=0x00;
 while(1)
{
 key_init();

get_key();


}
}

/*
+---------------------------------------+
| Prototype: void key_init(void);       |
| Return Type: void                     |
| Arguments: None                       |
| Description: Initialize ports and     |
|              Keypad.                  |
+---------------------------------------+
*/
void key_init(){
        keyportddr = 0x0F;
        keyport = 0xFF;
       

}

/*
+-----------------------------------------------+
| Prototype: unsigned char get_key(void);       |
| Return Type: unsigned char                    |
| Arguments: None                               |
| Description: To read key from the keypad      |
+-----------------------------------------------+
*/
unsigned char get_key(){
       key=1;
        for(i=0;i<4;i++){               //Loop for 4 rows
                keyport &=~(0x01<<i);   //Make rows low one by one
                        if(PORTB.F4==0){
                         //check COL1
                                while(!(keyportpin & (1<<col1)));
                                 
                                //wait for release
                                PORTD=keytab[key];
                                return key;
                                //return pressed key value
                        }
                        if((PORTB.F5==0))){
                         //Check COL2
                                key += 1;
                                //Second key pressed
                                while(!(keyportpin & (1<<col2)));
                                //wait for release
								PORTD=keytab[key];
                                return key;
                                //return key value
                        }
                        if(PORTB.F6==0){
                         //Check COL3
                                key += 2;
                                //Third key pressed
                                while(!(keyportpin & (1<<col3)));
                                //Wait for release
								               PORTD=keytab[key];
                                return key;
                                //Return value
                        }
                        if(PORTB.F7==0){
                         //check COL4
                                key += 3;
                                //Fourth key pressed
                                while(!(keyportpin & (1<<col4)));
                                //Wait for release
								PORTD=keytab[key];
                                return key;
                                //Return key value
                        }
                key +=4;        //Next row
                keyport |= 0x01<<i;
                //make read row high again
        }
        return 0;   //return false if no key pressed
}
 
Last edited:
Hi,
why not using the built-in Keypad library?

If you have erratic results, to me, it sounds like you forgot to add some pull-up resistors.

HTH
 
Last edited:
mister_e said:
Hi,
why not using the built-in Keypad library?
i was use that built in keypad library...........but led's toggles without pressing any key.....

If you have erratic results, to me, it sounds like you forgot to add some pull-up resistors.

HTH
i did it boss u can see this in my code,
if i m using key pad library i can't do this.....
 
I can't see where in your code you are turning on the WPUs. I can't see where you write to a port to light LEDs.

Mike.
 
sachin.kolkar said:
i m initialize PORTC for the LED light....:confused:

You set port C to output and set all pins low but then you write the keyboard value to Port D. The 16F72 doesn't even have a Port D.:confused:

Mike.
 
sachin.kolkar said:
sorry, here is the code
Code:
#define keyport PORTB           //Keypad Port
#define keyportddr TRISB      //Data Direction Register
static char keytab[17]={0,1,2,3,10,4,5,6,11,7,8,9,12,13,0,14,15};
unsigned char keyval;   //A variable
void key_init();
unsigned char get_key();
 unsigned char i,key=1,j;
 void compare();
int main()
{
 TRISC=0x00;
 PORTC=0x00;
 while(1)
{
 key_init();

get_key();


}
}

/*
+---------------------------------------+
| Prototype: void key_init(void);       |
| Return Type: void                     |
| Arguments: None                       |
| Description: Initialize ports and     |
|              Keypad.                  |
+---------------------------------------+
*/
void key_init(){
        keyportddr = 0xF0;
        keyport = 0xFF;
       

}

/*
+-----------------------------------------------+
| Prototype: unsigned char get_key(void);       |
| Return Type: unsigned char                    |
| Arguments: None                               |
| Description: To read key from the keypad      |
+-----------------------------------------------+
*/
unsigned char get_key(){
       key=1;
        for(i=0;i<4;i++){               //Loop for 4 rows
                keyport &=~(0x01<<i);   //Make rows low one by one
                        if(PORTB.F4==0){
                         //check COL1
                                while(!(keyportpin & (1<<col1)));
                                 
                                //wait for release
                                PORTC=keytab[key];
                                return key;
                                //return pressed key value
                        }
                        if((PORTB.F5==0))){
                         //Check COL2
                                key += 1;
                                //Second key pressed
                                while(!(keyportpin & (1<<col2)));
                                //wait for release
								PORTC=keytab[key];
                                return key;
                                //return key value
                        }
                        if(PORTB.F6==0){
                         //Check COL3
                                key += 2;
                                //Third key pressed
                                while(!(keyportpin & (1<<col3)));
                                //Wait for release
								               PORTC=keytab[key];
                                return key;
                                //Return value
                        }
                        if(PORTB.F7==0){
                         //check COL4
                                key += 3;
                                //Fourth key pressed
                                while(!(keyportpin & (1<<col4)));
                                //Wait for release
								PORTC=keytab[key];
                                return key;
                                //Return key value
                        }
                key +=4;        //Next row
                keyport |= 0x01<<i;
                //make read row high again
        }
        return 0;   //return false if no key pressed
}
i corrected it ....
another problem is when i connect the RB4-RB7,
all portc leds start toggling..
 
This suggests that you have not even managed to get this code to compile. As I said in my initial reply col1, col2 etc weren't defined, and they still aren't.

Are you posting all your code?

Mike.
 
Pommie said:
This suggests that you have not even managed to get this code to compile. As I said in my initial reply col1, col2 etc weren't defined, and they still aren't.

Are you posting all your code?

Mike.
yes, i posted complete code , compiled & run the code, it works fine in simulator.....
while i m using the mikroC keypad library.....same problem encountering...:confused:
 
when i initialise any port as input port & pull up resistors high..
but when i connect my DMM across that port pins it shows the low at that pins.....

Ex:
TRISB=0xFF;
PORTB=0xFF;
 
How can it compile when there are variables/constants undefined?

Writing 0xff to an input port does not pull it high. It is not an 8051. Look in the data sheet at the option register.

Mike.
 
Pommie said:
Writing 0xff to an input port does not pull it high. It is not an 8051. Look in the data sheet at the option register.

Mike.

i was pull down the PORTB registers,making one row high and check the columns......
but the same problem occoring..
help me boss..............
 
Pommie said:
How can it compile when there are variables/constants undefined?

Writing 0xff to an input port does not pull it high. It is not an 8051. Look in the data sheet at the option register.

Mike.
Pommie, it works fine ..
i used keypad library from mikroC for programming purpose.....
Problem is with the keypad matrix.....
i did not ground rows throgh 10k or 1000k resistor......
after this it works fine......:)
 
Status
Not open for further replies.

Latest threads

Back
Top