hello,
I've been trying to interface 4x4 hex keypad with pic16f877a using MPLAB X and XC8 compiler.

My task is to get 4 individual key presses from matrix keypad and convert it into a 4 digit integer and display the 4 digit integer on multiplexed common cathode seven segment display having 4 seven segment devices.

I am able to capture the key presses, also am able to display a 4 digit integer directly on to seven segment.

but whenever i need to make a 4 digit integer out of 4 different key presses and display the integer on to display, I am getting 4 equal numbers in 4 seven segment modules on a single key press.

code snippet`:

 char array[4]; // global

    int l=0,t,x=1000,key_int=0,d; // local to main function
    unsigned char key;

 while(1)
   {
       if(getkey())
       {
       key=0;    
       key=getkey();
       key_int=key-'0';
       array[l]=key_int;
       key_int=0;
       l++;
       if(l>3){l=0;}
       if(l==3)      
       {
           t=((array[0]*1000)+(array[1]*100)+(array[2]*10)+(array[3]*1)); 
           Print_seven_segment(t);
           delay(10);  // delay of 10*20 ms
           for(d=0;d<4;d++){array[d]=0;}  // clear array
       }

       }

   }

`

the getkey() function returns an unsigned character, The print_seven _segment () function accepts an integer, breaks it down to individual digits and passes each digit to corresponding positions in seven segment .

Can anyone figure out where am I going wrong?
Thanks.

A few possibilities. I'm not familiar with getkey(), but I imagine it gets called and the program stays there until a key is pressed, then it returns the key pressed. That's my assumption of the expected behavior due to the design of the loop. If that's your expectation, make sure that's true. There could be a problem if the key press causes a state change in a variable or pin and the state is changed again when the key is released. If the getkey() returns immediately if a key is in the key-down state, your loop could be iterated hundreds or thousands of times for one single key press and that would explain your problem. This is speculation since I'm not familiar with getkey() or the circuitry, but a common mistake is simply checking for key-down and returning immediately instead of checking for key-down and returning only once until there is a key-up then a key down. Your program is running much faster than your fingers press keys.

I do notice that you have two getkey() calls in your loop. Do you really want two of them? I would think you'd want one call per loop.

Also, I think you need to change how and where you adjust and test the l variable. Seems like even if the getkey() problem (if there is a problem) is solved, you're calling Print_seven_segment() after three key presses, not four.

But your main task is to somehow check your l variable to make sure it's only increased by one for every key press. Check the getkey() logic and also make sure that l is not getting overwritten somewhere else in the program since it's a global variable.