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.

pic16f690 spi communication with 25lc640 problem

Status
Not open for further replies.

ECG.ENG

New Member
hello guyz :)

I had written a program to read and write to the EEPROM through the SPI communication ports
fortunately I was able to read from the eeprom but the problem is that i could not write to it
I've tried the following :
1) put some delays between writing and reading from the EEPROM
2) read the status register on the beginning of the program it was 0x8C and after the write enable it is 0x8E which means that write enable lach is on but the write protection is enabled and the block protection*
3) after initializing the status register t0 0x00 still the value of the register is 0x8C

* status register (WPEN X X X BP1 BP0 WEL WIP)

25lc640 data sheet

Code:
// the last version of EEPOM in C modified on 28-11-2010

#include<pic.h>
__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

// Delay Function
#define _delay_us(x) { unsigned char us; \
	  	       us = (x)/(12000000/4000000)|1; \
		       while(--us != 0) continue; }
void _delay_ms(unsigned int ms)
{
  unsigned char i;
  do {
    i = 4;
    do {
      _delay_us(164);
    } while(--i);
  } while(--ms);
}


void main (void)
{
	unsigned char x=0x00,z=0x00,y=0x00,add=0x01,endadd=0xff;
	
	TRISC7=0;  // SERIAL OUTPUT
	TRISC6=0;	// SLAVE SELECT
	RC6=1;
	TRISB4=1;	// SERIAL INPUT
	TRISB6=0;	// SERIAL CLOCK OUTPUT
	ANSEL=0;	//digital input output
	ANSELH=0;	
	SSPCON=0;
	SSPCON=0X31;
	SSPSTAT=0;
	SSPSTAT=0X80;
	PIE1=0;	// DISABLE PREPHERAL INTERRUPT
	INTCON=0; // DISABLE ALL INTERRUPTS

//--------------------------------------------------------------
// intializing writing to write in the status register 
//--------------------------------------------------------------
	RC6=0;
	;
	;

	SSPBUF=0X06;	// SEND WRITE ENABLE INSTRUCTION TO THE EEPROM
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE	
	z=SSPBUF;
	RC6=1;		// TURN OFF THE CHIP (NECCESSARY TO START WRITING
	;
	;
	_delay_ms(50);

//--------------------------------------------------------------
// intializing the STATUS register
//--------------------------------------------------------------
	
	RC6=0;
	;
	;
	SSPBUF=0X01;	//write to STATUS register instruction (WRSR)
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE
	z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
	SSPBUF=0X00;
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE
	z=SSPBUF;
	RC6=1;
	;
// DELAY FOR THE WRITE CYCLE 5ms
 _delay_ms(50);

//-------------READ STATUS REGISTER TO CHECK---------------	
RC6=0;
	;
	;
	z=SSPBUF;
	SSPBUF=0X05;	//RDSR Read STATUS register
	while (!BF) continue;
	z=SSPBUF;	// READ WHAT'S IN BUFFER
	SSPBUF=0X05;	//RDSR Read STATUS register
	while (!BF) continue;
	z=SSPBUF;	// READ WHAT'S IN BUFFER
// STORE THE STATUS REGISTER IN THE INTERNAL EEPROM
	EEADR=0X00;
 	EECON1=0X04;	
	EEDAT=z;
	GIE=0;
	EECON2=0X55;
	EECON2=0XAA;
	WR=1;
	GIE=1;
	while(WR) continue;

//--------------------------------------------------------------
// intializing writing
//--------------------------------------------------------------
	RC6=0;
	;
	;
	SSPBUF=0X06;	// SEND WRITE ENABLE INSTRUCTION TO THE EEPROM
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE	
	z=SSPBUF;
	RC6=1;		// TURN OFF THE CHIP (NECCESSARY TO START WRITING
	;
	;
//	checkstatus();	//check the statuse of the EEPROM
	_delay_ms(20);
//--------------------------------------------------------------
// intializing the STATUS register (READ)
//--------------------------------------------------------------
polling:
	RC6=0;
	;
	;
	SSPBUF=0X05;
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE
	z=SSPBUF;
	SSPBUF=0X05;
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE
	z=SSPBUF;
	if((z&0x02)!=0X02)
	goto polling;
	RC6=1;
	;
	;
//--------------------------------------------------------------
// intializing the STATUS register (READ)
//--------------------------------------------------------------
	RC6=0;
	;
	;

	SSPBUF=0X05;	//read STATUS register instruction (WRSR)
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE
	z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
	SSPBUF=0X05;
	while (!BF) continue;	//WAIT FOR TRANSMISSION TO COMPLETE
	z=SSPBUF;
	RC6=1;
	;
	;
//______BEGIN WRITING_______

	RC6=0;
	;
	;
	SSPBUF=0X02;	// INSTRUCTION TO WRITE 
	while (!BF) continue;
	z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
	SSPBUF=0X00;	// SEND HIGH ADDRESS
	while (!BF) continue;
	z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
	SSPBUF=0x13;	// SEND LOW ADDRESS
	while (!BF) continue;
	z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
	//SEND DATA 
	SSPBUF=0x48;	// data to write
	while (!BF) continue;
	z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
	RC6=1;
	;
	;
	_delay_ms(20);
//	checkstatus();	//check the statuse of the EEPROM
//--------------------------------------------------------------
// intializing RECIEVING
//--------------------------------------------------------------
		RC6=0;		// TURN CHIP SELECT ON
		;
		;
;
		SSPBUF=0X03;	// READ INSTRUCTION TO THE EEPROM	
		while (!BF) continue;
		z=SSPBUF;	// READ WHAT'S IN BUFFER TO CLEAR
		SSPBUF=0X00;	// ADDRESS high to read 
		while (!BF) continue;
		z=SSPBUF;
		SSPBUF=add;	// ADDRESS low TO READ
		while (!BF) continue;
		z=SSPBUF;

		EEADR=0X01;
 		EECON1=0X04;

Read:
		SSPBUF=add;	// ADDRESS low TO READ
		while (!BF) continue;
		x=SSPBUF;
		add++;
	EEDAT=x;
	GIE=0;
	EECON2=0X55;
	EECON2=0XAA;
	WR=1;
	GIE=1;
	while(WR) continue;
	EEADR++;
	if (add!=endadd) 	
	goto Read;

	RC6=1;
	TRISC=0x00;
while(1)
{
	PORTC=0X01;
	 continue;
	 }
}
 
Last edited:
Make sure all external pins are tied the way they should be, I.e. make sure write protect isnt asserted!

Get back to me once you've checked that
WP should be 0V
 
Status
Not open for further replies.

Latest threads

Back
Top