|
我是按照I2C链接的,代码用的是这个:
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百六十三:BMI160 6轴惯性运动传感器 16位3轴加速度+超低功耗3轴陀螺仪 I2C/SPI 14LGA
项目之三:读取陀螺仪数据
实验接线:
BMI160 UNO
VIN 5V
GND GND
SCL A5
SDA A4
SAO GND
INT1 D2
*/
#include <BMI160Gen.h>
void setup() {
Serial.begin(9600); // initialize Serial communication
while (!Serial); // wait for the serial port to open
// initialize device
Serial.println("Initializing IMU device...");
//BMI160.begin(BMI160GenClass::SPI_MODE, /* SS pin# = */10);
BMI160.begin(BMI160GenClass::I2C_MODE);
uint8_t dev_id = BMI160.getDeviceID();
Serial.print("DEVICE ID: ");
Serial.println(dev_id, HEX);
// Set the accelerometer range to 250 degrees/second
BMI160.setGyroRange(250);
Serial.println("Initializing IMU device...done.");
}
void loop() {
int gxRaw, gyRaw, gzRaw; // raw gyro values
float gx, gy, gz;
// read raw gyro measurements from device
BMI160.readGyro(gxRaw, gyRaw, gzRaw);
// convert the raw gyro data to degrees/second
gx = convertRawGyro(gxRaw);
gy = convertRawGyro(gyRaw);
gz = convertRawGyro(gzRaw);
// display tab-separated gyro x/y/z values
Serial.print("g:\t");
Serial.print(gx);
Serial.print("\t");
Serial.print(gy);
Serial.print("\t");
Serial.print(gz);
Serial.println();
delay(50);
}
float convertRawGyro(int gRaw) {
// since we are using 250 degrees/seconds range
// -250 maps to a raw value of -32768
// +250 maps to a raw value of 32767
float g = (gRaw * 250.0) / 32768.0;
return g;
} |
|