2024-07-28 11:50 PM - edited 2024-07-29 02:58 AM
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:
Solved! Go to Solution.
2024-07-29 10:14 PM
I checked and found that MISO and MOSI was interchanged. So I corrected and tried again but still who_am_i register is return the same value 0xff instead of 0x44.
All the other high and low X,Y,Z axes registers are also return the same value as 0xff. Is there any problem with my code.
Please help.
2024-07-29 03:31 AM
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.
2024-07-29 03:35 AM
@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
2024-07-29 05:30 AM
Hi Sarra,
Thank you for replying.
The who_am_i register consistently returns 0xff (not 0x44) and tried to read the other register but it is also returns the same value as 0xff.
2024-07-29 05:37 AM
Hi Andrew,
Thank you for reply.
Here is the schematic:
The who_am_i register is not return the correct value(0x44). Instead it return 0xff.
2024-07-29 06:00 AM
@DivyaJayan wrote:The who_am_i register is not return the correct value(0x44). Instead it return 0xff.
That suggests that your SPI is not working at all. So, again:
The schematic image is too low-resolution to read clearly.
2024-07-29 06:06 AM
Here is the schematics :
2024-07-29 06:14 AM - edited 2024-07-29 06:19 AM
Are you sure you have MISO and MOSI the right way around?
:thinking_face:
EDIT:
2024-07-29 06:23 AM
Your schematic is still pretty low-res:
2024-07-29 10:05 PM
Hi Andrew,
Sorry for the low picture resolution. Here is the new one, please check it.