Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing 4x4 keypad with PIC16F877A in CCS...

Status
Not open for further replies.

Athar Rasul

Newbie level 4
Joined
Mar 16, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,346
hi,
I am interfacing the keypad on a single port using B0-B7. I am having problem with the key detection.. kindly help.

Code:
 int keypad(){
	int i; int key=0;
	output_B(0x01); // Start with column 1
	while((input_B() & 0xF0) == 0){ // While no key pressed
		output_B(input_B() << 1); // next column
		key++; // column number
		if(key==4){
			output_B(0x01); // Back to column 1
			key = 0;
		}
	}
	delay_ms(20); // Switch debounce

	for(i=0x10; i!=0; i<<=1){ // Find the key pressed
		if((input_B() & i) != 0)break;
		key=key+4;
	}
	output_B(0x0F);
	while((input_B() & 0xF0) != 0); // Wait until key released
	delay_ms(20); // Switch debounce
	return (key); // Return key number
}
 

I have used 4*4 keypad and following is my routine.You can use this routine

Code:
while(1)
{
 value=readkeypad();
 printf("%d",value)
}

int readkeypad (void)
{
 output_bit (r1, 0);
 output_bit (r2, 0);
 output_bit (r3, 0);
 output_bit (r4, 0);
 output_bit (r1, 1);//activate 1st row and scan for column
 delay_ms(10);
 IF (input (c1)) RETURN 10;
 IF (input (c2)) RETURN 0;
 IF (input (c3)) RETURN 11;
 IF (input (c4)) RETURN 12;
 output_bit (r1, 0);//Deactivate 1st row
 output_bit (r2, 1);//activate 2nd row and scan for column
 delay_ms(10);
 IF (input (c1)) RETURN 7;
 IF (input (c2)) RETURN 8;
 IF (input (c3)) RETURN 9;
 IF (input (c4)) RETURN 13;
 output_bit (r2, 0);//Deactivate 2nd row
 output_bit (r3, 1);//activate 3rd row and scan for column
 delay_ms(10);
 IF (input (c1)) RETURN 4;
 IF (input (c2)) RETURN 5;
 IF (input (c3)) RETURN 6;
 IF (input (c4)) RETURN 14;
 output_bit (r3, 0);//Deactivate 3rd row
 output_bit (r4, 1);//activate 4th row and scan for column
 delay_ms(10);
 IF (input (c1)) RETURN 1;
 IF (input (c2)) RETURN 2;
 IF (input (c3)) RETURN 3;
 IF (input (c4)) RETURN 15;
 output_bit (r4, 0);//Deactivate 4th row
 RETURN 255;
}
 

Man. Why are you bothering your head. The CCS compiler has got an keypad inbuilt library(KBD.C) in the driver folder. include it in your project...
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top