两种访问I2C总线的方法有什么区别?



我看到了两种从I2C总线请求数据的不同方法:

方法一:

Wire.beginTransmission(MPU);
Wire.write(0x43); // Gyro data first register address 0x43
Wire.endTransmission(false);
//https://www.arduino.cc/en/Reference/WireEndTransmission
//false will send a restart, keeping the connection active. 
Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
GyroX = (Wire.read() << 8 | Wire.read()) / 131.0; // For a 250deg/s range we have to divide first the raw value by 131.0, according to the datasheet
GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;

方法2:

Wire.beginTransmission(0b1101000); //I2C address of the MPU //Accelerometer and Temperature reading (check 3.register map) 
Wire.write(0x3B); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,8); //Request Accel Registers (3B - 42)
while(Wire.available() < 8);
accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
temp = Wire.read()<<8| Wire.read();

似乎第一种方法没有终止传输,即Wire.endTransmission(false(,而第二种方法没有指定。 它们之间有什么区别?以及哪一个更好,即响应时间/循环时间。

另外,是否

0b1101000 and 0x3B(the register address for Accel in Method 2)

彼此平等?

一个是请求陀螺仪读数,其他加速度计读数,它们是不同的东西。 与您的另一个问题相同,0b1101000 是 MPU 的地址,而 0x3B 是加速度计寄存器地址(所以不一样(,您可能应该阅读您为此使用的库,或者如果您不使用任何库,您可以在 Arduino 库选项中找到一些(无论它叫什么,都有一段时间没有使用它了(。

如果您不想使用库,请阅读 MPU 的数据表以更好地理解。

根据它是 MPU6 系列、MPU9 系列还是其他制造商,情况会发生变化,但大多数已经构建了库,我建议您使用它们,除非您有兴趣从头开始工作,这可能会变得相当复杂非常快。

最新更新