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.

Problems with 4x4 keypad interact with LCD..

Status
Not open for further replies.

chunei

New Member
Hi..
I have a problem here...

My 4x4 keypad had been connected to PIC16F877A Port C...according to this datasheet : https://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!!
 
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;
}
}
}
 
tis is a part of my schematic.....
 

Attachments

  • snapshot20080406231431.JPG
    snapshot20080406231431.JPG
    57.1 KB · Views: 508
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.
 
chunei said:
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.
 
blueroomelectronics said:
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??
 
Nigel Goodwin said:
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?:confused: And, what does the compiler say?

Chunei,

You just need the pullups on the inputs.

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

Attachments

  • snapshot20080408000445.JPG
    snapshot20080408000445.JPG
    61.3 KB · Views: 382
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:
thanks for the advise.... will updates u all after i fix it...

and

NIGEL... can u tell me which are the inputs that u wan to mention????
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top