I am doing I2C project with MPU6050 using STM32F4DISCOVERY board. The ADDR bit in SR1 register is clearing in one instruction execution without reading SR2 register. Because of clearing ADDR bit, I can't do acknowledge disable is made during EV6 in single byte receiving case.
The datasheet states:
ADDR: Address sent (master mode)/matched (slave mode) This bit is cleared by software reading SR1 register followed reading SR2.
In case a single byte has to be received, the Acknowledge disable is made during EV6 (before ADDR flag is cleared) and the STOP condition generation is made after EV6.
If I write any instruction (or declaration) between I2C->DR=address|0
and while(!(I2C2->SR1 & (1<<1)))
in below code, then ADDR flag is cleared without reading the SR2 register (I2C2->SR2
).
if(direction==i2c_transmitter_mode)
{
I2C2->DR = address | 0;//SEND ADDRESS WITH ZERO LAST BIT
while(!(I2C2->SR1 & (1<<1)))
{
if(--i2c1_timeout==00)
{
return 1;
}
}
I2C2->SR2;
return 0;
}
How can I fix this problem?