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 !
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.
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: