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.

Electronic Lock PIC16F877A save new password

Status
Not open for further replies.

xachapx

New Member
I did this project using PIC16F877A and it went ok.
nothing wrong.
but the problem is how can i change a new password and it doesn't lost when the power is turn off.
what i mean is the new password will be save in memory of the pic.
and the LCD display will go like this "please enter new password".
can someone show me.
please.. !!

this is the code.

Code:
//          	:PIC16F877A + 4x3 keypad + LCD are used to build a keypad door
//						 security system which will activate the relay and buzzer after
//						 a preset 6-digit password is entered.
//						 LCD will display ****** when keypad is pressed.
//						 preset password for this program is 123456
//
//
//	include
//==========================================================================
//#include <p16F877A.h>

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

//	define
//==========================================================================
#define	rs			    portc.f0 //RC0
#define	e			      portc.f1 //RC1
#define	led_red		  portc.f2 //RC2
#define led_green	  portc.f3 //RC3
#define	lcd_data	  PORTD
#define relay		    portb.f1 //RB1
#define buzzer		  portb.f2 //RB2
#define RE0         porte.f0
#define RE1         porte.f1
#define RA5         porta.f5
#define RA4         porta.f4
#define RA3         porta.f3
#define RA2         porta.f2
#define RA1         porta.f1
#define RA0         porta.f0


//	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 scanrow1(void);
void scanrow2(void);
void scanrow3(void);
void scanrow4(void);

//	global variable
//==========================================================================
unsigned char step=0;
unsigned char lcd_count=0;
unsigned char password_count=0;
unsigned char password_wrong=0;
unsigned char sequence=0;
unsigned char testbit=0x01;
unsigned char init;

//	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=1;
	PORTD=0;
	relay=0;
	buzzer=0;
	led_green=0;
	led_red=0;
	init=0;
	sequence=0;

	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();
	delay(1000);
	lcd_goto(0);							//initial display
	send_string("PLEASE ENTER");
	lcd_goto(20);
	send_string("6-DIGIT PASSWORD");


	while(1)
	{
												//keypad scanning
			RE1=0;								//clear the 1st column and set the others
			RE0=1;
			RA5=1;
			RA4=1;
			scanrow1();							//scan row
			RE1=1;								//clear the 2nd column and set the others
			RE0=0;
			RA5=1;
			RA4=1;
			scanrow2();							//scan row
			RE1=1;								//clear the 3rd column and set the others
			RE0=1;
			RA5=0;
			RA4=1;
			scanrow3();							//scan row
			RE1=1;								//clear the 4th column and set the others
			RE0=1;
			RA5=1;
			RA4=0;
			scanrow4();							//scan row

			if(password_count==6)
			{
				if(testbit==1)						//test whether password is correct
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("SUCCESS");		//display SUCCESS
          lcd_goto(20);
          send_string("DOOR OPEN");
					led_green=1;					//green light on
					buzzer=1;						//buzzer on
					relay=1;						//relay on
					delay(8000);
					buzzer=0;						//buzzer off
					while(1);
				}
				else
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("ACCESS");			//display ERROR!
          lcd_goto(20);
          send_string("DENIED");
					led_red=1;						//red light on
					buzzer=1;						//buzzer on
					delay(8000);
					buzzer=0;						//buzzer off
					delay(13000);
					buzzer=1;						//buzzer on
					delay(8000);
					buzzer=0;						//buzzer off
					while(1);
				}

			}
		}
}

//	scanning functions
//=========================================================================
void scanrow1(void)
{
	if(RA3==0)							//value '#'
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//to clear the testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}

}

void scanrow2(void)
{
	if(RA3==0)							// value 3
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==2)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA2==0)						// value 2
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==1)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA1==0)						// value 1
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==0)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA0==0)						// value 0
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==2)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
}

void scanrow3(void)
{
	if(RA3==0)							// value 7
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA2==0)						// value 6
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==5)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA1==0)						// value 5
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==4)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA0==0)						// value 4
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==3)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
}

void scanrow4(void)
{

	if(RA1==0)							// value 9
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA0==0)						// value 8
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}

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

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++);

}


please tell me what i need to add.
:(
 
yes.
i know about the EEPROM.
but i don't know how write it in C code.
can u show me please.
I'm stuck at the EEPROM part.
 
i know nothing about the EEPROM.
i see the EEPROM Editor.
It shows :-

0x00 and FF in 16 times.
what does it means.
i use MicroC.
 
i know nothing about the EEPROM.
Look at Chapter 3.0 of your datasheet (Page 33). Both EEPROM and Flash reading and writing are very clearly explained there. There are step by step instructions of how to read and how to write EEPROM. There's even some simple asm code showing what registers to do what with in what order. Dead easy to port to C.
 
ok, this is what i get.
can anyne please tell me whats wrong.
i'm out of idea n i'm stuck.

Code:
//	include
//==========================================================================
//#include <p16F877A.h>

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

//	define
//==========================================================================
#define	rs			    portc.f0 //RC0
#define	e			      portc.f1 //RC1
#define	led_red		  portc.f2 //RC2
#define led_green	  portc.f3 //RC3
#define	lcd_data	  PORTD
#define relay		    portb.f1 //RB1
#define buzzer		  portb.f2 //RB2
#define RE0         porte.f0
#define RE1         porte.f1
#define RA5         porta.f5
#define RA4         porta.f4
#define RA3         porta.f3
#define RA2         porta.f2
#define RA1         porta.f1
#define RA0         porta.f0


#define EEPROM_INSTRUCTION_WREN 0x06
#define EEPROM_INSTRUCTION_WRDI 0x04
#define EEPROM_INSTRUCTION_RDSR 0x05
#define EEPROM_INSTRUCTION_WRSR 0x01
#define EEPROM_INSTRUCTION_READ 0x03
#define EEPROM_INSTRUCTION_WRITE 0x02
#define EEPROM_DEVICE 0x03
#define MY_FIRST_VALUE (EEPROM_SYSTEM_BASE - 4)
#define MY_SECOND_VALUE (MY_FIRST_VALUE - 8)
#define MY_STORED_VALUE (EEPROM_SYSTEM_BASE - 4)

//	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 scanrow1(void);
void scanrow2(void);
void scanrow3(void);
void scanrow4(void);

//	global variable
//==========================================================================
unsigned char step=0;
unsigned char lcd_count=0;
unsigned char password_count=0;
unsigned char password_wrong=0;
unsigned char sequence=0;
unsigned char testbit=0x01;
unsigned char init;

//	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=1;
	PORTD=0;
	relay=0;
	buzzer=0;
	led_green=0;
	led_red=0;
	init=0;
	sequence=0;

	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();
	delay(1000);
	lcd_goto(0);							//initial display
	send_string("PLEASE ENTER");
	lcd_goto(20);
	send_string("6-DIGIT PASSWORD");


	while(1)
	{
												//keypad scanning
			RE1=0;								//clear the 1st column and set the others
			RE0=1;
			RA5=1;
			RA4=1;
			scanrow1();							//scan row
			RE1=1;								//clear the 2nd column and set the others
			RE0=0;
			RA5=1;
			RA4=1;
			scanrow2();							//scan row
			RE1=1;								//clear the 3rd column and set the others
			RE0=1;
			RA5=0;
			RA4=1;
			scanrow3();							//scan row
			RE1=1;								//clear the 4th column and set the others
			RE0=1;
			RA5=1;
			RA4=0;
			scanrow4();							//scan row

			if(password_count==6)
			{
				if(testbit==1)						//test whether password is correct
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("SUCCESS");		//display SUCCESS
          lcd_goto(20);
          send_string("DOOR OPEN");
					led_green=1;					//green light on
					buzzer=1;						//buzzer on
					relay=1;						//relay on
					delay(8000);
					buzzer=0;						//buzzer off
					while(1);
				}
				else
				{
					lcd_clr();						//clear lcd
					lcd_goto(0);
					send_string("ACCESS");			//display ERROR!
          lcd_goto(20);
          send_string("DENIED");
					led_red=1;						//red light on
					buzzer=1;						//buzzer on
					delay(8000);
					buzzer=0;						//buzzer off
					delay(13000);
					buzzer=1;						//buzzer on
					delay(8000);
					buzzer=0;						//buzzer off
					while(1);
				}

			}
		}
}

//	scanning functions
//=========================================================================
void scanrow1(void)
{
	if(RA3==0)							//value '#'
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//to clear the testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}

}

void scanrow2(void)
{
	if(RA3==0)							// value 3
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==2)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA2==0)						// value 2
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==1)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA1==0)						// value 1
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==0)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA0==0)						// value 0
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==2)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
}

void scanrow3(void)
{
	if(RA3==0)							// value 7
	{
		while(RA3==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA2==0)						// value 6
	{
		while(RA2==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==5)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA1==0)						// value 5
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==4)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA0==0)						// value 4
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		if(sequence==3)					//if the sequence of password is correct
		{
			testbit=testbit&0x01;		//maintain testbit as 1
			sequence+=1;				//increase sequence by 1
		}
		else testbit=testbit&0x00;		//else clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
}

void scanrow4(void)
{

	if(RA1==0)							// value 9
	{
		while(RA1==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}
	else if(RA0==0)						// value 8
	{
		while(RA0==0)continue;			//waiting the key to be released
		if(init==0)lcd_clr();			//if init is clear, clear the lcd
		lcd_goto(lcd_count);
		send_char(0x2A);				//send symbol '*'
		testbit=testbit&0x00;			//clear testbit
		lcd_count+=1;					//increase lcd_count by 1
		password_count+=1;				//increase password_count by 1
		init=1;							//set init
	}

}

{
static int  Eeprom_Start( void );
static int  Eeprom_Stop( void );

static void Eeprom_WriteEnable( void );
 //static void Eeprom_WriteDisable( void );

 static void Eeprom_Ready( void );

static int Eeprom_users;
}

int Eeprom_SetActive( int state )
{
if ( state )
return Eeprom_Start();
else
return Eeprom_Stop();
}

 /**
  Read the active state of the EEPROM.
  @return State - 1/non-zero (on) or 0 (off).
*/
 int Eeprom_GetActive( )
{
return Eeprom_users > 0;

int my_number_to_store = 1234
int size = 4 // ints are 4
if(Eeprom_Write( MY_STORED_VALUE, (uchar*)&my_number_to_store, size ); < 0)
{
    // then there was an error...
   }
  else
{
     // my_stored_number now has the stored value
  }
  
  int Eeprom_Write( int address, uchar* buffer, int count )
 {
if ( address < 0 || address >= EEPROM_SIZE )
return CONTROLLER_ERROR_BAD_ADDRESS;

if ( Eeprom_users == 0 )
{
int status = Eeprom_Start();
if ( status != CONTROLLER_OK )
return status;
}
Spi_Lock();

Eeprom_Ready( );

Eeprom_WriteEnable();

uchar c[ count + 4 ];

c[ 0 ] = EEPROM_INSTRUCTION_WRITE;
c[ 1 ] = (unsigned char)( address >> 8 );
c[ 2 ] = (unsigned char)( address & 0xFF );
c[ 3 ] = 0;

int i;
for ( i = 0; i < count; i++ )
{
c[ i + 3 ] = buffer[ i ];
}

Spi_ReadWriteBlock( EEPROM_DEVICE, c, 3 + count );

Spi_Unlock();

return CONTROLLER_OK;
}
  
   int my_stored_number;
int size = 4; // ints are 4
if(Eeprom_Read( MY_STORED_VALUE, (uchar*)&my_stored_number, size ) < 0)
{
 // then there was an error...
}
 else
 {
 // my_stored_number now has the stored value
 }


int Eeprom_Read( int address, uchar* buffer, int count )
{
if ( address < 0 || address > EEPROM_SIZE )
return CONTROLLER_ERROR_BAD_ADDRESS;
if ( Eeprom_users == 0 )
{
 int status = Eeprom_Start();
if ( status != CONTROLLER_OK )
return status;
}

Spi_Lock();

 Eeprom_Ready( );

 unsigned char c[ count + 4 ];

 c[ 0 ] = EEPROM_INSTRUCTION_READ;
 c[ 1 ] = (unsigned char)( address >> 8 );
c[ 2 ] = (unsigned char)( address & 0xFF );
c[ 3 ] = 0;

Spi_ReadWriteBlock( EEPROM_DEVICE, c, count + 3 );

 int i;
 for ( i = 0; i < count; i++ )
{
buffer[ i ] = c[ i + 3 ];
 }

Spi_Unlock();

return CONTROLLER_OK;
}

int Eeprom_Start()
{
int status;

if ( Eeprom_users++ == 0 )
{

status = Spi_Start( EEPROM_DEVICE );
if ( status != CONTROLLER_OK )
{
Eeprom_users--;
return status;
}


status = Spi_Configure( EEPROM_DEVICE, 8, 4, 0, 1 );
   if ( status != CONTROLLER_OK )
   {

      Eeprom_Stop();
      return status;
    }
   }

  return CONTROLLER_OK;
 }

 int Eeprom_Stop()
 {
  if ( Eeprom_users <= 0 )
   return CONTROLLER_ERROR_NOT_LOCKED;

  if ( --Eeprom_users == 0 )
    Spi_Stop( EEPROM_DEVICE );

   return CONTROLLER_OK;
 }
void Eeprom_WriteEnable()
{
  uchar c;
 c = EEPROM_INSTRUCTION_WREN;

  Spi_ReadWriteBlock( EEPROM_DEVICE, &c, 1 );
 }


 void Eeprom_Ready( void )
 {
  int status;
 do
{
unsigned char c[ 2 ];
c[ 0 ] = EEPROM_INSTRUCTION_RDSR;
c[ 1 ] = 0;

Spi_ReadWriteBlock( EEPROM_DEVICE, c, 2 );

    status = c[ 1 ] != 0xFF;

  } while ( !status );
}

  

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

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++);

}
 
Door locking system using pic16F877A

I have a project to design door locking system using PIC16F877A. Please anyone can help me by sending circuit diagram for the system. Pleas!!!!
 
Please can you send me circuit diagram of your project. I also have to do same kind of project that you did. Please!!!! help me.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top