Skip to main content
Lchal.1
Associate III
January 19, 2021
Question

How to read and write values to EEPROM IC (24C16 IC)

  • January 19, 2021
  • 2 replies
  • 2583 views

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

This topic has been closed for replies.

2 replies

Vangelis Fortounas
Associate II
January 19, 2021

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

Lchal.1
Lchal.1Author
Associate III
January 20, 2021

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

Vangelis Fortounas
Associate II
January 24, 2021

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

Pavel A.
Super User
January 19, 2021

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.

Lchal.1
Lchal.1Author
Associate III
January 20, 2021

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

Pavel A.
Super User
January 24, 2021

Too many errors, this code is hopeless.. Please do what Vangelis Fortounas advices.

-- pa