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 in interfacing of 4x4 keypad wit PIC

Status
Not open for further replies.
iam about to interface a 4x4 hex keypad with pic16f877a
here is my code

------------------------------------------------------------------------------------------------------------
#include"lcd.h" //lcd header file( working properly with both string and numbers)
void main()
{
unsigned int ar[]={0xE7,0xD7,0xB7,0x77,0xEB,0xDB,0xBB,0x7B,0xED,0xDD,0xBD,0x7D,0xEE,0xDE,0xBE,0x7E}; //scanning code for (4x4)keypad

unsigned int a,b,c,i;
init();
TRISD=0; // keypad connected to portd
while(1)
{
PORTD=0x0F; // initially 0F
while(PORTD==0x0F); /*wait for any key to pressed( here PORTD status supposed be 0F untill any button is pressed, but it changes by default as shown in simulation in proteus below. That is why the code below doesent work properly*/

// but in 8051 the status of any port does changes only when the button is pressed and the code further work properly

a=PORTD;

PORTD=0xF0;

b=PORTD;
c=a|b;
for(i=0;i<16;i++)
{
if (ar==c)
num(i);
}
}
}
---------------------------------------------------------------------------------------------
This code has been tested with 8051 and has being properly working..provided the syntax changed for 8051 programing e.g. P2 instead of PORTD... etc.
as u see in the snapshot yellow dots are there instead of red or blue

whats wrong could anybody explain??
here is the snapshot of simulation at the early stage when no key is being pressed.
 

Attachments

  • Capture3.PNG
    Capture3.PNG
    45.2 KB · Views: 855
I can't see how it can ever work with all of port D set to output.

Mike.
 
I can't see how it can ever work with all of port D set to output.

Mike.
exactly!!
thats my point how can i manage to set PORT as input as well as output
should i place individually TRISD=0/TRISD=1 before the line where it needed

i did not tried it because at the starting point PORTD is set as an output also even no key is pressed but yellow dots sre shown at some of the pins of PORTD
this really confuses me.. thats the reason i did not tried
 
You need to make one pin at a time output and read the four inputs, something like,
Code:
int keys;
    for(i=1;i<16;i*=2){
        TRISD=255-i;
        PORTD=255-1;
        keys<<=4;
        keys|=((PORTD&0xf0)>>4)^0x0f;
    }
You will also need pullup resistors on portd bits 4 to 7. If you switch to portb there are inbuilt pullups.
After this keys will contain the 16 keys as bits.

Mike.
 
You need to make one pin at a time output and read the four inputs, something like,
Code:
int keys;
    for(i=1;i<16;i*=2){
        TRISD=255-i;
        PORTD=255-1;
        keys<<=4;
        keys|=((PORTD&0xf0)>>4)^0x0f;
    }
You will also need pullup resistors on portd bits 4 to 7. If you switch to portb there are inbuilt pullups.
After this keys will contain the 16 keys as bits.

Mike.

thanks for your help sir but
sorry iam a noob here can't understand your point

can u explain me why are there is yellow dots on PORTD even the key is not pressed.. and is there any way we can fix it?
 
You have ported the code from the humble 8051.... They have different port parameters...

You only need to write a 1 to an intel port to make it an input.... The yellow dots are because there is logic contention

Logic contention means 5v and ground are connected together....

To run the above example the four lower bits of PORTD need to be inputs...

TRISD = 0x0F;
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top