cancel
Showing results for 
Search instead for 
Did you mean: 

HAL I2C and 24c02 eeprom communication problem

Arman Ilmak
Senior

Hi.

I read 24c02 datasheet and tried to write my code to write and read a byte from this device. But my code does not work. Can you tell me what the problem is?

https://imghostr.com/M4hXKXT6A

void EEPROM_Write(unsigned char address,unsigned char data)
{
 
	buff[0]=address;
        buff[1]=data;
        HAL_I2C_Master_Transmit(&hi2c1,0x50<<1,buff,2,10);	
	delay_ms(10);
}

https://imghostr.com/fTqONyMAW

unsigned char EEPROM_Read(unsigned char address)
{
	buff[0]=0;
	HAL_I2C_Master_Transmit(&hi2c1,0x50<<1,buff,1,10);
	HAL_I2C_Master_Receive(&hi2c1,0x50<<1|1,&buff[1],1,10);
	deay_ms(10);
	return buff[1];
	}		

I put the image of datasheet and my code above.

1 ACCEPTED SOLUTION

Accepted Solutions

Perhaps lose the delay, and make sure the timeout is long enough. Watch for return errors.

Unfortunately "does not work", with a diagram of the signalling you're supposed to be using tells me nothing.

What does the signal look like on the wire? Get a scope on the thing.

What data do you see?

What errors do you get?

What speed are you clocking the bus at?

What STM32?

Do you have the interface wired properly, with pull-up resistors?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

8 REPLIES 8

Perhaps lose the delay, and make sure the timeout is long enough. Watch for return errors.

Unfortunately "does not work", with a diagram of the signalling you're supposed to be using tells me nothing.

What does the signal look like on the wire? Get a scope on the thing.

What data do you see?

What errors do you get?

What speed are you clocking the bus at?

What STM32?

Do you have the interface wired properly, with pull-up resistors?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

Put a scope and check for ACKnowledge bit. Dig in the HAL to know if you need to put 0x50 or 0x50<<1

If the function is a write or read, do you need the |1 ?

Normally you need a restart between the write and read, and I guess here you'll have a Start xxxxxx stop start xxxxxx stop

Try the memory forms

 status = HAL_I2C_Mem_Write(&I2CHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 100);

 status = HAL_I2C_Mem_Read(&I2CHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 100);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Arman Ilmak
Senior

This was because of the delay Mr. Clive mentioned.

Thanks guys.

Piranha
Chief II
HAL_I2C_Master_Transmit(&hi2c1,0x50<<1,buff,1,10);
HAL_I2C_Master_Receive(&hi2c1,0x50<<1|1,&buff[1],1,10);

This generates STOP condition in between the transfers, which does not correspond to:

  1. I2C specification
  2. 24C02 datasheet
  3. https://imghostr.com/fTqONyMAW Look into it carefully - there is no STOP between second ACK and second (repeated) START.

Therefore use the functions Clive suggested!

Im trying to use mem erite and read function but they dont work and i dont know why.

unsigned char EEPROM_Read(unsigned char address)
{
 
 Statusr=HAL_I2C_Mem_Read(&hi2c1,0x50<<1,address,0xFF,&buff[1],1,50);
delay_ms(20);
 return buff[1];
	}		

In read operation the function returns HAL_TIMEOUT.

void EEPROM_Write(unsigned char address,unsigned char data)
{
  Statusw=HAL_I2C_Mem_Write(&hi2c1,0x50<<1,address,0xFF,&data,1,50);
  delay_ms(20);
}

And in write operation the function returns HAL_ERROR.

I saw in a youtube tutorial that he just set MemAddSize to 0xFF and his code was working. And after I took a glance at what clive sent last night I saw that he wrote I2C_MEMADD_SIZE_8BIT,so I did this and the problem solved. But how is it possible that the youtuber code was working?

https://www.youtube.com/watch?v=qAarZ5tho9g

Arman Ilmak
Senior

Hi again.

Im still struggling with my stm32 uart:face_without_mouth:.I can do something to work with this problem and finish this project,but my curious mind wont let me till i find the problem.

I need to have an interrupt after every one byte of data that has been received, so as I read i think using DMA is not a good idea. So i decided to use interrupt to do this , but i faced a problem:

When I transfer the command to esp8266 it receives the command and starts to send me back the data,if the size of its data be more than the buffer size i put in hal_uart_receive,the uart receive wont work anymore but if the data be just the same as buffer the uart will keep on working. So what is the solution?