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.

keypad interfacing with PIC16F877a

Status
Not open for further replies.

zakir_cool

Junior Member level 3
Junior Member level 3
Joined
Sep 27, 2012
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,558
Hi i am trying keypad interfacing using PIC16F877a,this code is giving a error,but this same is code working fine in 8051 with little modification of ports.Can any body figure it out what's the mistake.my intention is to give four char password.
Code:
unsigned char pass[4];

#define c1 RA5;
#define c2 RA6;
#define c3 RA7;
#define r1 RA0;
#define r2 RA1;
#define r3 RA2;


void keypad()
{
unsigned int i;
i=0;
do
{
r1=0; r2=r3=1;
if(c1==0)
{
while(c1==0);
delay(5000);
pass[i++]='1';
lcddata('*');

}
if(c2==0)
{
while(c2==0);
delay(5000);
pass[i++]='2';
lcddata('*');

}

if(c3==0)
{
while(c3==0);
delay(5000);
pass[i++]='3';
lcddata('*');

}

r2=0; r1=r3=1;
if(c1==0)
{
while(c1==0);
delay(5000);
pass[i++]='4';
lcddata('*');

}
if(c2==0)
{
while(c2==0);
delay(5000);
pass[i++]='5';
lcddata('*');

}

if(c3==0)
{
while(c3==0);
delay(5000);
pass[i++]='6';
lcddata('*');

}
r3=0; r2=r1=1;
if(c1==0)
{
while(c1==0);
delay(5000);
pass[i++]='7';
lcddata('*');

}
if(c2==0)
{
while(c2==0);
delay(5000);
pass[i++]='8';
lcddata('*');

}
if(c3==0)
{
while(c3==0);
delay(5000);
pass[i++]='9';


}
}while(i<=3);
pass='\0';
}
 

Code:
#include "Includes.h"


// Function name: InitKeypad
void InitKeypad(void)
{
	Keypad_PORT	    = 0x00;	// Set Keypad port pin values zero
	Keypad_PORT_Dir = 0xF0;	// Last 4 pins input, First 4 pins output

	// Enable weak internal pull up on input pins
	OPTION_REG &= 0x7F;
}



// Function name: READ_SWITCHES
// Scan all the keypad keys to detect any pressed key.
char READ_SWITCHES(void)	
{	
	RowA = 0; RowB = 1; RowC = 1; RowD = 1; 	//Test Row A

	if (C1 == 0) { __delay_ms(250); while (C1==0); return '7'; }
	if (C2 == 0) { __delay_ms(250); while (C2==0); return '8'; }
	if (C3 == 0) { __delay_ms(250); while (C3==0); return '9'; }
	if (C4 == 0) { __delay_ms(250); while (C4==0); return '/'; }

	RowA = 1; RowB = 0; RowC = 1; RowD = 1; 	//Test Row B

	if (C1 == 0) { __delay_ms(250); while (C1==0); return '4'; }
	if (C2 == 0) { __delay_ms(250); while (C2==0); return '5'; }
	if (C3 == 0) { __delay_ms(250); while (C3==0); return '6'; }
	if (C4 == 0) { __delay_ms(250); while (C4==0); return 'x'; }
	
	RowA = 1; RowB = 1; RowC = 0; RowD = 1; 	//Test Row C

	if (C1 == 0) { __delay_ms(250); while (C1==0); return '1'; }
	if (C2 == 0) { __delay_ms(250); while (C2==0); return '2'; }
	if (C3 == 0) { __delay_ms(250); while (C3==0); return '3'; }
	if (C4 == 0) { __delay_ms(250); while (C4==0); return '-'; }
	
	RowA = 1; RowB = 1; RowC = 1; RowD = 0; 	//Test Row D

	if (C1 == 0) { __delay_ms(250); while (C1==0); return 'C'; }
	if (C2 == 0) { __delay_ms(250); while (C2==0); return '0'; }
	if (C3 == 0) { __delay_ms(250); while (C3==0); return '='; }
	if (C4 == 0) { __delay_ms(250); while (C4==0); return '+'; }

	return 'n';           	// Means no key has been pressed
}


// Function name: GetKey
// Read pressed key value from keypad and return its value
char GetKey(void)           	 // Get key from user
{
	char key = 'n';              // Assume no key pressed

	while(key=='n')              // Wait untill a key is pressed
		key = READ_SWITCHES();   // Scan the keys again and again

	return key;                  //when key pressed then return its value
}

use this header file and the call it main as
InitKeypad(); // Initialize Keypad
and when calling keypad use this
num1 = get_num(key); // Get int number from char value, it checks for wrong input as well
 

first, I assume that you were using a keil compiler with the 8051, but which compiler are you using for the PIC???
second, are you taking in account that it's imperative to set-up the pin direction (via TRISA register) prior using the IO port?
third, did you checked the LCDdata routine? does it have also errors???
(BTW, which are the exact errors did you experience with your code)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top