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

littleshrimp   2018-6-29 23:18 楼主

使用STMems_Standard_C_drivers库
程序参考了STM32CubeExpansion MEMS-XT1的LSM6DSL_FreeFallDetection


  1. void lsm6dsl_free_fall_detection(void)
  2. {  
  3.   /*
  4.    *  Initialize mems driver interface
  5.    */
  6.   dev_ctx.write_reg = platform_write;
  7.   dev_ctx.read_reg = platform_read;
  8.   dev_ctx.handle = &hi2c1;  
  9.   
  10.   /*
  11.    *  Check device ID
  12.    */
  13.   whoamI = 0;
  14.   lsm6dsl_device_id_get(&dev_ctx, &whoamI);
  15.   if ( whoamI != LSM6DSL_ID )
  16.     while(1); /*manage here device not found */
  17.   /*
  18.    *  Restore default configuration
  19.    */
  20.   lsm6dsl_reset_set(&dev_ctx, PROPERTY_ENABLE);
  21.   do {
  22.     lsm6dsl_reset_get(&dev_ctx, &rst);
  23.   } while (rst);
  24.   /*
  25.    *  Enable Block Data Update
  26.    */
  27.   lsm6dsl_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  28.   /*
  29.    * Set Output Data Rate
  30.    */
  31.   lsm6dsl_xl_data_rate_set(&dev_ctx, LSM6DSL_XL_ODR_416Hz);
  32.   /*
  33.    * Set full scale
  34.    */  
  35.   lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
  36.   lsm6dsl_wkup_dur_set(&dev_ctx,0);
  37.   lsm6dsl_timestamp_res_set(&dev_ctx,LSM6DSL_LSB_6ms4);
  38.   lsm6dsl_act_sleep_dur_set(&dev_ctx,0);
  39.   lsm6dsl_ff_dur_set(&dev_ctx,0);
  40.   lsm6dsl_ff_threshold_set(&dev_ctx,LSM6DSL_FF_TSH_312mg);
  41.   /*
  42.    * 中断
  43.    */  
  44.   lsm6dsl_int1_route_t lsm6dsl_int1_route;
  45.   lsm6dsl_int1_route.int1_ff = 1;
  46.   lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);
  47.   lsm6dsl_all_sources_t lsm6dsl_all_sources;
  48.   while(1)
  49.   {
  50.     if(int1_flag == 1)
  51.     {
  52.       int1_flag = 0;
  53.       lsm6dsl_all_sources_get(&dev_ctx,&lsm6dsl_all_sources);
  54.       if(lsm6dsl_all_sources.reg.wake_up_src.ff_ia == 1)
  55.       {
  56.         
  57.         snprintf(data_out, MAX_BUF_SIZE, "free fall indication.\r\n");
  58.         tx_com((uint8_t*)data_out, strlen(data_out));
  59.       }
  60.     }
  61.   }
  62. }




QQ图片20180629231609.png

工程文件
lsm6dsl stm32l4r5 free_fall_detection.rar (9.52 MB)
(下载次数: 5, 2018-6-29 23:16 上传)

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

回复评论 (1)

谢谢分享
嘉立创PCB打样  QQ800058452
点赞  2018-6-30 09:50
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复