Stm32f7 – Disco Get data from IMU LSM9DS1 (I2C interface)

Stm32f7 – Disco Get data from IMU LSM9DS1 (I2C interface) . In this post, I show how to get data from LSM9DS1 sensor by I2C interface.
I do it with only gyroscope and accelerometer, if you want ,you can do the same with magnetometer.
The first you have to look at LSM9DS1 datasheet .
 LSM9DS1_DATASHEET

 Connect
Sensor                             MPU
3.3V——————————–3.3 V
GND——————————-GND
SCL———————————PB_8
SDA——————————–PB_9
1. Pin out setting
RCC -> High speed clock -> Crystal ceramic resonator
Up system clock to 216Mhz
I2C1 -> I2C check

2. I2C configuration
I2C speed mode -> High speed mode
I2C speed frequency ->300 Hz
Address length -> 7bit
Pull up GPIO PB_8 and PB_9

 
 

3. Source generation and edition
After generated the source you add in Src/main.c file

#include “stm32f7xx_hal.h”
#include “stm32f7xx_hal.h”
#include “stm32f7xx_hal_uart.h”
#include “stm32f7xx_hal_sdram.h”
#include “stm32f7xx_hal_ltdc.h”
#include “stm32746g_discovery.h”
#include “stm32746g_discovery_lcd.h”
#include “stm32746g_discovery_sdram.h”
#include “stm32f7xx_ll_fmc.h”
#define ACT_THS             0x04
#define ACT_DUR             0x05
#define INT_GEN_CFG_XL      0x06
#define INT_GEN_THS_X_XL    0x07
#define INT_GEN_THS_Y_XL    0x08
#define INT_GEN_THS_Z_XL    0x09
#define INT_GEN_DUR_XL      0x0A
#define REFERENCE_G         0x0B
#define INT1_CTRL           0x0C
#define INT2_CTRL           0x0D
#define WHO_AM_I_XG         0x0F
#define CTRL_REG1_G         0x10
#define CTRL_REG2_G         0x11
#define CTRL_REG3_G         0x12
#define ORIENT_CFG_G        0x13
#define INT_GEN_SRC_G       0x14
#define OUT_TEMP_L          0x15
#define OUT_TEMP_H          0x16
#define STATUS_REG_0        0x17
#define OUT_X_L_G           0x18
#define OUT_X_H_G           0x19
#define OUT_Y_L_G           0x1A
#define OUT_Y_H_G           0x1B
#define OUT_Z_L_G           0x1C
#define OUT_Z_H_G           0x1D
#define CTRL_REG4           0x1E
#define CTRL_REG5_XL        0x1F
#define CTRL_REG6_XL        0x20
#define CTRL_REG7_XL        0x21
#define CTRL_REG8           0x22
#define CTRL_REG9           0x23
#define CTRL_REG10          0x24
#define INT_GEN_SRC_XL      0x26
#define STATUS_REG_1        0x27
#define OUT_X_L_XL          0x28
#define OUT_X_H_XL          0x29
#define OUT_Y_L_XL          0x2A
#define OUT_Y_H_XL          0x2B
#define OUT_Z_L_XL          0x2C
#define OUT_Z_H_XL          0x2D
#define FIFO_CTRL           0x2E
#define FIFO_SRC            0x2F
#define INT_GEN_CFG_G       0x30
#define INT_GEN_THS_XH_G    0x31
#define INT_GEN_THS_XL_G    0x32
#define INT_GEN_THS_YH_G    0x33
#define INT_GEN_THS_YL_G    0x34
#define INT_GEN_THS_ZH_G    0x35
#define INT_GEN_THS_ZL_G    0x36
#define INT_GEN_DUR_G       0x37

///////////////////////////////
// LSM9DS1 Magneto Registers //
///////////////////////////////
#define OFFSET_X_REG_L_M    0x05
#define OFFSET_X_REG_H_M    0x06
#define OFFSET_Y_REG_L_M    0x07
#define OFFSET_Y_REG_H_M    0x08
#define OFFSET_Z_REG_L_M    0x09
#define OFFSET_Z_REG_H_M    0x0A
#define WHO_AM_I_M          0x0F
#define CTRL_REG1_M         0x20
#define CTRL_REG2_M         0x21
#define CTRL_REG3_M         0x22
#define CTRL_REG4_M         0x23
#define CTRL_REG5_M         0x24
#define STATUS_REG_M        0x27
#define OUT_X_L_M           0x28
#define OUT_X_H_M           0x29
#define OUT_Y_L_M           0x2A
#define OUT_Y_H_M           0x2B
#define OUT_Z_L_M           0x2C
#define OUT_Z_H_M           0x2D
#define INT_CFG_M           0x30
#define INT_SRC_M           0x30
#define INT_THS_L_M         0x32
#define INT_THS_H_M         0x33

#define IMU_ADD     0x6B<<1

———————————————————————————————–
 int main(void)
{

  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  

  BSP_LCD_Init();
  BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
  BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
  BSP_LCD_DisplayOn();
  BSP_LCD_SelectLayer(0);
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SelectLayer(1);
   BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE); 


  uint8_t cmd[1];
  cmd[0]=0xC0;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG1_G, 1, cmd,1,0×100);
  cmd[0]=0x00;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG2_G, 1, cmd,1,0×100);
  cmd[0]=0x00;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG3_G, 1, cmd,1,0×100);
  cmd[0]=0x38;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG4, 1, cmd,1,0×100);
  cmd[0]=0x38;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG5_XL, 1, cmd,1,0×100);
  cmd[0]=0x00;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG6_XL, 1, cmd,1,0×100);
  cmd[0]=0x00;
  HAL_I2C_Mem_Write(&hi2c1, IMU_ADD, CTRL_REG7_XL, 1, cmd,1,0×100);
  double acc[3],gyro[3];
  uint8_t data[6];
  int16_t i=0,gyro_data[3],acc_data[3];
  char buffer[30]; 

 
  while (1)
  {
    HAL_I2C_Mem_Read(&hi2c1, IMU_ADD, 0x18, 1, data,6,0×100);
      for(i=0;i<3;i++){
        gyro_data[i]=(data[2*i+1]<<8)|data[2*i]; 
        gyro[i]=(double)gyro_data[i]*0.00875;
    }
    sprintf(buffer,”vx=%2.2lf vy=%2.2lf vz=%2.2lf”, gyro[0],gyro[1],gyro[2]);
     BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)”GyroScope:”, LEFT_MODE);
     BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)buffer, LEFT_MODE); 


    HAL_I2C_Mem_Read(&hi2c1, IMU_ADD, 0x28, 1, data,6,0×100);
      for(i=0;i<3;i++){
        acc_data[i]=(data[2*i+1]<<8)|data[2*i]; 
        acc[i]=(double)acc_data[i]/16384*9.8;
    }
    sprintf(buffer,”ax=%2.2lf ay=%2.2lf az=%2.2lf”, acc[0],acc[1],acc[2]);
     BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)”Accelerometer:”, LEFT_MODE);
      BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)buffer, LEFT_MODE);
     HAL_Delay(200);
    BSP_LCD_Clear(LCD_COLOR_WHITE);

  }
}

Sponsored Links: