5681|1

650

帖子

8

TA的资源

纯净的硅(初级)

楼主
 

Max32630(五)——BMI160传感器 [复制链接]

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操作。驱动部分:
  1. #include "bmi160_i2c.h"
  2. #include "i2cm.h"

  3. const sys_cfg_i2cm_t bmi160_sys_cfg = {
  4.         .clk_scale = CLKMAN_SCALE_AUTO,
  5.         .io_cfg = IOMAN_I2CM2(IOMAN_MAP_A, 1)
  6. };

  7. struct bmi160_dev sensor;
  8. struct bmi160_cfg prev_accel_cfgure;
  9. struct bmi160_cfg prev_gyro_cfgure;
  10. struct bmi160_aux_cfg aux_cfgure;
  11. struct bmi160_aux_cfg prev_aux_cfgure;

  12. struct bmi160_cfg accel_cfgure = {
  13.         .power = BMI160_ACCEL_NORMAL_MODE,
  14.         .odr = BMI160_ACCEL_ODR_1600HZ,
  15.         .range = BMI160_ACCEL_RANGE_2G,
  16.         .bw = BMI160_ACCEL_BW_NORMAL_AVG4,
  17. };



  18. struct bmi160_cfg gyro_cfgure = {
  19.         .power = BMI160_GYRO_NORMAL_MODE,
  20.         .odr = BMI160_GYRO_ODR_3200HZ,
  21.         .range = BMI160_GYRO_RANGE_2000_DPS,
  22.         .bw = BMI160_GYRO_BW_NORMAL_MODE,
  23. };








  24. #define BMI160_I2CM       MXC_I2CM2

  25. void BMI160_Init(void)
  26. {
  27.         uint8_t ui8Status = 0;
  28.     uint8_t ui8Attempts = 20;
  29.         s8 rslt = BMI160_OK;
  30.         u8 temp = 0;
  31. //        u8 *temp = NULL;
  32. //        u8 reg_adr = 0;
  33.         

  34.         sensor.id = BMI160_I2C_ADDR;               
  35.         sensor.interface = BMI160_I2C_INTF;        /*! 0 - I2C , 1 - SPI Interface */
  36.         sensor.read = bmi_i2c_read;
  37.         sensor.write = bmi_i2c_write;
  38.         sensor.delay_ms = delay_ms;
  39.         rslt = I2CM_Init(BMI160_I2CM, &bmi160_sys_cfg, I2CM_SPEED_400KHZ);
  40.         

  41.         rslt = bmi160_init(&sensor);                //Init
  42.         if(BMI160_OK == rslt)
  43.                 printf("BMI160 Init OK!\r\n");
  44.         else
  45.                 printf("BMI160 Init Erro: %d!\r\n",rslt);
  46.         
  47.         BMI160_Set_PowerMode();
  48.         

  49.         /* Select the Output data rate, range of accelerometer sensor */
  50.         sensor.accel_cfg.odr = BMI160_ACCEL_ODR_800HZ;
  51.         sensor.accel_cfg.range = BMI160_ACCEL_RANGE_4G;
  52.         sensor.accel_cfg.bw = BMI160_ACCEL_BW_OSR2_AVG2;

  53.         /* Select the power mode of accelerometer sensor */
  54.         sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;

  55.         /* Select the Output data rate, range of Gyroscope sensor */
  56. //        sensor.gyro_cfg.odr = BMI160_GYRO_ODR_3200HZ;
  57. //        sensor.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
  58. //        sensor.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;

  59. //        /* Select the power mode of Gyroscope sensor */
  60. //        sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;

  61.         /* Set the sensor configuration */
  62.         rslt = bmi160_set_sens_conf(&sensor);
  63.         if(BMI160_OK == rslt)
  64.                 printf("BMI160 Conf OK!\r\n");
  65.         else
  66.                 printf("BMI160 Conf Erro: %d!\r\n",rslt);
  67.         

  68.         
  69.         
  70. }
  71. int8_t bmi_i2c_read(uint8_t dev_addr, uint8_t reg_addr,uint8_t *data, uint16_t len)
  72. {
  73.         if( I2CM_Read(BMI160_I2CM, dev_addr, ®_addr, 1, data, len) < 0)
  74.                 return BMI160_E_DEV_NOT_FOUND;
  75.         
  76.         return BMI160_OK;
  77. }

  78. int8_t bmi_i2c_write(uint8_t dev_addr, uint8_t reg_addr,uint8_t *data, uint16_t len)
  79. {
  80.         if(I2CM_Write(BMI160_I2CM, dev_addr, ®_addr, 1, data, len)<0)
  81.                 return BMI160_E_DEV_NOT_FOUND;
  82.         return BMI160_OK;

  83.         
  84. }

复制代码
读取数据部分:
  1. #include "bmi160_app.h"
  2. #include "bmi160_i2c.h"


  3. //struct bmi160_sensor_data accel_data;
  4. //struct bmi160_sensor_data gyro_data;

  5. struct bmi160_sensor_data accel;
  6. struct bmi160_sensor_data gyro;

  7. void BMI160_Task(void)
  8. {
  9.         u8 temp = BMI160_ACCEL_NORMAL_MODE;
  10.         u16 Step = 0;
  11.         static u32 PointM = 0;
  12.         static u8 mState = 0;
  13.         if( ( OsDelayCCnt - PointM ) >= T_1S)
  14.         {
  15.                 PointM = OsDelayCCnt;
  16.                
  17.                 int8_t rslt = BMI160_OK;
  18.                
  19.                 bmi160_read_step_counter(&User.StepCount,&sensor);

  20.                 /* To read only Accel data */
  21.                 rslt = bmi160_get_sensor_data(BMI160_ACCEL_SEL, &accel, NULL, &sensor);
  22.                
  23.                 /* To read only Gyro data */
  24.                 rslt = bmi160_get_sensor_data(BMI160_GYRO_SEL, NULL, &gyro, &sensor);
  25.                
  26.                 /* To read both Accel and Gyro data */
  27.                 bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL), &accel, &gyro, &sensor);

  28.                 /* To read Accel data along with time */
  29.                 rslt = bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_TIME_SEL) , &accel, NULL, &sensor);
  30.                
  31.                 /* To read Gyro data along with time */
  32.                 rslt = bmi160_get_sensor_data((BMI160_GYRO_SEL | BMI160_TIME_SEL), NULL, &gyro, &sensor);

  33.                 /* To read both Accel and Gyro data along with time*/
  34.                 bmi160_get_sensor_data((BMI160_ACCEL_SEL | BMI160_GYRO_SEL | BMI160_TIME_SEL), &accel, &gyro, &sensor);

  35.         }
  36.         
  37.         
  38. }

  39. void BMI160_Set_PowerMode(void)
  40. {
  41.         int8_t rslt = BMI160_OK;

  42.         /* Select the power mode */
  43.         sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
  44.         sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;

  45.         /*  Set the Power mode  */
  46.         rslt = bmi160_set_power_mode(&sensor);
  47.         if (rslt == BMI160_OK) {
  48.                 printf("\r\n Set PowerMode SUCCESS\r\n");
  49.         } else {
  50.                 printf("\r\n Set PowerMode FAIL\r\n");
  51.         }
  52. }

  53. void BMI160_Reset(void)
  54. {
  55.         int8_t rslt = BMI160_OK;
  56.         rslt = bmi160_soft_reset(&sensor);
  57.         if (rslt == BMI160_OK) {
  58.                 printf("\r\n Reset SUCCESS\r\n");
  59.         } else {
  60.                 printf("\r\n Reset FAIL\r\n");
  61.         }
  62. }
  63. void BMI160_Set_StepInt(void)
  64. {
  65.         struct bmi160_int_settg int_config;

  66.         /* Select the Interrupt channel/pin */
  67.         int_config.int_channel = BMI160_INT_CHANNEL_1;// Interrupt channel/pin 1

  68.         /* Select the Interrupt type */
  69.         int_config.int_type = BMI160_STEP_DETECT_INT;// Choosing Step Detector interrupt

  70.         /* Select the interrupt channel/pin settings */
  71.         int_config.int_pin_settg.output_en = BMI160_ENABLE;// Enabling interrupt pins to act as output pin
  72.         int_config.int_pin_settg.output_mode = BMI160_DISABLE;// Choosing push-pull mode for interrupt pin
  73.         int_config.int_pin_settg.output_type = BMI160_ENABLE;// Choosing active High output
  74.         int_config.int_pin_settg.edge_ctrl = BMI160_ENABLE;// Choosing edge triggered output
  75.         int_config.int_pin_settg.input_en = BMI160_DISABLE;// Disabling interrupt pin to act as input
  76.         int_config.int_pin_settg.latch_dur =BMI160_LATCH_DUR_NONE;// non-latched output

  77.         /* Select the Step Detector interrupt parameters, Kindly use the recommended settings for step detector */
  78.         int_config.int_type_cfg.acc_step_detect_int.step_detector_mode = BMI160_STEP_DETECT_NORMAL;
  79.         int_config.int_type_cfg.acc_step_detect_int.step_detector_en = BMI160_ENABLE;// 1-enable, 0-disable the step detector

  80.         /* Set the Step Detector interrupt */
  81.         bmi160_set_int_config(&int_config, &sensor); /* sensor is an instance of the structure bmi160_dev */
  82.         
  83. }

  84. void BMI160_Any_Motion_Interrupt(void)
  85. {
  86.         struct bmi160_int_settg int_config;

  87.         /* Select the Interrupt channel/pin */
  88.         int_config.int_channel = BMI160_INT_CHANNEL_1;// Interrupt channel/pin 1

  89.         /* Select the Interrupt type */
  90.         int_config.int_type = BMI160_ACC_ANY_MOTION_INT;// Choosing Any motion interrupt

  91.         /* Select the interrupt channel/pin settings */
  92.         int_config.int_pin_settg.output_en = BMI160_ENABLE;// Enabling interrupt pins to act as output pin
  93.         int_config.int_pin_settg.output_mode = BMI160_DISABLE;// Choosing push-pull mode for interrupt pin
  94.         int_config.int_pin_settg.output_type = BMI160_DISABLE;// Choosing active low output
  95.         int_config.int_pin_settg.edge_ctrl = BMI160_ENABLE;// Choosing edge triggered output
  96.         int_config.int_pin_settg.input_en = BMI160_DISABLE;// Disabling interrupt pin to act as input
  97.         int_config.int_pin_settg.latch_dur = BMI160_LATCH_DUR_NONE;// non-latched output

  98.         /* Select the Any-motion interrupt parameters */
  99.         int_config.int_type_cfg.acc_any_motion_int.anymotion_en = BMI160_ENABLE;// 1- Enable the any-motion, 0- disable any-motion
  100.         int_config.int_type_cfg.acc_any_motion_int.anymotion_x = BMI160_ENABLE;// Enabling x-axis for any motion interrupt
  101.         int_config.int_type_cfg.acc_any_motion_int.anymotion_y = BMI160_ENABLE;// Enabling y-axis for any motion interrupt
  102.         int_config.int_type_cfg.acc_any_motion_int.anymotion_z = BMI160_ENABLE;// Enabling z-axis for any motion interrupt
  103.         int_config.int_type_cfg.acc_any_motion_int.anymotion_dur = 0;// any-motion duration
  104.         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

  105.         /* Set the Any-motion interrupt */
  106.         bmi160_set_int_config(&int_config, &sensor); /* sensor is an instance of the structure bmi160_dev  */
  107. }

  108. void BMI160_User_Space(void)
  109. {
  110.         int8_t rslt = BMI160_OK;
  111.         uint8_t step_enable = 1;//enable the step counter

  112.         rslt = bmi160_set_step_counter(step_enable,  &sensor);
  113.         if (rslt == BMI160_OK) {
  114.                 printf("\r\n User Space SUCCESS\r\n");
  115.         } else {
  116.                 printf("\r\n User Space FAIL\r\n");
  117.         }
  118. }
  119. void BMI160_ISR(void)
  120. {
  121.         int8_t rslt = BMI160_OK;
  122.         uint16_t step_count = 0;//stores the step counter value

  123.         rslt = bmi160_read_step_counter(&step_count,  &sensor);
  124.         if (rslt == BMI160_OK) {
  125.                 printf("\r\n BMI160 ISR SUCCESS\r\n");
  126.         } else {
  127.                 printf("\r\n BMI160 ISR FAIL\r\n");
  128.         }
  129. }

  130. void BMI160_SelfTest(void)
  131. {
  132.         s8 rslt;
  133.         /* Call the "bmi160_init" API as a prerequisite before performing self test
  134. * since invoking self-test will reset the sensor */

  135.         rslt = bmi160_perform_self_test(BMI160_ACCEL_ONLY, &sensor);
  136.         /* Utilize the enum BMI160_GYRO_ONLY instead of BMI160_ACCEL_ONLY
  137.            to perform self test for gyro */
  138.         if (rslt == BMI160_OK) {
  139.                 printf("\r\n ACCEL SELF TEST RESULT SUCCESS\r\n");
  140.         } else {
  141.                 printf("\r\n ACCEL SELF TEST RESULT FAIL\r\n");
  142.         }
  143. }



复制代码
附件是官方驱动。 BMI160_driver-master.zip (44.38 KB, 下载次数: 28)



点赞 关注
 

回复
举报

650

帖子

8

TA的资源

纯净的硅(初级)

沙发
 
更正一个错误,IIC的发送函数有错,
int8_t bmi_i2c_write(uint8_t dev_addr, uint8_t reg_addr,uint8_t *data, uint16_t len)
{
        u8 temp_data[3];
        temp_data[0] = reg_addr;
        temp_data[1] = *data;
        if(I2CM_Write(BMI160_I2CM, dev_addr, NULL, 0, temp_data, 2) != 2)
                return BMI160_E_DEV_NOT_FOUND;
        return BMI160_OK;
       
       
}
用这个才可以正确的写BMI160的寄存器
原因是:
直接调用I2CM_Write函数先写了reg后不会发送stop位
 
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表