11、Nine-axis attitude sensor to obtain data-ICM20948

11.1、Experimental purpose

Use the SPI communication of STM32 to read the raw data of the nine-axis attitude sensor ICM20948 and print it out through the serial port assistant.

11.2、Configuration pin information

1.Import the ioc file from the Serial project and name it Read_ICM20948.

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, the AD0/SDO pin is connected to PB14, and the NSS pin is connected to PB12.

image-20230606113411298

image-20220326160924577

2.Enable the SPI2 interface, set PB13, PB14, and PB15 as SPI2 pins, and use the PB12 software control method for the NSS chip select pin. The specific parameters are shown in the figure below:

image-20230605183103284

Then set PB12 to output mode and rename it ICM20948_CS.

image-20230605183410441

11.3、Experimental flow chart analysis

image-20220326163358899

11.4、Core code explanation

1.Create new bsp_icm20948.h and bsp_icm20948.c, and add the following content in bsp_icm20948.h.

2..Create the following content in the bsp_icm20948.c file:

Start initializing the ICM20948. First, reset and wake up the ICM20948, set the clock source, magnetometer access method and filtering configuration, then calibrate the gyroscope and accelerometer, and set the scaling ratio.

3.Initialize the AK09916 magnetometer.

4.Enables and disables SPI communication of ICM20948.

5.The SPI method of reading data, read_single_reg is to read one byte of data from a register, and read_multiple_reg is to read multiple bytes of data from a register.

6.The SPI method of writing data, write_single_reg is to write one byte of data to a register, and write_multiple_reg is to write multiple bytes of data to a register.

7.Read accelerometer raw data. Because the calibration function cancels out the acceleration due to gravity, adding a scaling factor to the Z-axis brings the reading back to normal.

Combine the accelerometer raw data and the scale factor to convert the unit to g.

8.Read gyroscope raw data.

Combine the gyroscope raw data and the scale factor to convert the unit to dps.

9.Read magnetometer raw data.

Convert the magnetometer raw data into uT units.

10.Add content to initialize ICM20948 and AK09916 in the Bsp_Init() function.

8.Add the function of reading ICM20948 data in the Bsp_Loop() function.

 

11.5、Hardware connection

The ICM20948 nine-axis attitude sensor is already soldered on the expansion board, so there is no need to manually connect the device.

image-20230606110430675

11.6、Experimental effect

After burning the program, the LED light flashes every 200 milliseconds. Open the serial port assistant (the parameters are shown in the figure below), and you can see that the serial port assistant keeps printing the data of the accelerometer accel, gyroscope gyro, and magnetometer mag of ICM20948.

image-20230606110506441