Skip to main content
MS.11
Associate II
January 24, 2021
Solved

To read and write EEPROM IC 24c16

  • January 24, 2021
  • 1 reply
  • 4539 views

Is it possible to set I2C SCL and SDA pins to high and low in stm32 to read and write the EEPROM IC. If it is possible plz help me how to do that and how to generate the function to read and write the EEPROM 24c16 IC in stm32.Plz help me.

Thank you

This topic has been closed for replies.
Best answer by Vangelis Fortounas

Hello

It seems that Read operation or write are failed.

probably by some harware issue.

Note that pullup resistors must connected to SCL and SDA lines.

WP pin must grounded

1 reply

Vangelis Fortounas
Associate II
January 24, 2021

Hello

HAL_I2C_Mem_Read(...) and HAL_I2C_Mem_Write(...) functions are suitable for this

eg

uint8_t b[6];// define the buffer

HAL_I2C_Mem_Read(&hi2c, 0xA0, 0x00, 1, b, 6, 100);// 24c16 read the first 6 bytes from page 0.

parameters

hi2c is the I2C structure

0xA0 is the slave address for page 0

0x00 is the adress of first byte of current page

1 is the length of the address

b is a pointer to buffer

6 is the length of pointer

100 msec is the timeout for this operation to return.. ( check the return value.)

MS.11
MS.11Author
Associate II
January 25, 2021

Thank you.

uint8_t read_eeprom(uint16_t reg_addr)

{

uint8_t buffer[3]={reg_addr};

buffer[0]=(reg_addr>>8) & 0xFF;

buffer[1]=(reg_addr) & 0xFF;

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*)buf, 1, 100)!=HAL_OK)

{

if(HAL_I2C_GetError(&hi2c1)!=HAL_I2C_ERROR_AF)

{

Error_Handler();

}

}

return buf;

}

I tried like this. Is this wrong or tell me plz what mistake I am doing here

KnarfB
Super User
January 25, 2021

You should read and understand the warnings that you get from the compiler.