STM32 · 2023/07/05 0

STM32与ICM42688陀螺仪通信

STM32与ICM42688陀螺仪通信

在Arduino中工具栏汇总如是设置。需要打开USB CDC(虚拟串口)。

还需要添加ICM42688库。

#include <Arduino.h>
#include "ICM42688.h"

//HardwareSerial Serial1(PA10, PA9);

static const uint8_t CS_PIN = PC15;
static const uint8_t INT_PIN = PB2;
static const uint8_t MOSI_PIN = PD7;
static const uint8_t MISO_PIN = PA6;
static const uint8_t SCLK_PIN = PA5;
static SPIClass spi = SPIClass(MOSI_PIN, MISO_PIN, SCLK_PIN);

// an ICM42688 object with the ICM42688 sensor on SPI bus 0 and chip select pin 10
ICM42688 IMU(spi, CS_PIN);

void setup() {
  // serial to display data
  Serial.begin(115200);
  while(!Serial) {}

  // start communication with IMU
  int status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }
  Serial.println("ax,ay,az,gx,gy,gz,temp_C");
}

void loop() {
  // read the sensor
  IMU.getAGT();
  // display the data
  Serial.print(IMU.accX(),6);
  Serial.print("\t");
  Serial.print(IMU.accY(),6);
  Serial.print("\t");
  Serial.print(IMU.accZ(),6);
  Serial.print("\t");
  Serial.print(IMU.gyrX(),6);
  Serial.print("\t");
  Serial.print(IMU.gyrY(),6);
  Serial.print("\t");
  Serial.print(IMU.gyrZ(),6);
  Serial.print("\t");
  Serial.println(IMU.temp(),6);
  delay(10);
}