|
Bosch最新款单颗六轴惯性测量元件产品:BMI160,内置16-bit加速度传感器和16-bit陀螺仪,既能通过加速度追踪线性运动,也能通过陀螺仪追踪Z轴原地翻转运动,可适应智能手机上更多动感游戏要求。Bosch的陀螺仪具备多种运作模式,6轴全开模式功耗仅960uA,节省电力;休眠时耗电量减至5成,而唤醒时间仅需10毫秒,借由加速度所侦测到的动作即可触发。配合高精度16-bit加速度传感器,为陀螺仪提供高精度校准,从而获得稳定的6轴运动数据。Max32630开发板自带BMI160传感器,可以用他来做一个简单的计步;
首先官方提供了驱动程序,很方便,也很简单;
读取的数据
使用时,只需要增加自己的驱动即可;
Max32630开发板和BMI160传感器使用IIC通信;
所以首先需要初始化IIC,然后
struct bmi160_dev sensor;sensor.id = BMI160_I2C_ADDR;sensor.interface = BMI160_I2C_INTF;sensor.read = user_i2c_read;sensor.write = user_i2c_write;sensor.delay_ms = user_delay_ms; int8_t rslt = BMI160_OK;rslt = bmi160_init(&sensor);提供IIC的读写函数和延时函数; user_i2c_read;user_i2c_write;user_delay_ms;其他按照提供的README.md操作。驱动部分: - #include "bmi160_i2c.h"
- #include "i2cm.h"
- const sys_cfg_i2cm_t bmi160_sys_cfg = {
- .clk_scale = CLKMAN_SCALE_AUTO,
- .io_cfg = IOMAN_I2CM2(IOMAN_MAP_A, 1)
- };
- struct bmi160_dev sensor;
- struct bmi160_cfg prev_accel_cfgure;
- struct bmi160_cfg prev_gyro_cfgure;
- struct bmi160_aux_cfg aux_cfgure;
- struct bmi160_aux_cfg prev_aux_cfgure;
- struct bmi160_cfg accel_cfgure = {
- .power = BMI160_ACCEL_NORMAL_MODE,
- .odr = BMI160_ACCEL_ODR_1600HZ,
- .range = BMI160_ACCEL_RANGE_2G,
- .bw = BMI160_ACCEL_BW_NORMAL_AVG4,
- };
- struct bmi160_cfg gyro_cfgure = {
- .power = BMI160_GYRO_NORMAL_MODE,
- .odr = BMI160_GYRO_ODR_3200HZ,
- .range = BMI160_GYRO_RANGE_2000_DPS,
- .bw = BMI160_GYRO_BW_NORMAL_MODE,
- };
- #define BMI160_I2CM MXC_I2CM2
- void BMI160_Init(void)
- {
- uint8_t ui8Status = 0;
- uint8_t ui8Attempts = 20;
- s8 rslt = BMI160_OK;
- u8 temp = 0;
- // u8 *temp = NULL;
- // u8 reg_adr = 0;
-
- sensor.id = BMI160_I2C_ADDR;
- sensor.interface = BMI160_I2C_INTF; /*! 0 - I2C , 1 - SPI Interface */
- sensor.read = bmi_i2c_read;
- sensor.write = bmi_i2c_write;
- sensor.delay_ms = delay_ms;
- rslt = I2CM_Init(BMI160_I2CM, &bmi160_sys_cfg, I2CM_SPEED_400KHZ);
-
- rslt = bmi160_init(&sensor); //Init
- if(BMI160_OK == rslt)
- printf("BMI160 Init OK!\r\n");
- else
- printf("BMI160 Init Erro: %d!\r\n",rslt);
-
- BMI160_Set_PowerMode();
-
- /* Select the Output data rate, range of accelerometer sensor */
- sensor.accel_cfg.odr = BMI160_ACCEL_ODR_800HZ;
- sensor.accel_cfg.range = BMI160_ACCEL_RANGE_4G;
- sensor.accel_cfg.bw = BMI160_ACCEL_BW_OSR2_AVG2;
- /* Select the power mode of accelerometer sensor */
- sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
- /* Select the Output data rate, range of Gyroscope sensor */
- // sensor.gyro_cfg.odr = BMI160_GYRO_ODR_3200HZ;
- // sensor.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
- // sensor.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
- // /* Select the power mode of Gyroscope sensor */
- // sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
- /* Set the sensor configuration */
- rslt = bmi160_set_sens_conf(&sensor);
- if(BMI160_OK == rslt)
- printf("BMI160 Conf OK!\r\n");
- else
- printf("BMI160 Conf Erro: %d!\r\n",rslt);
-
-
-
- }
- int8_t bmi_i2c_read(uint8_t dev_addr, uint8_t reg_addr,uint8_t *data, uint16_t len)
- {
- if( I2CM_Read(BMI160_I2CM, dev_addr, ®_addr, 1, data, len) < 0)
- return BMI160_E_DEV_NOT_FOUND;
-
- return BMI160_OK;
- }
- int8_t bmi_i2c_write(uint8_t dev_addr, uint8_t reg_addr,uint8_t *data, uint16_t len)
- {
- if(I2CM_Write(BMI160_I2CM, dev_addr, ®_addr, 1, data, len)<0)
- return BMI160_E_DEV_NOT_FOUND;
- return BMI160_OK;
-
- }
复制代码读取数据部分: - #include "bmi160_app.h"
- #include "bmi160_i2c.h"
- //struct bmi160_sensor_data accel_data;
- //struct bmi160_sensor_data gyro_data;
- struct bmi160_sensor_data accel;
- struct bmi160_sensor_data gyro;
- void BMI160_Task(void)
- {
- u8 temp = BMI160_ACCEL_NORMAL_MODE;
- u16 Step = 0;
- static u32 PointM = 0;
- static u8 mState = 0;
- if( ( OsDelayCCnt - PointM ) >= T_1S)
- {
- PointM = OsDelayCCnt;
-
- int8_t rslt = BMI160_OK;
-
- bmi160_read_step_counter(&User.StepCount,&sensor);
- /* To read only Accel data */
- rslt = bmi160_get_sensor_data(BMI160_ACCEL_SEL, &accel, NULL, &sensor);
-
- /* To read only Gyro data */
- rslt = bmi160_get_sensor_data(BMI160_GYRO_SEL, NULL, &gyro, &sensor);
-
- /* To read both Accel and Gyro data */
- bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &accel, &gyro, &sensor);
- /* To read Accel data along with time */
- rslt = bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_TIME_SEL) , &accel, NULL, &sensor);
-
- /* To read Gyro data along with time */
- rslt = bmi160_get_sensor_data((BMI160_GYRO_SEL | BMI160_TIME_SEL), NULL, &gyro, &sensor);
- /* To read both Accel and Gyro data along with time*/
- bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL | BMI160_TIME_SEL), &accel, &gyro, &sensor);
- }
-
-
- }
- void BMI160_Set_PowerMode(void)
- {
- int8_t rslt = BMI160_OK;
- /* Select the power mode */
- sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
- sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
- /* Set the Power mode */
- rslt = bmi160_set_power_mode(&sensor);
- if (rslt == BMI160_OK) {
- printf("\r\n Set PowerMode SUCCESS\r\n");
- } else {
- printf("\r\n Set PowerMode FAIL\r\n");
- }
- }
- void BMI160_Reset(void)
- {
- int8_t rslt = BMI160_OK;
- rslt = bmi160_soft_reset(&sensor);
- if (rslt == BMI160_OK) {
- printf("\r\n Reset SUCCESS\r\n");
- } else {
- printf("\r\n Reset FAIL\r\n");
- }
- }
- void BMI160_Set_StepInt(void)
- {
- struct bmi160_int_settg int_config;
- /* Select the Interrupt channel/pin */
- int_config.int_channel = BMI160_INT_CHANNEL_1;// Interrupt channel/pin 1
- /* Select the Interrupt type */
- int_config.int_type = BMI160_STEP_DETECT_INT;// Choosing Step Detector interrupt
- /* Select the interrupt channel/pin settings */
- int_config.int_pin_settg.output_en = BMI160_ENABLE;// Enabling interrupt pins to act as output pin
- int_config.int_pin_settg.output_mode = BMI160_DISABLE;// Choosing push-pull mode for interrupt pin
- int_config.int_pin_settg.output_type = BMI160_ENABLE;// Choosing active High output
- int_config.int_pin_settg.edge_ctrl = BMI160_ENABLE;// Choosing edge triggered output
- int_config.int_pin_settg.input_en = BMI160_DISABLE;// Disabling interrupt pin to act as input
- int_config.int_pin_settg.latch_dur =BMI160_LATCH_DUR_NONE;// non-latched output
- /* Select the Step Detector interrupt parameters, Kindly use the recommended settings for step detector */
- int_config.int_type_cfg.acc_step_detect_int.step_detector_mode = BMI160_STEP_DETECT_NORMAL;
- int_config.int_type_cfg.acc_step_detect_int.step_detector_en = BMI160_ENABLE;// 1-enable, 0-disable the step detector
- /* Set the Step Detector interrupt */
- bmi160_set_int_config(&int_config, &sensor); /* sensor is an instance of the structure bmi160_dev */
-
- }
- void BMI160_Any_Motion_Interrupt(void)
- {
- struct bmi160_int_settg int_config;
- /* Select the Interrupt channel/pin */
- int_config.int_channel = BMI160_INT_CHANNEL_1;// Interrupt channel/pin 1
- /* Select the Interrupt type */
- int_config.int_type = BMI160_ACC_ANY_MOTION_INT;// Choosing Any motion interrupt
- /* Select the interrupt channel/pin settings */
- int_config.int_pin_settg.output_en = BMI160_ENABLE;// Enabling interrupt pins to act as output pin
- int_config.int_pin_settg.output_mode = BMI160_DISABLE;// Choosing push-pull mode for interrupt pin
- int_config.int_pin_settg.output_type = BMI160_DISABLE;// Choosing active low output
- int_config.int_pin_settg.edge_ctrl = BMI160_ENABLE;// Choosing edge triggered output
- int_config.int_pin_settg.input_en = BMI160_DISABLE;// Disabling interrupt pin to act as input
- int_config.int_pin_settg.latch_dur = BMI160_LATCH_DUR_NONE;// non-latched output
- /* Select the Any-motion interrupt parameters */
- int_config.int_type_cfg.acc_any_motion_int.anymotion_en = BMI160_ENABLE;// 1- Enable the any-motion, 0- disable any-motion
- int_config.int_type_cfg.acc_any_motion_int.anymotion_x = BMI160_ENABLE;// Enabling x-axis for any motion interrupt
- int_config.int_type_cfg.acc_any_motion_int.anymotion_y = BMI160_ENABLE;// Enabling y-axis for any motion interrupt
- int_config.int_type_cfg.acc_any_motion_int.anymotion_z = BMI160_ENABLE;// Enabling z-axis for any motion interrupt
- int_config.int_type_cfg.acc_any_motion_int.anymotion_dur = 0;// any-motion duration
- int_config.int_type_cfg.acc_any_motion_int.anymotion_thr = 20;// (2-g range) -> (slope_thr) * 3.91 mg, (4-g range) -> (slope_thr) * 7.81 mg, (8-g range) ->(slope_thr) * 15.63 mg, (16-g range) -> (slope_thr) * 31.25 mg
- /* Set the Any-motion interrupt */
- bmi160_set_int_config(&int_config, &sensor); /* sensor is an instance of the structure bmi160_dev */
- }
- void BMI160_User_Space(void)
- {
- int8_t rslt = BMI160_OK;
- uint8_t step_enable = 1;//enable the step counter
- rslt = bmi160_set_step_counter(step_enable, &sensor);
- if (rslt == BMI160_OK) {
- printf("\r\n User Space SUCCESS\r\n");
- } else {
- printf("\r\n User Space FAIL\r\n");
- }
- }
- void BMI160_ISR(void)
- {
- int8_t rslt = BMI160_OK;
- uint16_t step_count = 0;//stores the step counter value
- rslt = bmi160_read_step_counter(&step_count, &sensor);
- if (rslt == BMI160_OK) {
- printf("\r\n BMI160 ISR SUCCESS\r\n");
- } else {
- printf("\r\n BMI160 ISR FAIL\r\n");
- }
- }
- void BMI160_SelfTest(void)
- {
- s8 rslt;
- /* Call the "bmi160_init" API as a prerequisite before performing self test
- * since invoking self-test will reset the sensor */
- rslt = bmi160_perform_self_test(BMI160_ACCEL_ONLY, &sensor);
- /* Utilize the enum BMI160_GYRO_ONLY instead of BMI160_ACCEL_ONLY
- to perform self test for gyro */
- if (rslt == BMI160_OK) {
- printf("\r\n ACCEL SELF TEST RESULT SUCCESS\r\n");
- } else {
- printf("\r\n ACCEL SELF TEST RESULT FAIL\r\n");
- }
- }
复制代码附件是官方驱动。
BMI160_driver-master.zip
(44.38 KB, 下载次数: 28)
|
|