2021-03-24 06:04 AM
Hi folks,
Im working with lis3dh & stm32 using https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lis3dh_STdC
this library.No output found.While debugging my code stuck in while(1) loop(i.e while compairing i2c address) ,I even try with different i2c addess i.e(0x19,0x32) but it fails ,
My connections are SCL of sensor to SCL of uc board ,SDA (sensor) to SDA board vdd & gnd.
Please suggest any solution with this or other library, or connection.
Please check my code below.
/* USER CODE BEGIN 2 */
/* Initialize mems driver interface. */
stmdev_ctx_t dev_ctx;
lis3dh_ctrl_reg3_t ctrl_reg3;
lis3dh_int1_cfg_t int1_cfg;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hi2c1;
/* Initialize platform specific hardware */
platform_init();
/* Check device ID */
lis3dh_device_id_get(&dev_ctx, &whoamI);
if (whoamI != LIS3DH_ID) {
while (1) {
/* manage here device not found */
}
}
/* Set full scale to 2 g */
lis3dh_full_scale_set(&dev_ctx, LIS3DH_2g);
/* Enable AOI1 interrupt on INT pin 1 */
lis3dh_pin_int1_config_get(&dev_ctx, &ctrl_reg3);
ctrl_reg3.i1_ia1 = PROPERTY_ENABLE;
lis3dh_pin_int1_config_set(&dev_ctx, &ctrl_reg3);
/* Enable Interrupt 1 pin latched */
lis3dh_int1_pin_notification_mode_set(&dev_ctx, LIS3DH_INT1_LATCHED);
/* Set threshold to 16h -> 350mg
* Set Duration to 03h -> minimum event duration ~= 30 msec
* If acceleration an all axis is below the threshold for more
* than 30 ms than device is falling down */
lis3dh_int1_gen_threshold_set(&dev_ctx, 0x16);
lis3dh_int1_gen_duration_set(&dev_ctx, 0x02);
/* Configure free-fall recognition
*
* Set interrupt condition (AND) for x, y, z acc. data below
* threshold */
lis3dh_int1_gen_conf_get(&dev_ctx, &int1_cfg);
int1_cfg.aoi = PROPERTY_ENABLE;
int1_cfg.zlie = PROPERTY_ENABLE;
int1_cfg.ylie = PROPERTY_ENABLE;
int1_cfg.xlie = PROPERTY_ENABLE;
lis3dh_int1_gen_conf_set(&dev_ctx, &int1_cfg);
/* Set device in HR mode */
lis3dh_operating_mode_set(&dev_ctx, LIS3DH_HR_12bit);
/* Set Output Data Rate to 100 Hz */
lis3dh_data_rate_set(&dev_ctx, LIS3DH_ODR_100Hz);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* Check INT 1 pin or read source register */
lis3dh_reg_t all_source;
lis3dh_int1_gen_source_get(&dev_ctx, &all_source.int1_src);
if (!(all_source.byte & 0x3f))
{
sprintf((char *)tx_buffer, "freefall detected\r\n");
tx_com(tx_buffer, strlen((char const *)tx_buffer));
}
}
2021-03-25 02:07 AM
Hi @PMOHO.1 ,
Is the code not compiling or are you receiving wrong data from the sensor? In the latter case, could you share the digital pattern captured by an analyzer?
By the way, since you are using an STM32L4 and not a generic platform for the interface with the LIS3DH accelerometer, I would suggest you to start from an existing package for the sensors such as the FP-SNS-ALLMEMS1.
This function pack has lot of examples with boards STM32L4: you can find the sensor drivers in the \Drivers\BSP\Components folder, and the STM32L4 project in the \Projects folder.
-Eleon