[传感器MCU驱动例程]
LSM6DSL 自由落体检测 STMems_Standard_C_drivers库
工程使用NUCLEO-L4R5ZI测试
程序参考了STM32CubeExpansion MEMS-XT1的LSM6DSL_Pedometer
- void lsm6dsl_pedometer(void)
- {
- uint16_t step_count;
- 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_26Hz);
- /*
- * Set full scale
- */
- lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
- lsm6dsl_pedo_full_scale_set(&dev_ctx,LSM6DSL_PEDO_AT_2g);
- /* Set pedometer threshold PEDOMETER_THRESHOLD_MID_HIGH. */
- lsm6dsl_pedo_threshold_set(&dev_ctx,0x17);
- /* Enable pedometer algorithm. */
- lsm6dsl_pedo_sens_set(&dev_ctx,1);
- lsm6dsl_pedo_step_reset_set(&dev_ctx,1);
- /*
- * 中断
- */
- lsm6dsl_int1_route_t lsm6dsl_int1_route;
- lsm6dsl_int1_route.int1_step_detector = 1;
- 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(lsm6dsl_all_sources.reg.func_src1.step_detected == 1)
- {
- if(lsm6dsl_setp_counter_get(&dev_ctx,&step_count) == 0)
- {
- snprintf(data_out, MAX_BUF_SIZE, "Step count: %d\r\n", step_count);
- tx_com((uint8_t*)data_out, strlen(data_out));
- }
- }
- }
- }
- }
工程文件:
暂无评论,赶紧抢沙发吧