cancel
Showing results for 
Search instead for 
Did you mean: 

To read and write EEPROM IC 24c16

MS.11
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

10 REPLIES 10

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.)

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

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

MS.11
Associate III

okay

Hi,

As u told I used HAL_I2C_Mem_Read & HAL_I2C_Mem_Write in my function.

char buffer[3];

char d[3]={7, 5, 10}; 

these are the values given for d

at24_HAL_WriteBytes(&hi2c1, 0xA0, 0, &d, 3); //function call for write

at24_HAL_ReadBytes(&hi2c1, 0xA0, 1, buffer, 3); //function call for read

The values what I gave for 'd' is displaying on the live expression but that values are not storing in the buffer. If the values stored in the buffer the read operation is going to complete. Plz Can u help me what mistake I am doing.

Thank you

Hello

This is a working example made for this purpose.

Dont forget the pullup resisrors.

Here is the code

//#############################################################

 uint8_t W_buf[6]={1,2,3,4,5,6};// define the write buffer

 uint8_t R_buf[6];// define the read buffer

//This is the write function

 if( HAL_I2C_Mem_Write(&hi2c1, 0xA0, 0x0000, 2, W_buf, 6, 100)!=HAL_OK)// 24c16 random rwrite at address 0.

 {

   ;// possible error will trapped here

 }

 //this is the read function

 if( HAL_I2C_Mem_Read(&hi2c1, 0xA0, 0x0000, 2, R_buf, 6, 100)!=HAL_OK)// 24c16 read at address 0.

 {

   ;// possible error will trapped here

 }

//#############################################################

0693W000007CXCMQA4.jpg

MS.11
Associate III

Thank you soo much.

MS.11
Associate III

Hi,

Thank you sir. I tried as u told it is working but in R_buf the values are not storing. I don't know what to do. Plz help me sir.0693W000007CnDVQA0.jpg

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