LSM303DLHC Accelerometer data extraction problem using stm32f401 uc
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
