11. Nine-axis attitude sensor acquisition data -ICM20948

11. Nine-axis attitude sensor acquisition data -ICM2094811.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

Using the SPI communication of STM32, the original data of the nine-axis attitude sensor ICM20948 is read and printed out through the serial assistant.

11.2. Configure 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

  1. Enable SPI2 interface, set PB13, PB14 and PB15 as SPI2 pins, and use PB12 software to control NSS chip selection pins. 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. Analysis of experimental flow chart

image-20220326163358899

11.4. Core code interpretation

  1. Create bSP_ICM209448.h and bsp_ICM209448.c, and add the following content to bsp_ICM209448.h:
  1. Create the following content in the bsp_icm20948.c file:

Start initializing the ICM20948 by resetting and waking up the ICM20948, setting the clock source, magnetometer access, and filtering, then calibrating the gyroscope and accelerometer, and setting the scaling ratio.

  1. Initialize the AK09916 magnetometer.
  1. Enable and disable SPI communication for ICM20948.
  1. The method of SPI reading data, read_single_reg reads one byte of data from a register, read_multiple_reg reads multiple bytes of data from a register.

6.SPI writes data. write_single_reg writes one byte of data to a register, and write_multiple_reg writes multiple bytes of data to a register.

  1. Read the original accelerometer data. Since the calibration function cancels out the acceleration of gravity, the scale factor is added to the Z-axis to restore the reading value to normal.

The raw accelerometer data and scale factor are combined to convert the unit to g.

  1. Read the original gyroscope data.

The raw gyroscope data and scale factor are combined to convert the unit to dps.

  1. Read the magnetometer raw data.

Convert the original magnetometer data into uT.

  1. Add the content to initialize ICM20948 and AK09916 in the Bsp_Init() function.
  1. Add the ability to read ICM20948 data to the Bsp_Loop() function.

 

11.5. Hardware connection

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

image-20230606110430675

11.6. Experimental effect

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

image-20230606110506441