cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2DTW12 - SPI

DivyaJayan
Associate II

Hi, 

 

I am attempting to obtain readings from the LIS2DTW12 accelerometer on board, utilizing the SPI interface and an STM32F098RCT6 microcontroller.

I’m accessing the ‘who_am_i’ register along with the high and low registers for the x, y, and z axes. Upon debugging, I’ve found that the high and low values for the x, y, and z axes are consistently 0xff, resulting in raw x, y, and z values of 0xffff.

Notably, these values remain unchanged even when the board is in motion.

Please help me.

Here is the code:

 

 

 

 

 

#define LIS2DTW12_WHO_IAM_I_ADDR    0x0F
#define LIS2DTW12_OUT_X_L_ADDR      0x28
#define LIS2DTW12_OUT_X_H_ADDR      0x29
#define LIS2DTW12_OUT_Y_L_ADDR      0x2A
#define LIS2DTW12_OUT_Y_H_ADDR      0x2B
#define LIS2DTW12_OUT_Z_L_ADDR      0x2C
#define LIS2DTW12_OUT_Z_H_ADDR      0x2D

uint8_t LIS2DTW12_ReadReg(uint8_t reg){

	uint8_t Received_data = 0;
	uint8_t address = reg | 0x80;

	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&hspi1, &address,1,50);
	HAL_SPI_Receive(&hspi1, &Received_data, 1, 50);
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
	return Received_data;
}
int main(void){
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_SPI1_Init();

  uint8_t who_am_i = LIS2DTW12_ReadReg(LIS2DTW12_WHO_IAM_I_ADDR);
  if(who_am_i != 0x44){}

  uint8_t x_low  = LIS2DTW12_ReadReg(LIS2DTW12_OUT_X_L_ADDR);
  uint8_t x_high = LIS2DTW12_ReadReg(LIS2DTW12_OUT_X_H_ADDR);
  uint8_t y_low  = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Y_L_ADDR);
  uint8_t y_high = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Y_H_ADDR);
  uint8_t z_low  = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Z_L_ADDR);
  uint8_t z_high = LIS2DTW12_ReadReg(LIS2DTW12_OUT_Z_H_ADDR);

  uint16_t  x_raw = ((x_high << 8)|x_low);
  uint16_t  y_raw = ((y_high << 8)|y_low);
  uint16_t  z_raw = ((z_high << 8)|z_low);

  while (1)
  {
  }
}
static void MX_SPI1_Init(void){ 
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}

 

 

 

 

 

 

After debugging, values are obtaining as follows:

Screenshot 2024-07-29 121820.png

2 REPLIES 2
Sarra.S
ST Employee

Hello @DivyaJayan,

Try reading other registers to see if they return valid data, if the who_am_i register returns the expected value (0x44), there might be an issue with the sensor itself 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Andrew Neil
Evangelist III

@DivyaJayan wrote:

I’m accessing the ‘who_am_i’ register :


So, as @Sarra.S asked,  does that return the correct value?

Are there any other devices on the same SPI bus? If so, are they working OK?

Have you used a logic analyser or oscilloscope to verify what's happening on the wires?

Please post your schematic showing the connections between the STM32 and the LIS2DTW12