2024-10-16 01:49 AM - last edited on 2024-10-23 02:13 AM by Andrew Neil
+I'm a beginner. I used the STM32 CubeIDE to compile and used the SPI of the Nucleo-F446RE to connect to the LSM9DS1. I want to read the data of its accelerometer, gyroscope and magnetometer, and use the field expression function to observe it. I found that the value read by it will keep beating without moving the LSM9DS1 and my magnetometer will not respond. I don't know what I did wrong.
The following is what I wrote myself about the function library and main program used by LSM9DS1.
[SPI_IMU_LSM9DS1.h]
#include "stm32f446xx.h"
#include "stm32f4xx_hal.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#ifndef INC_SPI_IMU_LSM9DS1_H_
#define INC_SPI_IMU_LSM9DS1_H_
#define CS_AG_PORT GPIOA
#define CS_AG_PIN GPIO_PIN_0
#define CS_M_PORT GPIOA
#define CS_M_PIN GPIO_PIN_1
#define DEN_AG_PIN GPIO_PIN_2
// Magnetometer Register address (CS_M low level)
#define WHO_AM_I_M 0x0F // Response: 0011 1101 (0x3D)
#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 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 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 function type
int8_t IMU_spiRead_9D(SPI_HandleTypeDef *hspi, uint8_t address, uint8_t measurer);
void IMU_spiWrite_9D(SPI_HandleTypeDef *hspi, uint8_t address, uint8_t data, uint8_t measurer);
int8_t IMU_configure_9D(SPI_HandleTypeDef *hspi);
int16_t Check_ID(SPI_HandleTypeDef *hspi);
#endif /* INC_SPI_IMU_LSM9DS1_H_ */
[SPI_IMU_LSM9DS1.c]
#include "stdio.h"
#include "stdlib.h"
#include "SPI_IMU_LSM9DS1.h"
#include "main.h"
int8_t IMU_configure_9D(SPI_HandleTypeDef *hspi)
{
// IMU_spiWrite_9D(SPI_HandleTypeDef *hspi, uint8_t address, uint8_t data, uint8_t measurer);
//PC0 = 0;
//int8_t data1;
//IMU_spiWrite_9D(CTRL_REG8,0x01,1); //1100 0001
//interrupt
IMU_spiWrite_9D(hspi, 0x06, 0x3F, 1); // 0011 1111
IMU_spiWrite_9D(hspi, 0x30, 0x3F, 1); // 0011 1111
// IMU configure gyroscope accelerometer
IMU_spiWrite_9D(hspi, CTRL_REG1_G, 0x78, 1); //0101 1100 2000dps ODR=199Hz cut-off=14Hz (after LPF1)(after LPF2) //011 1 1000
// when both Gyro and Accel enabled, Both ODR only be set here!
// 110 11 0 11 = 1101 1011
IMU_spiWrite_9D(hspi, CTRL_REG2_G, 0x00, 1); //0000 0000 //0000 0000
//LP_mode HP_EN 0(1)1 0(1) HPCF3_G HPCF2_G HPCF1_G HPCF0_G
IMU_spiWrite_9D(hspi, CTRL_REG3_G, 0x48, 1); //0000 0000 HP filter // 0000 1000
IMU_spiWrite_9D(hspi, ORIENT_CFG_G, 0x00, 1); //0000 0000
IMU_spiWrite_9D(hspi, 0x14, 0x00, 1);
IMU_spiWrite_9D(hspi, CTRL_REG4, 0x38, 1); //0011 1000 output enabled, interrupt
IMU_spiWrite_9D(hspi, CTRL_REG5_XL, 0x78, 1); // 01 1 1 1 000 decimation?
IMU_spiWrite_9D(hspi, CTRL_REG6_XL, 0x16, 1); // 000 10 1 10 "0x10"ODR_XL=0Hz(ODR=119Hz) BW=408Hz BW selected by ODR +-4g //011 11 0 00
//ODR_XL only enable when "only-Accel mode":1101 0000 ODR_XL=952Hz BW=408Hz BW selected by ODR
IMU_spiWrite_9D(hspi, CTRL_REG7_XL, 0x02, 1); //0000 0010 HR=1 DCF=10 LP cutff=ODR/9
IMU_spiWrite_9D(hspi, CTRL_REG8, 0x40, 1); // 0100 0000 last bit is reset 0000 0100
IMU_spiWrite_9D(hspi, CTRL_REG9, 0x07, 1); // 0000 0111
IMU_spiWrite_9D(hspi, 0x2E, 0x00, 1); //000 0 0001
// end
// IMU configure Magnetometer
IMU_spiWrite_9D(hspi, CTRL_REG1_M, 0x62, 0); //0 11 000 1 0 temperature com not using,ultra mode,Fast_ODR disabled 0010 0010
IMU_spiWrite_9D(hspi, CTRL_REG2_M, 0x60, 0); //0 11 0 0 0 0 00 +-16 gauss bit2 is reset operation //+-4 cause error ,10 +-12, 01 +-8, 00 +-4
IMU_spiWrite_9D(hspi, CTRL_REG3_M, 0x80, 0); //1 0 0 00 0 00 I2C_DISABLE,SPI read&write,Operating mode=Single-conversion mode %%%%%%%%% SIM opposite 1000 0000
IMU_spiWrite_9D(hspi, CTRL_REG4_M, 0x0C, 0); //0000 11 00
IMU_spiWrite_9D(hspi, CTRL_REG5_M, 0x00, 0); //0100 0000
return 0;
}
int16_t Check_ID(SPI_HandleTypeDef *hspi)
{
return IMU_spiRead_9D(hspi, WHO_AM_I_AG, 1); // Read the ID of the accelerometer/gyroscope
}
void IMU_spiWrite_9D(SPI_HandleTypeDef *hspi, uint8_t address, uint8_t data, uint8_t measurer)
{
uint8_t txData[2] = {address & ~0x80, data}; // Write operation, clear RW bit
if (measurer == 1) // Accelerometer/Gyroscope
{
HAL_GPIO_WritePin(CS_AG_PORT, CS_AG_PIN, GPIO_PIN_RESET); // Pull CS_AG low
HAL_SPI_Transmit(hspi, txData, 2, HAL_MAX_DELAY); // Send data
while (HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY); // wait
HAL_GPIO_WritePin(CS_AG_PORT, CS_AG_PIN, GPIO_PIN_SET); // Pull up CS_AG
}
else // Magnetometer
{
HAL_GPIO_WritePin(CS_M_PORT, CS_M_PIN, GPIO_PIN_RESET); // Pull CS_M low
HAL_SPI_Transmit(hspi, txData, 2, HAL_MAX_DELAY); // Send Data
while (HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY); // wiat
HAL_GPIO_WritePin(CS_M_PORT, CS_M_PIN, GPIO_PIN_SET); // Pull up CS_M
}
}
int8_t IMU_spiRead_9D(SPI_HandleTypeDef *hspi, uint8_t address, uint8_t measurer)
{
uint8_t txData = address | 0x80; // Read operation, set RW bit
uint8_t rxData = 0;
if (measurer == 1) // Accelerometer/Gyroscope
{
HAL_GPIO_WritePin(CS_AG_PORT, CS_AG_PIN, GPIO_PIN_RESET); // 拉低CS_AG
HAL_SPI_Transmit(hspi, &txData, 1, HAL_MAX_DELAY); // 發送地址
while (HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY);
HAL_SPI_Receive(hspi, &rxData, 1, HAL_MAX_DELAY); // 接收數據
while (HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY);
HAL_GPIO_WritePin(CS_AG_PORT, CS_AG_PIN, GPIO_PIN_SET); // 拉高CS_AG
}
else // Magnetometer
{
HAL_GPIO_WritePin(CS_M_PORT, CS_M_PIN, GPIO_PIN_RESET); // 拉低CS_M
HAL_SPI_Transmit(hspi, &txData, 1, HAL_MAX_DELAY); // 發送地址
while (HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY);
HAL_SPI_Receive(hspi, &rxData, 1, HAL_MAX_DELAY); // 接收數據
while (HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY);
HAL_GPIO_WritePin(CS_M_PORT, CS_M_PIN, GPIO_PIN_SET); // 拉高CS_M
}
return rxData;
}
2024-10-16 04:08 AM
Hello @Jian003 and welcome to the community,
Please use </> button to paste your code. You can refer to our tips on posting on this link. I'm editing your post then.
Thank you for your understanding.
2024-10-23 06:11 AM