Welcome to RDK X3 Robot repository
11. Nine-axis attitude sensor to obtain data
11. The nine-axis attitude sensor acquires data
11. The nine-axis attitude sensor acquires data11.1. Experimental purpose11.2. Configure pin information11.3. Analysis of experimental flow chart11.4. Core code interpretation11.5. Hardware connection11.6. Experimental effect
11.1. Experimental purpose
The GPIO port of the STM32 is used to simulate IIC communication, read the original data of the nine-axis attitude sensor MPU9250, and print it out through the serial assistant.
11.2. Configure pin information
- Import the ioc file from the Serial project and name it Read_IMU.
According to the schematic diagram, the SDA/SDI pin of the nine-axis attitude sensor is connected to PB15, the SCL/SCLK pin is connected to PB13, and the AD0/SDO pin is connected to PB14.


- Set PB13, PB14, and PB15 to the output mode, as shown in the following figure:


11.3. Analysis of experimental flow chart

11.4. Core code interpretation
- Create bsp_mpuic. h and bsp_mpuic. c, and add the following content to bsp_mpuic. h:

- Create the following content in the bsp_mpuic. c file:
According to the IIC protocol, MPU_IIC_Start() generates the IIC start signal, and MPU_IIC_Stop() generates the IIC stop signal.
3.IIC response correlation function.

4.IIC sends and reads data related functions.

- Create bSP_MPU9250-h and bsp_mPU9250-c, and add the following content to bsp_mPU9250-h:

- Add the following related functions to bsp_mpu9250.c.
Pull down pin AD0 so that the ID of the MPU6500 is 0x68.

Initialize MPU9250, return values: 0, success, the other, error codes

Read gyroscope value (original value), return value :0, success, other, error code

Read acceleration value (original value), return value :0, success, other, error code

Read the magnetometer value (original value), return value :0, success, other, error code

Read and print data, called every 10 milliseconds.

- Add the content to initialize MPU9250 in the Bsp_Init() function. If the initialization fails, stop the program.

- Add the ability to read MPU9250 data to the Bsp_Loop() function.

11.5. Hardware connection
The MPU9250 nine-axis attitude sensor is already soldered to the expansion plate, so no manual device connection is required.

11.6. Experimental effect
After burning the program, the LED light flashes every 200 milliseconds. Open the serial assistant (parameters are shown in the following figure), you can see that the serial assistant has been printing the data of the MPU9250's accelerometer accel, gyroscope gyro, magnetometer mag.
