使用STMems_Standard_C_drivers库
先通过lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);函数设置传感器DRDY中断输出引脚为INT1
再循环检测INT1引脚电平,产生中断后先读取状态寄存器再读取加速度、陀螺仪和温度传感器数据
- /* datalog_int1 Example --------------------------------------------------------------*/
- void datalog_int1(void)
- {
- /*
- * 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_12Hz5);
- lsm6dsl_gy_data_rate_set(&dev_ctx, LSM6DSL_GY_ODR_12Hz5);
- /*
- * Set full scale
- */
- lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
- lsm6dsl_gy_full_scale_set(&dev_ctx, LSM6DSL_2000dps);
-
- /*
- * Configure filtering chain(No aux interface)
- */
- /* Accelerometer - analog filter */
- lsm6dsl_xl_filter_analog_set(&dev_ctx, LSM6DSL_XL_ANA_BW_400Hz);
-
- /* Accelerometer - LPF1 path ( LPF2 not used )*/
- //lsm6dsl_xl_lp1_bandwidth_set(&dev_ctx, LSM6DSL_XL_LP1_ODR_DIV_4);
-
- /* Accelerometer - LPF1 + LPF2 path */
- lsm6dsl_xl_lp2_bandwidth_set(&dev_ctx, LSM6DSL_XL_LOW_NOISE_LP_ODR_DIV_100);
-
- /* Accelerometer - High Pass / Slope path */
- //lsm6dsl_xl_reference_mode_set(&dev_ctx, PROPERTY_DISABLE);
- //lsm6dsl_xl_hp_bandwidth_set(&dev_ctx, LSM6DSL_XL_HP_ODR_DIV_100);
-
- /* Gyroscope - filtering chain */
- lsm6dsl_gy_band_pass_set(&dev_ctx, LSM6DSL_HP_260mHz_LP1_STRONG);
-
- //使能加速度和陀螺仪DRDY中断
- lsm6dsl_int1_route_t lsm6dsl_int1_route;
- lsm6dsl_int1_route.int1_drdy_xl = 1;
- lsm6dsl_int1_route.int1_drdy_g = 1;
- lsm6dsl_pin_int1_route_set(&dev_ctx,lsm6dsl_int1_route);
- /*
- * Read samples in polling mode (no int)
- */
- while(1)
- {
- if(int1_flag == 1)
- {
- int1_flag = 0;
- /*
- * Read output only if new value is available
- */
- lsm6dsl_reg_t reg;
- lsm6dsl_status_reg_get(&dev_ctx, ®.status_reg);
- if (reg.status_reg.xlda)
- {
- /* Read magnetic field data */
- memset(data_raw_acceleration.u8bit, 0x00, 3*sizeof(int16_t));
- lsm6dsl_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
- acceleration_mg[0] = LSM6DSL_FROM_FS_2g_TO_mg( data_raw_acceleration.i16bit[0]);
- acceleration_mg[1] = LSM6DSL_FROM_FS_2g_TO_mg( data_raw_acceleration.i16bit[1]);
- acceleration_mg[2] = LSM6DSL_FROM_FS_2g_TO_mg( data_raw_acceleration.i16bit[2]);
-
- sprintf((char*)tx_buffer, "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
- acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
- tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
- }
- if (reg.status_reg.gda)
- {
- /* Read magnetic field data */
- memset(data_raw_angular_rate.u8bit, 0x00, 3*sizeof(int16_t));
- lsm6dsl_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate.u8bit);
- angular_rate_mdps[0] = LSM6DSL_FROM_FS_2000dps_TO_mdps(data_raw_angular_rate.i16bit[0]);
- angular_rate_mdps[1] = LSM6DSL_FROM_FS_2000dps_TO_mdps(data_raw_angular_rate.i16bit[1]);
- angular_rate_mdps[2] = LSM6DSL_FROM_FS_2000dps_TO_mdps(data_raw_angular_rate.i16bit[2]);
-
- sprintf((char*)tx_buffer, "Angular rate [mdps]:%4.2f\t%4.2f\t%4.2f\r\n",
- angular_rate_mdps[0], angular_rate_mdps[1], angular_rate_mdps[2]);
- tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
- }
- if (reg.status_reg.tda)
- {
- /* Read temperature data */
- memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
- lsm6dsl_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
- temperature_degC = LSM6DSL_FROM_LSB_TO_degC( data_raw_temperature.i16bit );
-
- sprintf((char*)tx_buffer, "Temperature [degC]:%6.2f\r\n", temperature_degC );
- tx_com( tx_buffer, strlen( (char const*)tx_buffer ) );
- }
- }
- }
- }
工程文件: