[传感器MCU驱动例程] LSM6DSL 自由落体检测 STMems_Standard_C_drivers库

littleshrimp   2018-6-30 21:22 楼主
工程使用NUCLEO-L4R5ZI测试
程序参考了STM32CubeExpansion MEMS-XT1的LSM6DSL_Pedometer
  1. void lsm6dsl_pedometer(void)
  2. {
  3.   uint16_t step_count;
  4.   lsm6dsl_all_sources_t lsm6dsl_all_sources;
  5.     /*
  6.    *  Initialize mems driver interface
  7.    */
  8.   dev_ctx.write_reg = platform_write;
  9.   dev_ctx.read_reg = platform_read;
  10.   dev_ctx.handle = &hi2c1;  
  11.   
  12.   /*
  13.    *  Check device ID
  14.    */
  15.   whoamI = 0;
  16.   lsm6dsl_device_id_get(&dev_ctx, &whoamI);
  17.   if ( whoamI != LSM6DSL_ID )
  18.     while(1); /*manage here device not found */
  19.   /*
  20.    *  Restore default configuration
  21.    */
  22.   lsm6dsl_reset_set(&dev_ctx, PROPERTY_ENABLE);
  23.   do {
  24.     lsm6dsl_reset_get(&dev_ctx, &rst);
  25.   } while (rst);
  26.   /*
  27.    *  Enable Block Data Update
  28.    */
  29.   lsm6dsl_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  30.   /*
  31.    * Set Output Data Rate
  32.    */
  33.   lsm6dsl_xl_data_rate_set(&dev_ctx, LSM6DSL_XL_ODR_26Hz);
  34.   /*
  35.    * Set full scale
  36.    */  
  37.   lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
  38.   lsm6dsl_pedo_full_scale_set(&dev_ctx,LSM6DSL_PEDO_AT_2g);
  39.   /* Set pedometer threshold PEDOMETER_THRESHOLD_MID_HIGH. */
  40.   lsm6dsl_pedo_threshold_set(&dev_ctx,0x17);
  41.   /* Enable pedometer algorithm. */
  42.   lsm6dsl_pedo_sens_set(&dev_ctx,1);
  43.   lsm6dsl_pedo_step_reset_set(&dev_ctx,1);
  44.   /*
  45.    * 中断
  46.    */  
  47.   lsm6dsl_int1_route_t lsm6dsl_int1_route;
  48.   lsm6dsl_int1_route.int1_step_detector = 1;
  49.   lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);
  50.   while(1)
  51.   {
  52.     if(int1_flag == 1)
  53.     {
  54.       int1_flag = 0;
  55.       lsm6dsl_all_sources_get(&dev_ctx,&lsm6dsl_all_sources);
  56.       if(lsm6dsl_all_sources.reg.func_src1.step_detected == 1)
  57.       {
  58.         if(lsm6dsl_setp_counter_get(&dev_ctx,&step_count) == 0)
  59.         {        
  60.             snprintf(data_out, MAX_BUF_SIZE, "Step count: %d\r\n", step_count);
  61.             tx_com((uint8_t*)data_out, strlen(data_out));
  62.         }
  63.       }
  64.     }
  65.   }
  66. }

QQ图片20180630211738.png

工程文件:
lsm6dsl pedometer stm32l4r5 .rar (15.88 MB)
(下载次数: 74, 2018-6-30 21:22 上传)

虾扯蛋,蛋扯虾,虾扯蛋扯虾

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复