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.

How to create a program in PIC16F877A to save password for Combination Lock using EMF

Status
Not open for further replies.

josh_world90

Newbie level 1
Joined
Sep 20, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,288
Hello, I'm a newbie here... I'm doing a project, "password door security". I need to program my pic to be able to change the password. I don't know how to continue... What's programmed on the password section seems to be wrong.
I've programmed till here... Some1 please help me..\ How to disable the "*" button.. I only want it to work as a button to enter the settings to change password.

Code:
//=======================================================================================
//	Author				:Josh	
/	Project				:Keypad door security
//	Project description	:PIC18F877A + 4x4 keypad + LCD are used to build a keypad door 
//						 security system which will activate the relay and buzzer after 
//						 a preset 4-digit password is entered.   
//						 LCD will display **** when keypad is pressed. 
//						 preset password for this program is 1234 
//========================================================================================


//========================================================================================
//	include
//=========================================================================================
#include <pic.h> 

//=========================================================================================
//	configuration
//=========================================================================================
__CONFIG ( 0x3F32 );

//==========================================================================================
//	define
//==========================================================================================
#define	rs			RC0
#define	e			RC1
#define	led_red		RC2
#define led_yellow	RC3
#define	lcd_data	PORTD
#define relay		RB1
#define buzzer		RB2

//===========================================================================================
//	function prototype
//===========================================================================================
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void e_pulse(void);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void clearrow1(void);
void clearrow2(void);
void clearrow3(void);
void clearrow4(void);
void scancolumn1(void);
void scancolumn2(void);
void scancolumn3(void);
void scancolumn4(void);
void beep_once(void);
void beep_twice(void);
void beep_ten(void);
void password_change(void);


//============================================================================================
//	global variable
//============================================================================================
unsigned char password_count=0;
unsigned char keyin_char[5];				// Declare an array to stall the 4-digit key in password
unsigned char stalled_char[5]="1234";		// Declare an array to stall the 4-digit desired password

//============================================================================================
//	main function
//============================================================================================
void main(void)
{
	ADCON1=0b00000110;	//set all portA pins as digital I/O
	TRISA=0b11001111;	//clear bit 4&5 portA as output and set the rest as input 
	TRISB=0b00000000;	//set portB as output
	TRISD=0b00000000;	//set portD as output
	TRISC=0b11110000;	//set bit4-7 portC as input(connected to 4 row of keypad)
	TRISE=0b00000000;	//set portE as output

	PORTC=0;
	PORTD=0;
	relay=0;
	buzzer=0;
	led_yellow=0;
	led_red=0;

	here:
	send_config(0b00001001);	//clear display at lcd
	send_config(0b00000010);	//Lcd Return to home 
	send_config(0b00000110);	//entry mode-cursor increase 1
	send_config(0b00001100);	//diplay on, cursor off and cursor blink off
	send_config(0b00111000);	//function
	
	
	lcd_clr();							//clear LCD
	delay(1000);						//delay
	lcd_goto(0);						//initial display
	send_string("PLEASE ENTER");		//Display "PLEASE ENTER" on lcd
	lcd_goto(20);						//Display on 2nd line
	send_string("4-DIGIT PASSWORD");	//Display "4-DIGIT PASSWORD" on lcd

	
	while(1)
	{											//keypad scanning algorithm
			clearrow1();						//Clear 1st output pin and set the others
			scancolumn1();						//scan column 1-4
			clearrow2();						//Clear 2nd output pin and set the others
			scancolumn2();						//scan column
			clearrow3();						//Clear 3rd output pin and set the others
			scancolumn3();						//scan column
			clearrow4();						//Clear 4th output pin and set the others
			scancolumn4();						//scan column
			
			if(password_count==4)	
			{
				password_count=0;
				if((keyin_char[0]==stalled_char[0])&&(keyin_char[1]==stalled_char[1])&&
					(keyin_char[2]==stalled_char[2])&&(keyin_char[3]==stalled_char[3]))	//compare the keyin value with stalled value to test whether password is correct
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("SUCCESS!");		//display SUCCESS
					led_yellow=1;					//yellow light on //RC3 
					relay=1;						//relay on        //RB1
					beep_once();					//beep one time 
					delay(1500000);						//delay
					relay=0;						//relay off (door closed)
					led_yellow=0;					//off led yellow
					beep_ten();						//beep ten time 	
					goto here;
					while(1);						//infinity loop 
				}
				else
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("ERROR!");			//display ERROR!
					led_red=1;						//red light on
					beep_twice();					//beep two time
 					delay(150000);					//delay
					led_red=0;						//red light off
					goto here;
					while(1);						//infinity loop 
				}
			}
		}
}

//=======================================================================================
//	scanning functions
//=======================================================================================
void clearrow1(void)			//clear the 1st row and set the others
{
	RE1=0;						//RE1,RE0, RA5 and RA4 are the output pins from PIC which connect to 4 pins of keypad		
	RE0=1;
	RA5=1;
	RA4=1;				
}

void clearrow2(void)			//clear the 2nd row and set the others
{
	RE1=1;								
	RE0=0;
	RA5=1;
	RA4=1;				
}

void clearrow3(void)			//clear the 3rd row and set the others
{
	RE1=1;								
	RE0=1;
	RA5=0;
	RA4=1;				
}

void clearrow4(void)			//clear the 4th roe and set the others
{
	RE1=1;								
	RE0=1;
	RA5=1;
	RA4=0;				
}

void scancolumn1(void)
{
	if(RA0==0)							//if key '1' is being pressed
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='1';	//Stall the '1' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA1==0)							//if key '2' is being pressed
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='2';	//Stall the '2' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA2==0)						//if key '3' is being pressed
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='3';	//Stall the '3' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA3==0)						//if key 'A' is being pressed
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='A';	//Stall the 'A' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
}

void scancolumn2(void)
{
	if(RA0==0)							//if key '4' is being pressed
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='4';	//Stall the '4' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA1==0)							//if key '5' is being pressed
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='5';	//Stall the '5' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA2==0)						//if key '6' is being pressed
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='6';	//Stall the '6' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA3==0)						//if key 'B' is being pressed
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='B';	//Stall the 'B' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
}

void scancolumn3(void)
{
	if(RA0==0)						//if key '7' is being pressed
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='7';	//Stall the '7' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA1==0)						//if key '8' is being pressed
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='8';	//Stall the '8' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA2==0)						//if key '9' is being pressed
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='9';	//Stall the '9' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA3==0)						//if key 'C' is being pressed
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='C';	//Stall the 'C' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
}

void scancolumn4(void)
{
	if(RA0==0)						//if key '*' is being pressed
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='*';	//Stall the '*' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
		password_change();
						
	}
	else if(RA1==0)						//if key '0' is being pressed
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='0';	//Stall the '0' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}
	else if(RA2==0)						//if key '#' is being pressed
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='#';	//Stall the '#' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
		
	}
	else if(RA3==0)						//if key 'D' is being pressed
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(password_count==0)lcd_clr();	//Clear the LCD if the key is the 1st password
		lcd_goto(password_count);		//The cursor of LCD points to the column equivalent to the value of password_count variable
		send_char('*');					//Display the symbol '*' at LCD	
		keyin_char[password_count]='D';	//Stall the 'D' value at the keyin_char array
		password_count+=1;				//increase the Password_count variable's value by 1 and the result stall back to the variable	
		beep_once();
	}	
}

//===============================================================================================
//	General Purpose	functions
//===============================================================================================
void delay(unsigned long data)
{
	for( ;data>0;data-=1);
}

void beep_once(void)
{
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
}

void beep_twice(void)
{
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(13000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
}
void beep_ten(void)
{
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	delay(8000);
	buzzer=1;						//buzzer on
	delay(8000);
	buzzer=0;						//buzzer off
	
}
//========================================================================================
//	LCD	functions
//========================================================================================
void send_config(unsigned char data)
{
	rs=0;								//clear rs into config mode 
	lcd_data=data;
	delay(50);
	e_pulse();
}

void send_char(unsigned char data)
{
	rs=1;								//set rs into write mode
	lcd_data=data;			            			 
	delay(50);
	e_pulse();
}

void e_pulse(void)
{
	e=1;
	delay(50);
	e=0;
	delay(50);
}

void lcd_goto(unsigned char data)
{
 	if(data<16)
	{
	 	send_config(0x80+data);
	}
	else
	{
	 	data=data-20;
		send_config(0xc0+data);
	}
}

void lcd_clr(void)
{
 	send_config(0x01);
	delay(50);	
}

void send_string(const char *s)
{          
	unsigned char i=0;
  	while (s && *s)send_char (*s++);

}


void password_change(void)
{
		
	lcd_clr();						//clear lcd
	lcd_goto(0);
	send_string("Please Enter");		//display SUCCESS
	lcd_goto(20);						//Display on 2nd line
	send_string("U'r Old Password:");	//Display "6-DIGIT PASSWORD" on lcd

		while(password_count<5)
		{
			clearrow1();						//Clear 1st output pin and set the others
			scancolumn1();						//scan column 1-4
			clearrow2();						//Clear 2nd output pin and set the others
			scancolumn2();						//scan column
			clearrow3();						//Clear 3rd output pin and set the others
			scancolumn3();						//scan column
			clearrow4();						//Clear 4th output pin and set the others
			scancolumn4();						//scan column
		}
		
				password_count=0;
				if((keyin_char[0]==stalled_char[0])&&(keyin_char[1]==stalled_char[1])&&
					(keyin_char[2]==stalled_char[2])&&(keyin_char[3]==stalled_char[3]))	//compare the keyin value with stalled value to test whether password is correct
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
				
				
					send_string("Now enter");		//display SUCCESS
					lcd_goto(20);						//Display on 2nd line
					send_string("U'r New Password:");	//Display "6-DIGIT PASSWORD" on lcd
				 
					
					while(password_count<4)
					{
						clearrow1();						//Clear 1st output pin and set the others
						scancolumn1();						//scan column 1-4
						clearrow2();						//Clear 2nd output pin and set the others
						scancolumn2();						//scan column
						clearrow3();						//Clear 3rd output pin and set the others
						scancolumn3();						//scan column
						clearrow4();						//Clear 4th output pin and set the others
						scancolumn4();						//scan column
					}	
						 
					 for (int n=0;n<4;n++)
					 {
					 stalled_char[n]=keyin_char[n];
					 }
					 
					 lcd_clr();						//clear lcd
				     lcd_goto(0);
				     send_string("Password Changed!");		//display SUCCESS
			     	led_yellow=1;					//yellow light on 
					led_red=1;						//red light on
				}
				
				else
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("Err, Please press");			//display ERROR!
 					lcd_goto(20);						//Display on 2nd line
					send_string("'*'to try again!");	//Display "6-DIGIT PASSWORD" on lcd
					led_red=1;						//red light on
					beep_twice();					//beep two time 
					delay(200);						//Delay for seconds 

				}
			
}
 
Last edited by a moderator:

Hi josh_world90,

please dont put whole code here..
Rather you should explain the problem briefly.

thank you.
 

Hello, I'm a newbie here... I'm doing a project, "password door security". I need to program my pic to be able to change the password. I don't know how to continue... What's programmed on the password section seems to be wrong.
I've programmed till here... Some1 please help me..\ How to disable the "*" button.. I only want it to work as a button to enter the settings to change password.

Actually, there is nothing wrong with posting your entire code listing, however I would suggest enclosing your code in CODE tags, using the CODE tag button (#) under "Go Advanced" or simply upload your code using the features under "Go Advanced".

Doing so would make examining, debugging and replying to your posting more convenient.

You mention the password code section seems to have issues. Can you elaborate on what exactly these issues are?

Concerning the "*" button, you can effectively disable the button by implementing a state or flag variable which is only set active in a particular section of code, the button's routine can then check to see if the state/flag variable is set before executing the button handler code.

BigDog
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top