Problem in HEX keypad inderfacing

Discussion in 'Embedded Systems and Microcontrollers' started by bobparihar, Oct 20, 2014.

  1. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    iam about to interface a 4x4 hex keypad with pic16f877a
    here is my code

    Code (Text):
    1.  
    2. #include"lcd.h"      //lcd header file( working properly with both string and numbers)
    3. void main()
    4. {
    5. unsigned int ar[]={0xE7,0xD7,0xB7,0x77,0xEB,0xDB,0xBB,0x7B,0xED,0xDD,0xBD,0x7D,0xEE,0xDE,0xBE,0x7E};      //scanning code for (4x4)keypad
    6.  
    7. unsigned int a,b,c,i;
    8. init();
    9. TRISD=0;                        // keypad connected to portd
    10.    while(1)
    11.      {
    12.        PORTD=0x0F;                                            // initially 0F
    13.      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*/
    14.  
    15. // but in 8051 the status of any port does changes only when the button is pressed and the code further work properly
    16.  
    17.        a=PORTD;                                            
    18.  
    19.        PORTD=0xF0;
    20.  
    21.        b=PORTD;
    22.        c=a|b;
    23.        for(i=0;i<16;i++)
    24.          {
    25.            if (ar[I]==c)
    26.            num(i);
    27.          }
    28.      }
    29. }
    30.  
    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.
     
    Last edited by a moderator: Oct 20, 2014
  2. yuanan00157

    New Member

    Oct 20, 2014
    4
    0
    dear
    bobparihar:


    I do not understand the code

    Code (Text):
    1. if (ar==c)
    is the pointer type constant .Is it same to the code
    Code (Text):
    1.  if(ar[I]==c)
    2.  
    And,i am not good at PIC series MCU ,and are you sure the electric current input is more strong to current output ?
    And you can try like this :
    Code (Text):
    1.  
    2. #include"lcd.h" //lcd header file( working properly with both string and numbers)
    3. void main()
    4. {
    5. unsigned int ar[]={0xE7,0xD7,0xB7,0x77,0xEB,0xDB,0xBB,0x7B,0xED,0xDD,0xBD,0x7D,0xEE,0xDE,0xBE,0x7E}; //scanning code for (4x4)keypad
    6.  
    7. unsigned int a,b,c,i;
    8. init();
    9. [COLOR=#ff0000]//[/COLOR]TRISD=0; // keypad connected to portd
    10. while(1)
    11. {
    12. [COLOR=#ff0000]TRISD=0x0f;[/COLOR]
    13. PORTD=0x0F; // initially 0F
    14. 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*/
    15.  
    16. // but in 8051 the status of any port does changes only when the button is pressed and the code further work properly
    17.  
    18. a=PORTD;
    19. [COLOR=#ff0000]TRISD=0xf0;[/COLOR]
    20. PORTD=0xF0;
    21.  
    22. b=PORTD;
    23. c=a|b;
    24. for(i=0;i<16;i++)
    25. {
    26. if (ar[i]==c)
    27. num(i);
    28. }
    29. }
    30. }
    31.  
    My english is so so ,please never mind .
     
    Last edited by a moderator: Oct 20, 2014
  3. yuanan00157

    New Member

    Oct 20, 2014
    4
    0
    my god !--it cannot show the word on the follow snapshot !
     
  4. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    yes there is ar instead of ar i missed it..sorry
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
    Trying to show

    Code (Text):
    1.  
    2. ar[I] = c;
    3.  
    gets interpreted as italics formatting on a bulletin board.

    Use code tags when posting code.
     
    Last edited: Oct 20, 2014
  6. JWHassler

    Member

    Sep 25, 2013
    201
    33
    I don't know much about PICs, but....
    Does Port D have any peripherals (ADC, timer etc) enabled by default that would make some of the higher-order pins non-zero?
    Also: how does PortD[3:0] become non-zero when all outputs to the matrix (PortD[7:4]) are zeros?
     
  7. bobparihar

    Thread Starter Member

    Jul 31, 2014
    93
    0
    somebody please answer him
     
  8. ScottWang

    Moderator

    Aug 23, 2012
    4,853
    767
    You need to adding four 10k to ground for your inputs of keypad.
     
Loading...