Mpu6050 corresponds to i2c address _ how to read data

The MPU6050 is actually an I2C device with a lot of registers (but we only use a few), we operate the chip by reading and writing registers. So the first issue is the I2C communication between the STM32 and the MPU6050.

1. Configure STM32 (use I2C1: PB6 - SCL; PB7 - SDA)

1) Clock RCC

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

2) GPIO configuration

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; // Both pins add 4.7K pull-up resistor

GPIO_Init(GPIOB, &GPIO_InitStructure);

3) I2C configuration

Void I2C_ConfiguraTIon(void)

{

I2C_InitTypeDef I2C_InitStructure;

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

I2C_InitStructure.I2C_OwnAddress1 =0xc0; // STM32's own address, not the same as the slave

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

I2C_InitStructure.I2C_ClockSpeed ​​= 100000;

I2C_Init(I2C1, &I2C_InitStructure);

I2C_Cmd(I2C1, ENABLE);

}

At this point, STM32 has been configured, it is not that difficult.

2, initialize the MPU6050

Void MPU6050_IniTIalize () / / initialization process, in fact, is to write 5 registers

{

MPU6050_I2C_ByteWrite(0xd0,0x00, MPU6050_RA_PWR_MGMT_1); // reg107, wake up, 8M internal clock source

MPU6050_I2C_ByteWrite(0xd0,0x07, MPU6050_RA_SMPLRT_DIV); //Use frequency 1000

MPU6050_I2C_ByteWrite(0xd0,0x06, MPU6050_RA_CONFIG);

MPU6050_I2C_ByteWrite(0xd0,0x01,MPU6050_RA_ACCEL_CONFIG); //Acceleration range 2g

MPU6050_I2C_ByteWrite(0xd0,0x18, MPU6050_RA_GYRO_CONFIG); //Angle velocity range 2000 degrees/s

}

Note: 0xD0 indicates the address of the MPU6050. We know that the I2C slave (here the MPU6050, of course) has an 8-bit address, the first 7 bits are determined by WHO AM I, and the 8th bit is determined by the level of AD0. The default value of WHO AM I is 0x68H (1101000B), and AD0 is connected low, so the I2C address of the MPU6050 is 0xD0H (11010000B).

3, I2C core program (read / write)

1) Write register

A write operation is divided into several steps: Send start signal - "Start success? (may not be described as accurate) - "Send MPU6050 address, status (write) -" Write address is successful? -" Send a register address to be written inside the MPU6050 -" Send successfully? - "Send the content to be written -" Successfully sent? -" Send end signal

Summary: first write the MPU6050 address, then write the register address, and finally write the content, and each time you have to verify (should be related to the response signal). This is like sending a courier, first write the city and county address, then write the street address, and finally write the house number.

It will be easier to understand some of the following programs.

Void MPU6050_I2C_ByteWrite(u8 slaveAddr, u8 pBuffer, u8 writeAddr)

{

/* Send START condiTIon */

I2C_GenerateSTART(I2C1, ENABLE); //Send start signal

/* Test on EV5 and clear it */

While(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

/* Send MPU6050 address for write */

I2C_Send7bitAddress(I2C1, slaveAddr, I2C_DirecTIon_Transmitter); // Send MPU6050 address, status (write)

/* Test on EV6 and clear it */

While(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

/* Send the MPU6050's internal address to write to */

I2C_SendData(I2C1, writeAddr); //Send an internal register address to be written to the MPU6050

/* Test on EV8 and clear it */

While(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send the byte to be written */

I2C_SendData(I2C1, pBuffer); //Send the content to be written

/* Test on EV8 and clear it */

While(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send STOP condition */

I2C_GenerateSTOP(I2C1, ENABLE); //Send end signal

}

Digital Display Test Pen

Digital Display Test Pen ,Power Tester Pen,Voltage Pen,Digital Voltage Tester

YINTE TOOLS (NINGBO) CO., LTD , https://www.yinte-tools.com