Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 5th May 2008, 04:02 AM   (permalink)
Default problem with key pad matrix interfacing

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......
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 5th May 2008, 04:29 AM   (permalink)
Default

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.
Pommie is offline   Reply With Quote
Old 5th May 2008, 07:28 AM   (permalink)
Question

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
}
__________________
hm:

Last edited by sachin.kolkar; 7th May 2008 at 10:04 AM.
sachin.kolkar is offline   Reply With Quote
Old 5th May 2008, 07:32 AM   (permalink)
Default

i m using mikro C compiler......
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 5th May 2008, 07:22 PM   (permalink)
Default

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
__________________
Steve
______________________
info@mister-e.org
www.mister-e.org

Last edited by mister_e; 5th May 2008 at 07:40 PM.
mister_e is offline   Reply With Quote
Old 5th May 2008, 10:59 PM   (permalink)
Default

Quote:
Originally Posted by sachin.kolkar
i m using mikro C compiler......
Why don't you check out the:
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
arhi is offline   Reply With Quote
Old 6th May 2008, 09:22 AM   (permalink)
Question

Quote:
Originally Posted by mister_e
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.....

Quote:
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.....
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 6th May 2008, 09:36 AM   (permalink)
Default

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.
Pommie is offline   Reply With Quote
Old 7th May 2008, 08:55 AM   (permalink)
Question

Quote:
Originally Posted by Pommie
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.
i m initialize PORTC for the LED light....
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 7th May 2008, 09:01 AM   (permalink)
Default

Quote:
Originally Posted by sachin.kolkar
i m initialize PORTC for the LED light....
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.

Mike.
Pommie is offline   Reply With Quote
Old 7th May 2008, 09:46 AM   (permalink)
Default

Quote:
Originally Posted by sachin.kolkar
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..
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 7th May 2008, 09:54 AM   (permalink)
Default

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 is offline   Reply With Quote
Old 7th May 2008, 10:00 AM   (permalink)
Question

Quote:
Originally Posted by Pommie
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...
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 7th May 2008, 10:01 AM   (permalink)
Default

i m using the same code for the atmel ATTINY2313
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Old 7th May 2008, 10:10 AM   (permalink)
Default

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;
__________________
hm:
sachin.kolkar is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
problem with serial port interfacing froten Micro Controllers 1 2nd January 2007 06:06 PM
Problem Interfacing Basic Stamp BS2px24 with LM629 kwang General Electronics Chat 4 25th December 2006 09:06 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



All times are GMT. The time now is 01:25 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.