LSM6DSL翻腕亮屏检测代码,使用NUCLEO-L4R5ZI板子测试
配置方法参考贴子
和数据手册的2.2
传感器驱动使用STMems_Standard_C_drivers
官方代码中有一种错误会导致无法设置检测X轴以外的其它轴
原因在于下边函数中只读取了LSM6DSL_A_WRIST_TILT_MASK 而没有写回,需要自行修改
int32_t lsm6dsl_tilt_src_set(lsm6dsl_ctx_t *ctx,lsm6dsl_a_wrist_tilt_mask_t *val)
主要配置代码
- void wrist_tilt(void)
- {
- 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);
- //1)打开加速计,
- lsm6dsl_xl_data_rate_set(&dev_ctx, LSM6DSL_XL_ODR_26Hz);
- /*
- * Set full scale
- */
- lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
- //2)始能AWT,
- uint8_t wrist = PROPERTY_ENABLE;
- lsm6dsl_wrist_tilt_sens_set(&dev_ctx, wrist);
- ///3)配置AWT中断引脚,
- lsm6dsl_int2_route_t lsm6dsl_int2_route;
- lsm6dsl_int2_route.int2_wrist_tilt = PROPERTY_ENABLE;
- lsm6dsl_pin_int2_route_set(&dev_ctx,lsm6dsl_int2_route);
- ///4)配置检测轴向,
- lsm6dsl_a_wrist_tilt_mask_t lsm6dsl_a_wrist_tilt_mask;
- lsm6dsl_a_wrist_tilt_mask.wrist_tilt_mask_zneg = PROPERTY_ENABLE;
- lsm6dsl_a_wrist_tilt_mask.wrist_tilt_mask_zpos = PROPERTY_ENABLE;
- lsm6dsl_tilt_src_set(&dev_ctx,&lsm6dsl_a_wrist_tilt_mask);
- ///5)配置阈值,
- uint8_t threshold = 0x20;//1 LSB = 15.625 mg. Default value: 20h (500 mg).
- lsm6dsl_tilt_threshold_set(&dev_ctx,&threshold);
- ///6)配置持续时间
- uint8_t latency = 0x0f;//1 LSB = 40 ms. Default value: 0Fh (600 ms).
- lsm6dsl_tilt_latency_set(&dev_ctx,&latency);
- while(1)
- {
- if(int2_flag == 1)
- {
- int2_flag = 0;
- lsm6dsl_all_sources_get(&dev_ctx,&lsm6dsl_all_sources);
- if(lsm6dsl_all_sources.reg.wrist_tilt_ia.wrist_tilt_ia_zneg == 1 || lsm6dsl_all_sources.reg.wrist_tilt_ia.wrist_tilt_ia_zpos == 1)
- {
- snprintf(data_out, MAX_BUF_SIZE, "wrist tilt ia.\r\n");
- tx_com((uint8_t*)data_out, strlen(data_out));
- }
- }
- }
- }
工程文件: