imu-lsm6ds3-read.ino

Seeed Studio XIAO STM32C5 · Fichier : imu-lsm6ds3-read.ino · Langage : C · Usage : Lire les données d'accélération et de gyroscope du capteur LSM6DS3TR-C intégré.

Démontre la lecture brute des axes X, Y, Z de l'IMU via le bus I2C.

Nécessite le driver LSM6DS3TR-C de STMicroelectronics. Le capteur est adressé à 0x6A sur I2C1. Vérifiez les broches SCL/SDA correspondantes sur le XIAO. Les données sont brutes (LSB), nécessitant une conversion en g ou dps selon la sensibilité configurée.

imu-lsm6ds3-read.ino

#include "stm32c5xx_hal.h"
#include "lsm6ds3tr_c_reg.h" // Driver ST standard

extern I2C_HandleTypeDef hi2c1;
uint8_t whoami;
int16_t acc_data[3], gyr_data[3];

void Read_IMU(void) {
  uint8_t buf[6];
  // Lire Accéléromètre (OUTX_L_XL)
  HAL_I2C_Mem_Read(&hi2c1, 0x6A << 1, LSM6DS3TR_C_OUTX_L_XL, I2C_MEMADD_SIZE_8BIT, buf, 6, 100);
  acc_data[0] = (int16_t)(buf[1] << 8 | buf[0]);
  acc_data[1] = (int16_t)(buf[3] << 8 | buf[2]);
  acc_data[2] = (int16_t)(buf[5] << 8 | buf[4]);

  // Lire Gyroscope (OUTX_L_G)
  HAL_I2C_Mem_Read(&hi2c1, 0x6A << 1, LSM6DS3TR_C_OUTX_L_G, I2C_MEMADD_SIZE_8BIT, buf, 6, 100);
  gyr_data[0] = (int16_t)(buf[1] << 8 | buf[0]);
  gyr_data[1] = (int16_t)(buf[3] << 8 | buf[2]);
  gyr_data[2] = (int16_t)(buf[5] << 8 | buf[4]);
}

int main(void) {
  HAL_Init();
  // Init I2C1 (SCL/SDA sur XIAO)
  while(1) {
    Read_IMU();
    HAL_Delay(100);
  }
}

← Retour à la fiche