imu-lsm6ds3-read.ino

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

Demuestra la lectura bruta de los ejes X, Y, Z del IMU a través del bus I2C.

Requiere el controlador LSM6DS3TR-C de STMicroelectronics. El sensor se dirige a 0x6A en I2C1. Verifique los pines SCL/SDA correspondientes en el XIAO. Los datos son brutos (LSB), requieren conversión a g o dps según la sensibilidad configurada.

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);
  }
}

← Volver a la ficha