[SOLVED] Error reading from EEPROM

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
Activity points
1,571
dsPIC30F5011
24LC256(EEPROM)
MPLAB V8.8

Hi ,

I have written to read and write from EEPROM.
But when I am writing and integer to it I am not able to read it back exactly !
Help Required !




Code:
char HLVLineBuf[20];
static unsigned char *ptr;
unsigned int temp_HV = 0, temp_LV = 0;
unsigned int Voltage_Low,Voltage_High;
....
ptr = HLVLineBuf;
....
    temp_HV =  ConvertSTRING_Int(&HLVLineBuf[0],3);
    temp_LV =  ConvertSTRING_Int(&HLVLineBuf[3],3);
    printf("\n\rtemp_HV_CUT = %d", temp_HV);
    printf("\n\rtemp_LV_CUT = %d", temp_LV);
    Voltage_High = temp_HV;
    Voltage_Low  = temp_LV;
    ptr = (unsigned char*)&Voltage_High;
    Write_String_EEPROMx(2, ptr, EE_CUTOFF_HIGH);				// write default high voltage cutoff value
    ptr = (unsigned char*)&Voltage_Low ; 
    Write_String_EEPROMx(2, ptr, EE_CUTOFF_LOW);				// write default low voltage cutoff value
				
    Read_String_EEPROMx(2,ptr,EE_CUTOFF_HIGH);
    printf("\n EE_HV_CUT = %s", ptr);
    Read_String_EEPROMx(2,ptr,EE_CUTOFF_LOW);
    printf("\n EE_LV_CUT = %s", ptr);



Code:
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)
{
	
	if(length > 64)
		return 0;	
	WaitI2C(10000);							// bus free time 4.7 ms is required for every new transmission
	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;

}	

unsigned int ConvertSTRING_Int(unsigned char *ptr, unsigned char noofbytes)
{
	unsigned int c = 0,i = 1;
	while(--noofbytes)
		i = i*10;
	while(*ptr)
	{
		c = c + i*(*ptr++ - 48);
		i = i/10;
	}
	return c;
}

I tried to print the string , and I expect that to be 222 and 111 respectively. Once I get the exact one I can convert back to integers.
 
Last edited:

Couple of thing you need to do -

first of all check your HLVLineBuf contents I think some of them are not integer and when you are doing -48 will give you different value if it is not integer...... If I see your blog also your are counting for first three bytes ..... then I think you need to pass 0 to 2 indexing

Try this in your code to get an idea....

Code:
noofbytes1 = noofbytes
while(noofbytes1)
{
		i = i*10;
                noofbytes1 = noofbytes1 -1
}
 

They are not integers, it reads values from KEYPAD and its a char , so i m trying to convert to int and then type casting and then storing. but while retrieval I am not able see to the same. where could I have been wrong.

your bit of code is the same loop with an extra variable, if I am not wrong.
 

Try linke this for EEPROM read and write functions.


Code C - [expand]
1
Write_String_EEPROMx(2, &ptr, EE_CUTOFF_LOW);




Edit: The above is wrong.

This is also incorrect. Voltage_High is 16 bits wide and ptr is a pointer to an unsigned char which is 8 bit wide. You are assigning the address of Voltage_High to ptr after typecasting. ptr contains the address of first byte of Voltage_High.


Code C - [expand]
1
ptr = (unsigned char*)&Voltage_High;



ptr has to be de-referenced with typecasting while reading from or writing to eeprom location.
 
Last edited:


Hey there !!

Your posted code 'snippets' are wayyyy too long for me to decipher.
Could you just pull out the lines where your data of concern is being manipulated ?

It's most likely a problem of type conversion. But lets have a look first.... in a more condensed format.

 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…