|
LSM6DSL 单、双击检测 STMems_Standard_C_drivers库
[复制链接]
工程使用NUCLEO-L4R5ZI测试
程序参考了STM32CubeExpansion MEMS-XT1的SingleDoubleTap
- void single_double_tap(uint8_t double_mode)
- {
- lsm6dsl_all_sources_t lsm6dsl_all_sources;
- /*
- * Initialize mems driver interface
- */
- dev_ctx.write_reg = platform_write;
- dev_ctx.read_reg = platform_read;
- dev_ctx.handle = &hi2c1;
-
- /*
- * Check device ID
- */
- whoamI = 0;
- lsm6dsl_device_id_get(&dev_ctx, &whoamI);
- if ( whoamI != LSM6DSL_ID )
- while(1); /*manage here device not found */
- /*
- * Restore default configuration
- */
- lsm6dsl_reset_set(&dev_ctx, PROPERTY_ENABLE);
- do {
- lsm6dsl_reset_get(&dev_ctx, &rst);
- } while (rst);
- /*
- * Enable Block Data Update
- */
- lsm6dsl_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
- /*
- * Set Output Data Rate
- */
- lsm6dsl_xl_data_rate_set(&dev_ctx, LSM6DSL_XL_ODR_416Hz);
- /*
- * Set full scale
- */
- lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
- /* Enable x direction in tap recognition. */
- lsm6dsl_tap_detection_on_x_set(&dev_ctx,PROPERTY_ENABLE);
- /* Enable y direction in tap recognition. */
- lsm6dsl_tap_detection_on_y_set(&dev_ctx,PROPERTY_ENABLE);
- /* Enable z direction in tap recognition. */
- lsm6dsl_tap_detection_on_z_set(&dev_ctx,PROPERTY_ENABLE);
- /* Set tap threshold. LSM6DSL_TAP_THRESHOLD_MID_LOW 08*/
- lsm6dsl_tap_threshold_x_set(&dev_ctx,0x08);
- /*双击*/
- if(double_mode == 1)
- {
- /* Set tap shock time window. LSM6DSL_TAP_SHOCK_TIME_HIGH 0x03*/
- lsm6dsl_tap_shock_set(&dev_ctx,0x03);
- /* Set tap quiet time window. LSM6DSL_TAP_QUIET_TIME_HIGH 0x03*/
- lsm6dsl_tap_quiet_set(&dev_ctx,0x03);
- /* Set tap duration time window. LSM6DSL_TAP_DURATION_TIME_MID 0x08*/
- lsm6dsl_tap_dur_set(&dev_ctx,0x08);
- lsm6dsl_tap_mode_set(&dev_ctx,LSM6DSL_BOTH_SINGLE_DOUBLE);
- /*
- * 中断
- */
- lsm6dsl_int1_route_t lsm6dsl_int1_route;
- lsm6dsl_int1_route.int1_double_tap = PROPERTY_ENABLE;
- lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);
- }
- /*单击*/
- else
- {
-
- /* Set tap shock time window. LSM6DSL_TAP_SHOCK_TIME_MID_HIGH 0x02*/
- lsm6dsl_tap_shock_set(&dev_ctx,0x02);
- /* Set tap quiet time window. LSM6DSL_TAP_QUIET_TIME_MID_LOW 0x01*/
- lsm6dsl_tap_quiet_set(&dev_ctx,0x01);
- /* Reset tap duration time window.*/
- lsm6dsl_tap_dur_set(&dev_ctx,0x00);
- /*
- * 中断
- */
- lsm6dsl_int1_route_t lsm6dsl_int1_route;
- lsm6dsl_int1_route.int1_single_tap = PROPERTY_ENABLE;
- lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);
- }
-
- while(1)
- {
- if(int1_flag == 1)
- {
- int1_flag = 0;
- lsm6dsl_all_sources_get(&dev_ctx,&lsm6dsl_all_sources);
-
- if(double_mode == 1)
- {
- if(lsm6dsl_all_sources.reg.tap_src.double_tap == 1)
- {
- snprintf(data_out, MAX_BUF_SIZE, "Double tap indication.\r\n");
- tx_com((uint8_t*)data_out, strlen(data_out));
- }
- }
- else
- {
- if(lsm6dsl_all_sources.reg.tap_src.single_tap == 1)
- {
- snprintf(data_out, MAX_BUF_SIZE, "Single tap indication.\r\n");
- tx_com((uint8_t*)data_out, strlen(data_out));
- }
- }
- }
-
- }
- }
复制代码
工程文件:
lsm6dsl single double tap stm32l4r5 .rar
(1.64 MB, 下载次数: 20)
|
|