cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C problem

sukrubahadirarslan
Associate III
Posted on December 18, 2013 at 19:46

hello.

I want to communicate with sensör with I2C.

First I must send the sensor register address to sensor.

And then I receive the data from this sensor register.

So first I configure I2C peripheral as master transmitter.

Can I change the master transmitter to master receiver without generating stop condition ? 

I mean, I write this code : 

I2C_GenerateSTART(I2C1,ENABLE);

I2C_GetFlagStatus(I2C1,I2C_FLAG_SB);

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_MSL)==RESET);

I2C_Send7bitAddress(I2C1,MPU6050_Default_Address,I2C_Direction_Transmitter);

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TRA)==RESET);

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)==RESET);

I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR); 

 I2C_GetFlagStatus(I2C1,I2C_FLAG_MSL); // read sr2 . clear ADDR bit.

I2C_SendData(I2C1,Gyrox_Data_MSB8_Register_Address);

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE)==RESET);

// Should I generate a STOP condition here ? or Can I resume like this ? :

I2C_GenerateSTART(MPU6050_I2C, ENABLE);

I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Receiver);

...

thanks for your help

2 REPLIES 2
chen
Associate II
Posted on December 19, 2013 at 10:56

Hi

I am not going to debug you code.

''Can I change the master transmitter to master receiver without generating stop condition ?''

Yes.

It is called a 'repeated start condition'.

The full I2C spoecification :

http://www.nxp.com/documents/user_manual/UM10204.pdf

Sadly, it is long winded and goes on about lots of stuff (like ultra high speed) that is not relevant to you.

There are loads of information about I2C on the WEB.

sukrubahadirarslan
Associate III
Posted on December 20, 2013 at 14:36

Thanks a lot. 

this document is very good.

I have one more different question.

I read in datasheet that : I must clearADDR bit after address transmission.

In this code I monitor ADDR bit like this : 

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)==RESET);

When ADDR bit is set , ADDR bit is clear in this line ? or 

Should I write this code like this for clearing the ADDR bit ? 

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)==RESET);

I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR) ----> t

his line is required ???

sorry about my english .

thanks again.