cancel
Showing results for 
Search instead for 
Did you mean: 

How to read and write to eeprom ic (24c16) using i2c in stm32f103

Lchal.1
Associate II
  • hi, i have to read and write to eeprom ic . i am able to write but i am not able to read from my ic .
  • 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 anyone say me the error what i have done and what i need to add to it so that i can write to my eeprom ic and read from eeprom ic.

Thank you

1 REPLY 1
Pavel A.
Evangelist III