cancel
Showing results for 
Search instead for 
Did you mean: 

mpu6050 returns zero, I2C stm32f103

Ahmadreza.2ahm
Associate

I am working with MPU6050 and STM32f103VE as master. When I wake up the sensor and ask for information it returns 3 bytes of zeroes and the protocol stops working. I only send the communication Part of my code to be more specific:

void I2C_interface(void){

I2C1->CR1 |=I2C_CR1_START; //Generating Start condition

while(!(I2C1->SR1 & I2C_SR1_SB)); //making sure the communication has started

(void)I2C1->SR1;

I2C1->DR=(0x68<<1); //clearing SB and writing MPU address in "writing" mode

while(!(I2C1->SR1 & I2C_SR1_ADDR)){}; //waiting for the address to be sent

(void)I2C1->SR1;

(void)I2C1->SR2; //clearing ADDR bit

I2C1->DR=(0x6B); // addressing MPU internal power management register

while(!(I2C1->SR1 & I2C_SR1_TXE)){}; //waiting for bye to be sent

(void)I2C1->SR1;

I2C1->DR=(0<<6)|(0<<5); // clearing sleep mode bit

while(!(I2C1->SR1 & I2C_SR1_BTF)){};

I2C1->CR1 |=I2C_CR1_STOP; //generating stop condition

while((I2C1->CR1 & I2C_CR1_STOP ));

// **** second part ****

I2C1->CR1 |=I2C_CR1_START; //generating start condintion

while(!(I2C1->SR1 & I2C_SR1_SB)); //making sure the communication has started

(void)I2C1->SR1;

I2C1->DR=(0x68<<1); //sending address in "writing mode"

while(!(I2C1->SR1 & I2C_SR1_ADDR)){}; //waiting for the address to be sent

(void)I2C1->SR1;

(void)I2C1->SR2;//clearing ADDR

I2C1->DR=(0x71);//sending address of internal register

while(!(I2C1->SR1 & I2C_SR1_BTF)){}; //waiting for the address to be sent

(void)I2C1->SR1;

(void)I2C1->DR;

I2C1->CR1 |=I2C_CR1_START; //generating start condition to read data

while(!(I2C1->SR1 & I2C_SR1_SB)); //making sure the communication has started

(void)I2C1->SR1;

I2C1->DR=((0x68<<1)|(0x01)); //sending MPU address this time in "read" mode

while(!(I2C1->SR1 & I2C_SR1_ADDR)){};

(void)I2C1->SR1;

(void)I2C1->SR2;

I2C1->CR1 |=I2C_CR1_ACK; //enabling ack bit

while(!(I2C1->SR1 & I2C_SR1_BTF)){}; //Waiting for MPU to send data of the requested register

I2C1->SR1;

(void)I2C1->DR; //clearing BTF

while((I2C1->SR1 & I2C_SR1_BTF)){};

}

The first part of the code is responsible for clearing register 

0x6B

 in order to wake MPU up. The second part reads the requested register information. And here is what I get from the communication.

0 REPLIES 0