cancel
Showing results for 
Search instead for 
Did you mean: 

LSM303DLHC Accelerometer data extraction problem using stm32f401 uc

ssena.1514
Associate III

Hi,

I have a problem with reading the data from accelerometer LSM303DLHC using stm32f401 uc.

Here is the code snippet I have extracted from the demo .I was also following the datasheet.

int main(void)

{

 GPIO_InitTypeDef GPIO_InitStruct;

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14;

 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

 GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

 GPIO_InitStruct.GPIO_OType = GPIO_OType_PP ;

 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz ;

 GPIO_Init(GPIOD,&GPIO_InitStruct);

 if (SysTick_Config(SystemCoreClock / 1000))

 { 

  /* Capture error */ 

  while (1);

 }

Demo_Exec();

}//main end

/**

 * @brief Execute the demo application.

 * @param None

 * @retval None

 */

static void Demo_Exec(void)

{

 LSM303DLHCAcc_InitTypeDef LSM303DLHCAcc_InitStructure;

 LSM303DLHCAcc_FilterConfigTypeDef LSM303DLHCFilter_InitStructure;

  

while(1)

 {

  /* MEMS configuration ------------------------------------------------------*/

  /* Fill the accelerometer structure */

  LSM303DLHCAcc_InitStructure.Power_Mode = LSM303DLHC_NORMAL_MODE;

  LSM303DLHCAcc_InitStructure.AccOutput_DataRate = LSM303DLHC_ODR_50_HZ;

  LSM303DLHCAcc_InitStructure.Axes_Enable= LSM303DLHC_AXES_ENABLE;

  LSM303DLHCAcc_InitStructure.AccFull_Scale = LSM303DLHC_FULLSCALE_2G;

  LSM303DLHCAcc_InitStructure.BlockData_Update = LSM303DLHC_BlockUpdate_Continous;

  LSM303DLHCAcc_InitStructure.Endianness=LSM303DLHC_BLE_LSB;

  LSM303DLHCAcc_InitStructure.High_Resolution=LSM303DLHC_HR_ENABLE;

  /* Configure the accelerometer main parameters */

  LSM303DLHC_AccInit(&LSM303DLHCAcc_InitStructure);

   

  /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 

  = 3/100 = 30ms */

  Delay(30);

   

   

   

  /* Fill the accelerometer LPF structure */

  LSM303DLHCFilter_InitStructure.HighPassFilter_Mode_Selection =LSM303DLHC_HPM_NORMAL_MODE;

  LSM303DLHCFilter_InitStructure.HighPassFilter_CutOff_Frequency = LSM303DLHC_HPFCF_16;

  LSM303DLHCFilter_InitStructure.HighPassFilter_AOI1 = LSM303DLHC_HPF_AOI1_DISABLE;

  LSM303DLHCFilter_InitStructure.HighPassFilter_AOI2 = LSM303DLHC_HPF_AOI2_DISABLE;

   

  /* Configure the accelerometer LPF main parameters */

  LSM303DLHC_AccFilterConfig(&LSM303DLHCFilter_InitStructure);

  LSM303DLHC_AccFilterCmd(LSM303DLHC_HIGHPASSFILTER_ENABLE);

  LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_H_A, 6, Buffer_X);

  X_Offset = Buffer_X[0];

//x_data = X_Offset;

  LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_Y_H_A, 6, Buffer_Y);

  Y_Offset = Buffer_Y[0];

  //y_data = Y_Offset;

  /* Disable I2C1 used to drive the MEMS accelerometre */

  I2C_Cmd(LSM303DLHC_I2C, DISABLE);

GPIO_ToggleBits(GPIOD,GPIO_Pin_12);

   

}

}

//////////delay functions////////////////

/**

 * @brief Inserts a delay time.

 * @param nTime: specifies the delay time length, in milliseconds.

 * @retval None

 */

static void Delay(__IO uint32_t nTime)

 TimingDelay = nTime;

 while(TimingDelay != 0);

}

/**

 * @brief Decrements the TimingDelay variable.

 * @param None

 * @retval None

 */

void TimingDelay_Decrement(void)

{

 if (TimingDelay != 0x00)

 { 

  TimingDelay--;

 }

}

/**

 * @brief This function handles SysTick Handler.

 * @param None

 * @retval None

 */

void SysTick_Handler(void)

{

 TimingDelay_Decrement();

Counter ++;

if(Counter ==10)

{

  Buffer_X[0] = 0;

   Buffer_Y[0] = 0;

  LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_H_A, 6, Buffer_X);

   /* Remove the offsets values from data */

   Buffer_X[0] -= X_Offset;

    

   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_Y_H_A, 6, Buffer_Y);

   /* Remove the offsets values from data */

   Buffer_Y[0] -= Y_Offset;

temp1 = (int8_t)(Buffer_X[0]);

  Counter =0x00;

 }  

}

Kindly provide me the required information by analysing the above code.I mean where I am doing mistakes.

Board - STM32F401 Discovery IDE - Keil

Thanks & Regards

Satyabrata Senapati

3 REPLIES 3
TBomb.1
Senior II

Hi, which problem are you facing? A configuration problem (no data output) or you cannot even communicate with the device? If you query the WHO_AM_I register (0Fh) what value do you get?

I suggest you to check the code with the C github sample --> https://github.com/adafruit/Adafruit_LSM303DLHC

Tom

ssena.1514
Associate III

Hi,

Actually I was using the ​above code for data extraction from accelerometer .

May be It could be a configuration problem .

But when I am debugging I am not getting any change in data.

I also tried the accelerometer_mems_test() from demo code,but the data is not varying when using inside a while loop.

Ok let me check the given link from git hub.

Or will you show me the code section from demo code which I can follow?

Satya​

ssena.1514
Associate III

Hi Tom,

After downloading the stm32f401 disco demo .hex file to the board, the accelerometer functionality is not working after pressing the user button as per the readme instructions. It means the LSM303dlhc chip is damaged.

So to confirm that, will you take my demo.hex file & download(via st link utility) into your board to see the same effect? If the accelerometer functionality works there it means the chip in my board is damaged.

After confirming this problem ,I will buy a new accelerometer if possible.

Thats it . Please reply to this comment.

Satya