# Why is the ADDR flag cleared without reading the SR2 register?

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:

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

while(!(I2C2->SR1 & (1<<1)))
{
if(--i2c1_timeout==00)
{
return 1;
}
}
I2C2->SR2;
return 0;

}


How can I fix this problem?

You can fix it by using the correct bit. ADDR bit is not 1<<7 but 1<<1.

• Sir,you are right.But the waiting loop is being continuing with ADDR. Both ADDR AND TXE are set after transmission of address.But ADDR is reset immediately after set .so,i am using TXE bit for waiting and checking AF flag.I am getting WHO_AM_I register value of MPU6050.But problem is ADDR – naresh Oct 19 '19 at 5:31
• much more issues in those 5 lines of code OP has :) – P__J__ Oct 19 '19 at 12:07
1. Do not use magic numbers like 1<<1 1<<7 on those complex uCs. Use human readable definitions.

    I2C1 -> CR1 |= I2C_CR1_START;
while(!(I2C1 -> SR1 & I2C_SR1_SB ));
(void)I2C1 -> SR2;


it is a very simple version - you should add timeouts, error checking, address ACK checking etc. But it shows the general idea.

1. I2C2->DR = address | 0; - it does not clear the last bit. Actually it does nothing. My advice - read about bitwise operators

to clear the particular bit:

address &= ~(1 << bitnumber);


or for the bit zero

address &= ~(1 << 0);

why I did not write address &= 0xfffffffe; - because it less understandable for humans. If you see the code with ~ you know that someone is zeroing some bits.