2021-01-19 02:54 AM
HI, i want to read and write values to EEPROM 24C16 IC using stm32f103 . I am able to write the value but i am not able to read the values . And how can i check whether i have written value to my eeprom ic and how to read value. can anyone help me with it please.
Thank you
2021-01-19 03:48 AM
Hello
uint8_t buf[16];
HAL_I2C_Mem_Read(&hi2c1, 0xA0, 0x00, 1, buf, 16, 100);// 24c16 read the first 16 bytes from page 0
2021-01-19 01:30 PM
Note that 24C16 has several consecutive addresses on I2C bus (each address selects one 256 byte block).
Also the starting address within a block and size for writing has limitations, see the datasheet.
2021-01-19 09:01 PM
void write_eeprom_reg(uint16_t reg_addr, uint8_t value)
{
//uint8_t d[3];
d[0]=(reg_addr>>8) & 0xFF;
d[1]=(reg_addr) & 0xFF;
d[2]=value;
while(HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t)(0x50<<1),3,100)!=HAL_OK)
{
}
if(HAL_I2C_Master_Transmit(&hi2c1,(uint16_t)(0x50<<1),(uint8_t*)d,sizeof(d),1000)!=HAL_OK)
{
Error_Handler();
}
else
{
printf("Master transmitt Successfull\r\n");
}
}
uint8_t read_eeprom_reg(uint16_t addr)
{
uint8_t buffer[]={0x05};
//uint8_t buffer=addr;
uint8_t value;
while(HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t)(0x50<<1),3,100)!=HAL_OK)
{
}
while(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(0x50<<1), (uint8_t*)&buffer, 2, 100)!=HAL_OK)
{
if(HAL_I2C_GetError(&hi2c1)!=HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
while(HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t)(0x50<<1),3,100)!=HAL_OK)
{
}
while(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(0x50<<1),(uint8_t*)&value,1,100)!=HAL_OK)
{
if(HAL_I2C_GetError(&hi2c1)!=HAL_I2C_ERROR_AF)
{
Error_Handler();
}
return value;
}
}
This is the code i have done can tou say me the error what i have done and what i need to add to it.
Thank you
2021-01-19 09:01 PM
void write_eeprom_reg(uint16_t reg_addr, uint8_t value)
{
//uint8_t d[3];
d[0]=(reg_addr>>8) & 0xFF;
d[1]=(reg_addr) & 0xFF;
d[2]=value;
while(HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t)(0x50<<1),3,100)!=HAL_OK)
{
}
if(HAL_I2C_Master_Transmit(&hi2c1,(uint16_t)(0x50<<1),(uint8_t*)d,sizeof(d),1000)!=HAL_OK)
{
Error_Handler();
}
else
{
printf("Master transmitt Successfull\r\n");
}
}
uint8_t read_eeprom_reg(uint16_t addr)
{
uint8_t buffer[]={0x05};
//uint8_t buffer=addr;
uint8_t value;
while(HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t)(0x50<<1),3,100)!=HAL_OK)
{
}
while(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(0x50<<1), (uint8_t*)&buffer, 2, 100)!=HAL_OK)
{
if(HAL_I2C_GetError(&hi2c1)!=HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
while(HAL_I2C_IsDeviceReady(&hi2c1,(uint16_t)(0x50<<1),3,100)!=HAL_OK)
{
}
while(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(0x50<<1),(uint8_t*)&value,1,100)!=HAL_OK)
{
if(HAL_I2C_GetError(&hi2c1)!=HAL_I2C_ERROR_AF)
{
Error_Handler();
}
return value;
}
}
This is the code i have done can tou say me the error what i have done and what i need to add to it.
Thank you
2021-01-24 01:35 AM
Hello
You can't just transmit 3 bytes to write a memmory like 2416
2416 requires (for random write ) between d[1] and d[2] , master to transmit a stop condition.
HAL_I2C_Master_Transmit does not transmit STOPs between bytes but only at the end.
Use HAL_I2C_Mem_Write instead of HAL_I2C_Master_Transmit function
2021-01-24 01:54 AM
Too many errors, this code is hopeless.. Please do what Vangelis Fortounas advices.
-- pa