2019-09-08 11:12 AM
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.
Solved! Go to Solution.
2019-09-08 11:31 AM
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?
2019-09-08 11:31 AM
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?
2019-09-08 12:26 PM
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
2019-09-08 02:04 PM
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);
2019-09-08 03:05 PM
This was because of the delay Mr. Clive mentioned.
Thanks guys.
2019-09-08 05:40 PM
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:
Therefore use the functions Clive suggested!
2019-09-09 10:55 AM
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.
2019-09-09 11:03 AM
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?
2019-09-17 05:08 PM
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?