+ Reply to Thread
Page 1 of 2
1 2 Last
Results 1 to 15 of 18

Thread: Problems with 4x4 keypad interact with LCD..

  1. #1
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default Problems with 4x4 keypad interact with LCD..

    Hi..
    I have a problem here...

    My 4x4 keypad had been connected to PIC16F877A Port C...according to this datasheet : http://www.futurlec.com/Keypad4x4.shtml

    However i had programme some code into the PIC & the LCD do display something but when i press the button of the keypad, it just display the wrong key...
    Example : When I press 1 >> LCD Display "0" or sometimes display different number... Even when I didnt press anything, sometime it will just display a number... & there is a Delay when i press a Key...

    Wanna ask whats the problem of my circuit????
    Isnt that I need to connect some sort of encoder instead of just connect the keypad directly to the Port C of my PIC???

    hoe someone can help me!!!! Thanks!!


  2. #2
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,709
    Blog Entries
    5

    Default

    Schematic? Code? Do you have pullups on port C?
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  3. #3
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default

    Erm.. I didnt put pullups on port C... Does this effect the interaction from keypad to PIC to LCD???? whats da purpose of pullups???

    these is the codes :

    unsigned char var1, var2, kp;
    char attempts=0;
    char state = 1;//start from state 1
    char txt[4];

    char text[] = "Combination Lock" ;//message text
    char text1[]= " *Door1 On*";
    char text2[]= " *Door2 On*";
    char text3[]= "*Door1&2 On*";

    char table[17] = {0,1,2,3,0,4,5,6,0,7,8,9,0,10,0,11,0};

    void main(void) {

    CMCON = 0x07 ;//PORTA to digital mode
    LCD_Init(&PORTB) //Pin assignment
    ;//RS,E,RW,D7,D6,D5,D4
    LCD_Cmd(LCD_CLEAR) ;//Clear display
    LCD_Cmd(LCD_CURSOR_OFF) ;//Turn cursor off
    LCD_Cmd(LCD_SECOND_ROW) ;//Move to ROW2 for rest of program
    //Seup Keypad on PORTB
    Keypad_Init(&PORTC) ;// Initialize Keypad on PORTB
    LCD_OUT(1,1,text) ;//Print welcome message
    while(1) { //while ever loop
    switch(state){ //check the current state
    case 1:
    LCD_Cmd(LCD_CLEAR) ;
    LCD_out(1,1,text);
    LCD_out(2,1,"EnterPass:");
    while(!keypad_Read()){} //wait for key1 press
    Delay_ms(5) ;//debounce time
    kp = Keypad_Released() ;//wait for release
    var1=kp; //store var1 into kp
    var1=table[kp];
    ByteToStr(var1,txt);
    Lcd_Out_Cp(txt);
    state = 2;
    break;
    case 2:
    if(kp==15) state=1; //go back state 1 when kp = # (enter key)
    else if(kp==13) state=1;//go back state 1 when kp = * (clear key)
    else
    state = 3;//go to state 3 get key2
    break;
    case 3:
    while(!keypad_Read()){} //wait for key2 press
    Delay_ms(5) ;//debounce time
    kp = Keypad_Released() ;//wait for release
    var2=kp;//store var2 into kp
    var2=table[kp];
    ByteToStr(var2,txt);
    Lcd_Out_Cp(txt);
    state=4;
    break;
    case 4:
    if(kp==15) state=1;//enter key goes back state 1
    else if(kp==13) state=1;//clear key goes back state 1
    else
    state = 5;//continue goes to state 4
    break;
    case 5:
    while(!keypad_Read()){} //wait for enter key press
    Delay_ms(10) ;//debounce time
    kp = Keypad_Released() ;//wait for release
    state = 6;
    break;
    case 6:
    if(kp==13)
    state =1; //clear goes back to s1
    else if(kp==15)
    state = 7;//enter key goes to state 7
    else state = 5;
    break;
    case 7:
    if (var1==0 && var2==5)//comparing for door1
    state = 9; //goes state 11 open door1
    else state =8;
    break;

    case 8:
    LCD_OUT(2,1,"**Invalid Code**") ;
    attempts++; //increment attempts
    kp+=attempts;//kp = kp + attempts
    if(attempts < 3) //attempts less than 3 times
    state=1; //go state 1
    else
    state=10;
    while(!keypad_Read()){} //wait for enter key press
    Delay_ms(10) ;//debounce time
    kp = Keypad_Released() ;//wait for release
    LCD_Cmd(LCD_CLEAR);
    break;
    case 9://open door1 for 5secs
    LCD_Cmd(LCD_CLEAR);
    Lcd_out(2,1,text1) ;
    Lcd_out(1,1,text);
    //turn door1 led on
    PORTA.F4=1;
    PORTB.f4=1;
    PORTB.F7 = 0;
    PORTB.F6 = 0;
    PORTB.F5 = 0 ;
    Delay_ms(5000); //delay 5secs
    //turn door1 led off
    PORTA.F4 = 0;
    LCD_Cmd(LCD_CLEAR) ;
    attempts=0;
    state=1;
    break;

    default:// must be a bad attempts
    LCD_OUT(2,1," **Alarm On**");
    LCD_OUT(1,1,text);
    PORTA.F4=1;
    PORTB.F4=0;
    PORTB.F5=0;
    PORTb.f6=0;
    PORTb.f7=1;
    Delay_ms(5000);
    PORTA.F4=0;
    LCD_Cmd(LCD_CLEAR);
    attempts=0;
    state = 1; //goes to state 1
    break;
    }
    }
    }

  4. #4
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default

    tis is a part of my schematic.....
    Attached Images

  5. #5
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,709
    Blog Entries
    5

    Default

    Please use code tags, also yes you need pullups on port C. A pullup is a resistor that pulls floating input pins to VDD, usually 10K is a good choice.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  6. #6
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default

    izit because tat i din put pullups resistor that cause this problem???

  7. #7
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,709
    Blog Entries
    5

    Default

    Add them to the 4 input lines of your keypad matrix.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  8. #8
    mdanh2002 Okay
    Join Date
    Sep 2006
    Location
    Singapore
    Posts
    91

    Default

    Quote Originally Posted by chunei
    izit because tat i din put pullups resistor that cause this problem???
    yes. if you don't put pull up resistor (connected to VCC, usually 10k) or pull down resistor (connected to GND) the value read from that port will be floating when the the input is not connected, giving wrong values. Pullup/pulldown resistors give the read value a default value of 1 or 0 if nothing is connected.

  9. #9
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default

    Quote Originally Posted by blueroomelectronics
    Add them to the 4 input lines of your keypad matrix.
    erm... 4 input lines??? my keypad is 4x4 matrix.. and it should be 8 input lines right??

  10. #10
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,794

    Default

    Quote Originally Posted by chunei
    erm... 4 input lines??? my keypad is 4x4 matrix.. and it should be 8 input lines right??
    No, 4 inputs and 4 outputs.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  11. #11
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default

    Quote Originally Posted by Nigel Goodwin
    No, 4 inputs and 4 outputs.
    Do u mean that 4inputs is for the columns & 4outputs is for the row????

    sorry for the lame Q...

  12. #12
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,794

    Default

    Read what the compiler instructions say, it's not obvious from the code - you could always stick 8 resistors on, they won't do any harm on the outputs.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  13. #13
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,807

    Default

    Quote Originally Posted by Nigel Goodwin
    Read what the compiler instructions say, it's not obvious from the code - you could always stick 8 resistors on, they won't do any harm on the outputs.
    You put pullups on your outputs? And, what does the compiler say?

    Chunei,

    You just need the pullups on the inputs.

    Mike.

  14. #14
    chunei Bad
    Join Date
    Aug 2007
    Posts
    36

    Default

    do u mean i need to put the pullups which is a 'gang resistors 10K' like in this schematic????
    Attached Images

  15. #15
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,709
    Blog Entries
    5

    Default

    That'll work.

    If you want to get fancy you could mux the 4 LCD data lines to the 4 keypad columns. (The resistors only need to go across the rows since you're driving the columns)
    Last edited by blueroomelectronics; 7th April 2008 at 04:33 PM.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

+ Reply to Thread
Page 1 of 2
1 2 Last

Similar Threads

  1. LCD 2x16 Demo for the Unicorn (18F4550)
    By blueroomelectronics in forum Micro Controllers
    Replies: 25
    Latest: 13th January 2009, 02:04 AM
  2. HD44780 LCD operation
    By ssylee in forum Micro Controllers
    Replies: 25
    Latest: 15th December 2007, 12:52 AM
  3. HD44780 lcd help
    By jay543_uk in forum Micro Controllers
    Replies: 12
    Latest: 8th November 2007, 03:26 PM
  4. PIC16F628A LCD problems.
    By HerbertMunch in forum Micro Controllers
    Replies: 17
    Latest: 2nd October 2007, 09:20 AM
  5. PicBasic Keypad program
    By Sora in forum Micro Controllers
    Replies: 0
    Latest: 20th April 2004, 07:02 PM

Tags for this Thread