【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十三:BMI160 6轴惯性运动传感器 16位3轴加速度+超低功耗3轴陀螺仪 I2C/SPI 14LGA
项目之九:低功耗模式计步器
通过 setStepPowerMode 设置计步器功耗模式(stepNormalPowerMode 表示正常模式,stepLowPowerMode 表示低功耗模式)
实验开源代码
- /*
- 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验一百六十三:BMI160 6轴惯性运动传感器 16位3轴加速度+超低功耗3轴陀螺仪 I2C/SPI 14LGA
- 项目之九:低功耗模式计步器
- I2C地址:
- 0x68:将 BMI160 的 SDIO 引脚连接到 GND,即默认 I2C 地址
- 实验接线:
- BMI160 UNO
- VIN 5V
- GND GND
- SCL A5
- SDA A5
- SAO GND
- INT1 D2
- */
-
- #include <DFRobot_BMI160.h>
-
- DFRobot_BMI160 bmi160;
- const int8_t i2c_addr = 0x68;
- bool readStep = false;
-
- #if defined ARDUINO_AVR_UNO || defined ARDUINO_AVR_MEGA2560 || defined ARDUINO_AVR_PRO
- //uno和mega2560的中断号为0
- int pbIn = 2;
- #elif ARDUINO_AVR_LEONARDO
- //uno和leonardo的中断号为0
- int pbIn = 3;
- #else
- int pbIn = 13;
- #endif
- //bmi160 有两个中断接口
- int int1 = 1;
- int int2 = 2;
-
- void stepChange() {
- //一旦改变了步进控制器,就可以读取该值
- readStep = true;
- }
-
- void setup() {
- Serial.begin(115200);
- delay(100);
-
- //设置并初始化 bmi160 i2c 地址
- while (bmi160.I2cInit(i2c_addr) != BMI160_OK) {
- Serial.println("i2c init fail");
- delay(1000);
- }
-
- //将中断号设置为 int1 或 int2
- if (bmi160.setInt(int1) != BMI160_OK) {
- Serial.println("set interrput fail");
- while (1);
- }
-
- //将 bmi160 模式设置为计步器
- if (bmi160.setStepCounter() != BMI160_OK) {
- Serial.println("set step fail");
- while (1);
- }
-
- //设置 bmi160 电源模型(微功耗电源模式)
- if (bmi160.setStepPowerMode(bmi160.stepLowPowerMode) != BMI160_OK) {
- Serial.println("set setStepPowerMode fail");
- while (1);
- }
-
- #if defined ARDUINO_AVR_UNO || defined ARDUINO_AVR_MEGA2560 || defined ARDUINO_AVR_LEONARDO || defined ARDUINO_AVR_PRO
- //将板上的引脚设置为连接到 bmi160 的 int1 或 int2
- attachInterrupt(digitalPinToInterrupt(pbIn), stepChange, FALLING);
- #else
- attachInterrupt(pbIn, stepChange, FALLING);
- #endif
- Serial.println(pbIn);
- }
-
- void loop() {
- if (readStep) {
- uint16_t stepCounter = 0;
- //从硬件 bmi160 读取计步器
- if (bmi160.readStepCounter(&stepCounter) == BMI160_OK) {
- Serial.print("计步器 = "); Serial.println(stepCounter);
- }
- readStep = false;
- }
- }
复制代码
|