![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| 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......
__________________ |
| | |
| | #2 (permalink) |
| Experienced Member Join Date: Mar 2005 Location: Brisbane Australia
Posts: 3,244
| 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. |
| | |
| | #3 (permalink) |
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| 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 by sachin.kolkar; 7th May 2008 at 10:04 AM. |
| | |
| | #5 (permalink) |
| Experienced Member Join Date: Jul 2006 Location: Montreal, Canada
Posts: 275
| 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 by mister_e; 5th May 2008 at 07:40 PM. |
| | |
| | #6 (permalink) | |
| Experienced Member Join Date: Apr 2008 Location: Belgrade
Posts: 106
| Quote:
http://www.mikroe.com/pdf/keypad_board_schematic.pdf http://www.mikroe.com/zip/keypad_board_examples.zip you have there everything you need to make the keypad work
__________________ http://www.it4um.com | |
| | |
| | #7 (permalink) | ||
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| Quote:
Quote:
if i m using key pad library i can't do this.....
__________________ | ||
| | |
| | #9 (permalink) | |
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| Quote:
__________________ | |
| | |
| | #10 (permalink) | |
| Experienced Member Join Date: Mar 2005 Location: Brisbane Australia
Posts: 3,244
| Quote:
Mike. | |
| | |
| | #11 (permalink) | |
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| Quote:
another problem is when i connect the RB4-RB7, all portc leds start toggling..
__________________ | |
| | |
| | #12 (permalink) |
| Experienced Member Join Date: Mar 2005 Location: Brisbane Australia
Posts: 3,244
| 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. |
| | |
| | #13 (permalink) | |
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| Quote:
while i m using the mikroC keypad library.....same problem encountering...
__________________ | |
| | |
| | #15 (permalink) |
| Experienced Member Join Date: Dec 2007 Location: BANGALORE,INDIA
Posts: 39
| 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;
__________________ |
| | |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| Thread | Thread Starter | Forum | Replies | Latest |
| problem with serial port interfacing | froten | Micro Controllers | 1 | 2nd January 2007 06:07 PM |
| Problem Interfacing Basic Stamp BS2px24 with LM629 | kwang | General Electronics Chat | 4 | 25th December 2006 09:07 AM |
| problem in interfacing msm6242b with at89c52 | esconele | Micro Controllers | 21 | 18th May 2006 10:46 AM |
| Problem when interfacing servo motors with PIC18F452 MCU! | A_D_G | Micro Controllers | 2 | 14th February 2006 09:50 AM |
| pc keyboard interfacing problem | econsyst | Micro Controllers | 0 | 8th November 2003 08:28 PM |