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.

SPI connection between microcontroller and EEPROM

Status
Not open for further replies.

ECG eng.

Junior Member level 1
Joined
Oct 5, 2010
Messages
17
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,395
hello my frnds help is needed...

i am using 16f690 microcontroller and would like to connect it with an External EEPROM using the SPI connection, i have written the program but it is not working i dnt knw what is the problem or hw can i fix it ...

i have done all what the data sheet say's regarding the SPI connection ..
my EEPROM is 25LC640

pleas if any one have tried or knw anything help .. thnx in advance:wink:
 

you should always post code AND a schematic. You never know if you missed something - somebody else may discover an error you didn't know about!
 

sorry for not posting the code
here is the code , at the end i want to test if it has saved in the External EEPROM by reading it and puting the data in the Internal EEPROM

Code:
#include<pic.h>
__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);
unsigned short int x=0x00,z=0x00;

void checkstatus(void)
{	
	RC6=0;				// chip select 
/*	if (x==0x82)
	{	
		z=SSPBUF;		// READ WHAT'S IN BUFFER TO CLEAR
		WCOL=0;
		SSPBUF=0X01;	//
		while (!BF) continue;
		z=SSPBUF;
		WCOL=0;
		SSPBUF=0x02;	// 
		while (!BF) continue;
	}
*/
	z=SSPBUF;			// read and clear the sspbuf
	WCOL=0;				// check the WCOl 
	SSPBUF=0X05;		//RDSR Read STATUS register
	while (!BF) continue;	// check 
	z=SSPBUF;			// READ WHAT'S IN BUFFER
	while (z==0x03)
	continue;
	
return;

}
               
void main (void)
{
	TRISC7=0; 			 // SERIAL OUTPUT
	TRISC6=0;			// SLAVE SELECT chip select 
	TRISC5=0;			// WRITE PROTECT 
	TRISC4=0;			// HOLD CONTROL
	TRISB4=0;			// SERIAL INPUT
	TRISB6=0;			// SERIAL CLOCK OUTPUT
	ANSEL=0;			//digital input output
	ANSELH=0;			//
	SSPEN=1;			// ENABLE SSP
	SMP=1;				//INPUT DATA SAMPLED AT END AT DATA OUTPUT TIME
	CKE=0;				// DATA TRANSMISSION ON rising EDGE
	CKP=1;				// IDLE STATE FOR CLOCK IS A HIGH LEVEL
	RC4=1;				// HOLD DISABLE
	RC5=1;				// WP DIABLED
	
// start enabling the write process
	SSPIE=0;			// SSP INTERRUP[T ENABLE BIT 
	RC6=1;

// intializing writing
send:
	RC6=0;				// the chip select 
	z=SSPBUF;			// READ WHAT IN BUFFER TO CLEAR IT
	;
	WCOL=0;				// SET COLLISION BIT TO ZERO
	SSPBUF=0X06;		// SEND WRITE ENABLE INSTRUCTION TO THE EEPROM
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE	
	RC6=1;				// TURN OFF THE CHIP (NECCESSARY TO START WRITING
	;
	;
	;
	
/*		RC6=0;			// TURN THE CHIP ON AGAIN TO TRANSMIT THE DATA
		z=SSPBUF;		// READ WHAT'S IN BUFFER TO CLEAR
		WCOL=0;
		SSPBUF=0X05;	//
		while (!BF) continue;
		z=SSPBUF;
		WCOL=0;
		if (SSPBUF!=0x02)
		{	RC6=1;
			goto send;
		}
*/
	RC6=0;
	z=SSPBUF;			// READ WHAT'S IN BUFFER TO CLEAR
	;
	WCOL=0;				// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION
	SSPBUF=0X02;		// INSTRUCTION TO WRITE 
	while (!BF) continue;

	z=SSPBUF;			// READ WHAT'S IN BUFFER TO CLEAR
	;
	WCOL=0;				// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION
	SSPBUF=0X00;		// SEND ADDRESS
	while (!BF) continue;

	z=SSPBUF;			// clear and read the buffer 
	;
	WCOL=0;	
	SSPBUF=0X55;		// SEND ADDRESS
	while (!BF) continue;

	z=SSPBUF;			// READ WHAT'S IN BUFFER TO CLEAR
	;
	WCOL=0;				// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION

//SEND DATA 
	SSPBUF=0XAA;		// INSTRUCTION TO WRITE 
	while (!BF) continue;
	checkstatus();		//check the statuse of the EEPROM
//z=SSPBUF;				// READ WHAT'S IN BUFFER TO CLEAR
//	WCOL=0;				// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION
	RC6=1;

RECIEVE:
	TRISB4=1;			//
		;
		;
		;	
		;
		RC6=0;			// TURN CHIP SELECT ON
		z=SSPBUF;		// READ WHAT'S IN BUFFER TO CLEAR
		;
		WCOL=0;			// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION
	
		SSPBUF=0X03;	// READ INSTRUCTION TO THE EEPROM	
		while (!BF) continue;
		
//checkstatus();		//check the statuse of the EEPROM
		z=SSPBUF;		// READ WHAT'S IN BUFFER TO CLEAR
		;
		WCOL=0;			// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION
		SSPBUF=0X00;	// ADDRESSTO READ
		while (!BF) continue;
	
		z=SSPBUF;
		;
		WCOL=0;			// SET COLLISION BIT TO ZERO TO PREPARE FOR THE NEXT TRANSMISSION
		SSPBUF=0X55;	// ADDRESS TO READ
		while (!BF) continue;
	
// checkstatus();		//check the statuse of the EEPROM
		x=SSPBUF;		// READ WHAT'S IN BUFFER
		RC6=1;		
stophere:	
	EEADR=0X00;

// save in the EEPROM 
	
	EECON1=0X04;
	EEDAT=x;
	GIE=0;
	EECON2=0X55;
	EECON2=0XAA;
	WR=1;
	GIE=1;
	while(WR) continue;
	TRISC=0X00;
 	while(1)
	{PORTC=0X01;
	 continue;
	}
}
i am gussing that the problem is with timing but i dnt knw hw to fixed.
thnx
 
Last edited:

i have fxd the reading and it's working fine with a small problem that the address 2B is FF even though that data in it is not FF...
but i am still cant fix the writting and dnt knw wt to do ?!
plz give me hints if u knw coz i am time limited currently
 

Thnx :)

but realy the problem I have in writing to the EEPROM itself not the communication
it appears that the status register won't be cleared
when i set the write enable it is enabled but the write protection is on and the two block protect bits is also on

after writing 0x00 to the status register I get 0x8C (WPEN X X X BP1 BP0 WEL WIP)
and after sending the write instruction I get 8Eh

*before writing to the status register I should also send the write instruction
 

Just an idea

I'm not familiar with your specific compiler but as a general rule for eeprom
you need a significant delay between writes/reads.
Something in the order of 25mS perhaps as a rule of thumb while testing
depending on the specs for your EEPROM. (Check the datasheet)

It may be worth experimenting with various delay times to see if it helps.
Dont quote me but I'm not sure a test for changed data would be sufficient
for an eeprom to 'settle' and reset registers etc.

jack
 

yes...
I had tried many delays between reading and writing and between any possible changes that needs delay
but the problem when first time erased the EEPROM and program it with some certain numbers using the programmer
I was able to write to the EEPROM only once
and then when I try to write to it again it does not
and the Status register would not be cleared which means the write protection is always on
although the write protection pin is disabled
 

Sorry if this is all tried but all I can come up with is:

Are you sure you are framing the SPI commands right
Is your micro configured correctly
Are the SPI timings from the micro within the EEPROMs spec
Look for bad connections
failing all that - have you tried another chip?

jack
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top