[SOLVED] Unable read from EEPROM using PIC30F5011

Status
Not open for further replies.

Raady Here

Full Member level 5
Joined
Jun 8, 2013
Messages
242
Helped
26
Reputation
52
Reaction score
26
Trophy points
28
Location
India
Visit site
Activity points
1,571
HI ,

I am trying to read and write to EEPROM 24LC256 using I2C , but when I am trying with a string , the LCD doesn't display any thing.
(I tried other program displaying on LCD, and no problem with LCD functionality.)

Please let me know if I am missing some logic,

I have used library functions of PIC30F5011 in MPLAB 8.8


Code:
	#define value4		0x0004
	#define value5		0x0005
main(){

	unsigned char testarr1[] = "String 1";
	unsigned char testarr2[] = "String 2";
	unsigned char disarr1[9];          // array 1
	unsigned char disarr2[9];          // array 2
        Write_String_EEPROMx(8,testarr1,value4); // writing to EEPROM
	Write_String_EEPROMx(8,testarr2,value5);

        Read_String_EEPROMx(8,disarr1,value4);  // Read from EEPROM
	Read_String_EEPROMx(8,disarr2,value5);

        XLCDCommand(0x94);
	XLCDPutRamString((const char)disarr1); // display on LCD.
	XLCDCommand(0xD4);
	XLCDPutRamString((const char)disarr2);
}


void Write_String_EEPROMx(unsigned char length, unsigned char *arr,unsigned int Address)
{
	EEPROM_WP_Disable;
	WaitI2C(10000);							// Inital delay
	add.value = Address;
	IdleI2C();
	StartI2C();
	IdleI2C();
	MasterWriteI2C(EEPROM_WRITE_ID);       		// Write Control byte
	IdleI2C();
        while(I2CSTATbits.ACKSTAT);
	MasterWriteI2C(add.msb);					// write MSB Address
   	IdleI2C();
        while(I2CSTATbits.ACKSTAT);
	MasterWriteI2C(add.lsb);					// write LSB Address
	IdleI2C();
        while(I2CSTATbits.ACKSTAT);
        while(length--)
	{
		MasterWriteI2C(*arr++);       		// Write data byte
		IdleI2C();
	        while(I2CSTATbits.ACKSTAT);
	}			    					// write string
        while(I2CSTATbits.ACKSTAT);
	StopI2C();
	IdleI2C();
	EEPROM_WP_Enable;
}


unsigned char Read_String_EEPROMx(unsigned int length, unsigned char *arr, unsigned int Address)
{

	WaitI2C(10000);											
	add.value = Address;
	IdleI2C();
	StartI2C();
	IdleI2C();
	MasterWriteI2C(EEPROM_WRITE_ID);       		// Write Control byte
	IdleI2C();
	MasterWriteI2C(add.msb);					// write MSB Address
   	IdleI2C();
	MasterWriteI2C(add.lsb);					// write LSB Address
	IdleI2C();
	RestartI2C();
   	IdleI2C();
	MasterWriteI2C(EEPROM_READ_ID);       		// Read Control byte
	IdleI2C();
	
	while( length > 1 )
   	{
	    *arr++ = MasterReadI2C();
		IdleI2C();
    	Nop();
		AckI2C();
		IdleI2C();
		length--;
		*arr = '\0';
	}	
       *arr++ = MasterReadI2C();
       *arr = '\0';
    
	IdleI2C();
	NotAckI2C();
	IdleI2C();
	StopI2C();
	IdleI2C();
	return 1;
}
 

First question: where are EEPROM_WRITE_ID and EEPROM_READ_ID defined?
Second question: have you used a scope or logic analyzer to determine if the write and/or read are working? ie., is the eeprom sending acks back?

If you do not already have a scope or logic analyzer and you plan on writing code that utilizes communication such as SPI, I2C or RS232, I HIGHLY recommend the Salae USB logic analyzer. (https://www.saleae.com/logic/) It is only $150,is super simple to use and shows you both waveforms and data coming across. It has saved me countless hours of frustration. I do NOT work for Salae - I just use one frequently and when I am working on any code that involves communication I hook it up before I even start testing because I know I am going to need it.
 

Hello,
I think this is a problem in using pointer.
In the function "Write_String_EEPROMx()" you request a pointer as 2nd arg. But you haven't pass a a pointer.
Change,
Code:
Write_String_EEPROMx(8,testarr1,value4)
to this
Code:
Write_String_EEPROMx(8,&testarr1[0],value4)

Use this method in Reading function also.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…