PIC16F877A interface 4x4keypad 2x16LCD

Discussion in 'Embedded Systems and Microcontrollers' started by BlissEva, Jul 4, 2015.

  1. BlissEva

    Thread Starter New Member

    Jun 28, 2015
    20
    1
    can anyone help me to find out what is the problem of the code? It is successfully built but cant show the number of keypad when i press. I'm using MPLab IDE with HiTechC compiler.


    Code (Text):
    1.  
    2. #include <htc.h>
    3. #include <pic.h>
    4. #include <stdio.h>
    5.  
    6. __CONFIG(0x3F72)
    7.  
    8. #define RS RD2        //LCD
    9. #define EN RD3
    10. #define D4 RD4
    11. #define D5 RD5
    12. #define D6 RD6
    13. #define D7 RD7
    14.  
    15. #define R1 RC0        //Row1
    16. #define R2 RC1        //Row2
    17. #define R3 RC2        //Row3
    18. #define R4 RC3        //Row4
    19. #define C1 RC4        //Column1
    20. #define C2 RC5        //Column2
    21. #define C3 RC6        //Column3
    22. #define C4 RC7        //Column4
    23.  
    24. #define kyp_PORT          PORTC
    25. #define kyp_PORT_Dir    TRISC
    26.  
    27. #define FOSC
    28. #define BAUD_RATE 9.6
    29. #define BAUD_VAL (char)(FOSC/(16*BAUD_RATE))-1;
    30.  
    31. #define _XTAL_FREQ 8000000        //set the crystal oscillator frquency
    32. #include "lcd.h"
    33.                          
    34. void main(){
    35.  
    36.     TRISD = 0x00;        //LCD
    37.     TRISC = 0xF0;        //keypad
    38.  
    39.      EN = 1;
    40.     OPTION_REG &= 0x7F;
    41.  
    42.     lcd_init();
    43.     lcd_clear();
    44.     void keypad_init();
    45.     char keypad_read();
    46.     char keypad_wait();            
    47.     char key_get;
    48.     __delay_ms(100);
    49.  
    50.       while(1){
    51.         lcd_cmd(0x00);
    52.         lcd_cmd(0x01);
    53.  
    54.         __delay_ms(5);
    55.         SetCursor(1,0);
    56.         WriteString("Enter: ");
    57.         __delay_ms(5);
    58.         SetCursor(2,0);
    59.         for(int j=0;j<17;j++){
    60.             key_get = keypad_wait();
    61.             WriteChar(key_get);
    62.         }    
    63.     }
    64. }
    65.  
    66. char keypad_read(void){
    67.  
    68.     R1 = 0;        //Row1
    69.     R2 = 1;
    70.     R3 = 1;
    71.     R4 = 1;
    72.     __delay_us(30);
    73.     if(C1 == 0){ return '1';}
    74.     if(C2 == 0){ return '2';}
    75.     if(C3 == 0){ return '3';}
    76.     if(C4 == 0){ return 'A';}
    77.  
    78.     R1 = 1;        //Row2
    79.     R2 = 0;
    80.     R3 = 1;
    81.     R4 = 1;
    82.     __delay_us(30);
    83.     if(C1 == 0){ return '4';}
    84.     if(C2 == 0){ return '5';}
    85.     if(C3 == 0){ return '6';}
    86.     if(C4 == 0){ return 'B';}
    87.  
    88.     R1 = 1;        //Row3
    89.     R2 = 1;
    90.     R3 = 0;
    91.     R4 = 1;
    92.     __delay_us(30);
    93.     if(C1 == 0){ return '7';}
    94.     if(C2 == 0){ return '8';}
    95.     if(C3 == 0){ return '9';}
    96.     if(C4 == 0){ return 'C';}
    97.  
    98.     R1 = 1;        //Row4
    99.     R2 = 1;
    100.     R3 = 1;
    101.     R4 = 0;
    102.     __delay_us(30);
    103.     if(C1 == 0){ return '*';}
    104.     if(C2 == 0){ return '0';}
    105.     if(C3 == 0){ return '#';}
    106.     if(C4 == 0){ return 'D';}
    107.  
    108.     return 0xFF;
    109. }
    110.  
    111. char keypad_wait(){
    112.     char pressed_key = 0xFF;
    113.  
    114.     do{
    115.         pressed_key = keypad_read();
    116.     }
    117.     while (pressed_key == 0xFF);
    118.  
    119.     while (keypad_read() != 0xFF);
    120.  
    121.     return pressed_key;
    122. }
    123.  
    124. void kyp_init(){
    125.     TRISC = 0x00;
    126.     PORTC = 0xFF;
    127.     kyp_PORT = 0x00;
    128.     kyp_PORT_Dir = 0xF0;
    129.     OPTION_REG = 0x7F;
    130.     __delay_ms(30);
    131. }
    132.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Does anything appear on the display?

    What steps have you tried?

    Did you try using an in circuit debugger to watch the code flow?
     
  3. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,045
    3,811
    there are just too many possible problems to address without fully troubleshooting the keypad, the circuit that detects a key-press, the connections to the LCD, your chip programmer, and on and on and on.

    Could you tell us what does work in your circuit. What test programs you have written and proven that each piece of the system does work mechanically and design-wise and the only possible issue could be the programming. Then we can discuss the program.

    Cheers.
     
    absf likes this.
  4. BlissEva

    Thread Starter New Member

    Jun 28, 2015
    20
    1
    hi, my LCD can show the word "Enter" that i wrote in the main program but cannot show anything when i press my keypad.
    i never try any circuit debugger.
     
  5. BlissEva

    Thread Starter New Member

    Jun 28, 2015
    20
    1
    hi, my LCD can show what i wrote in the main but cant show the keypad value when i pressed. im new to this so i not really understand how to test my program.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Do you have a schematic to post?

    What programmer do you have? Is it a PICkit? (They do in circuit debugging.)

    What other equipment do you have? Voltmeter? Oscilloscope?
     
  7. BlissEva

    Thread Starter New Member

    Jun 28, 2015
    20
    1
    im using PICkit2. i dont have any equipment now.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    The PICkit 2 is also an in circuit debugger, and you seem to have it connected on dedicated pins so you can use it along with everything else on your board.

    I am not sure how you get the Hitech C to work in MPLAB with the debugger but start by looking at the menu to do a debug build, then the menu for the debugger where you can choose you PICkit.

    Worst case you might have to upgrade to the free XC8 compiler,which is bases on the Hitech one so most of the code remains the same.
     
  9. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,805
    833
    Are you sure that TRISC is defined correctly? It appears to me that your rows are defined as outputs and your columns as inputs... The opposite from the way you use them.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    The rows are on the low nibble, the cols are on the high nibble.

    Rows are driven, cols are read.

    TRISC or key_Port_dir is loaded with 0xF0 which makes rows on outputs, cols on inputs, which looks correct to me.
     
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,805
    833
    Ok. Sorry. I think I confused the bit numbering. Should have sketched it out before answering.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    I agree the code is confusing as things get declared at the very top and at the very bottom of the code, but it does appear to be correct.
     
  13. BlissEva

    Thread Starter New Member

    Jun 28, 2015
    20
    1

    actually what the different between XC8 compiler and Hitech?
     
  14. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,805
    833
    When no key is pressed, it leaves the column input bits floating. Don't you need a pull-up resistor on the pin inputs? Or does the Pic already include them (the Arduino can be configured either way)?
     
  15. BlissEva

    Thread Starter New Member

    Jun 28, 2015
    20
    1
    ya, there are 4 resistors at R1 R2 R3 R4.
     
  16. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,805
    833
    When I say pin inputs, I mean the column pins which are read by the Pic. From the pic's perspective, these four bits are an input. The pins I am referring to are C1 through C4 (or bits RC4 through RC7). There should be a pull-up resistor on each connection between the keypad and pic. Some microprocessors have such pull-up resistors built in (Atmega328P), but AFAIK, your pic does not.
     
  17. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,805
    833
    I just thought of something. Why are there resistors on your row outputs, R1 through R4? If they are too large, the uP will never see a high value on an input. And what are their values? I've not used resistors in this configuration and it works fine. Also, are you familiar with using pull-up resistors on a microprocessor input?
     
  18. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Point well taken: since the sense pins are looking for a low input the pins should be pulled to a high level.

    There maybe something in the code too. While it should be legal C I have seen statements such as:

    while (keypad_read() != 0xFF);

    confuse a compiler. Just add brackets for the loop made it work:

    while (keypad_read() !=0xFF) {}

    It took some nonsense link this to make this work when I ran into it.
     
  19. MrCarlos

    Active Member

    Jan 2, 2010
    400
    134
    Hello
    As far as I can see, the resistors R1, R2, R3 and R4 are not connected as Pull-Up or Pull-Down. They are in series with the inputs of the PIC.
    To be pull-up, they must be connected From the PIN to VCC.
    To be Pull-Down, they must be connected From the PIN to GND.
     
  20. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,805
    833
    I am waitng for the TS to reply as to their purpose. I agree with @MrCarlos ; they are NOT pull up nor down resistors. Likely they are included for a circuit that needs it's input pins protected. They are unnecessary in this circuit. As a matter of fact, if their value is too large, the micro will not be able to determine a reliable level.

    It is on the column inputs, that pull up resistors are needed. The keypad will present a floating input most if the time, making reads of the key press unreliable.
     
Loading...