cancel
Showing results for 
Search instead for 
Did you mean: 

how to read values from 24c16 EEPROM IC

MS.11
Associate III

Hi, I am new to stm32 I am not getting how to read the values what I write to the 24c16 EEPROM IC .Plz help me how to read the values by generating the read function in stm32 f103.

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");

}

}

this is the code I have written for write operation but I dont know how to do code for read operation. Plz help me how to do.

Thank you

2 REPLIES 2
KnarfB
Principal III

Use the HAL_I2C_Mem_Read and HAL_I2C_Mem_Write API functions for that.

Thank you. Write function is working for me but in read function I have created one buffer to store the value what I have written the value in the write function. Plz let me what mistake I have done.

uint8_t read_eeprom_reg(uint16_t addr)

{

uint8_t buf[19];

//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*)&buf, 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;

}

}