我正在研究读取MPU6050陀螺仪数据,但主要问题是无法实现。让我解释一下为什么会发生此问题。
我找到了Arduino MPU6050BasicExample的MPU6050示例。
我已经开始更改基本的Arduino示例。然后我将所有代码应用到我的项目中,除了这些“ writeByte”,“ readByte”,“ readBytes”功能。
我试图按照Arduino的规定更改这些代码,如下所示:
这些是Arduino代码;
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.write(data); // Put data in Tx buffer
Wire.endTransmission(); // Send the Tx buffer
}
uint8_t readByte(uint8_t address, uint8_t subAddress)
{
uint8_t data; // `data` will store the register data
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
Wire.requestFrom(address, (uint8_t) 1); // Read one byte from slave register address
data = Wire.read(); // Fill Rx buffer with result
return data; // Return data read from slave register
}
void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
{
Wire.beginTransmission(address); // Initialize the Tx buffer
Wire.write(subAddress); // Put slave register address in Tx buffer
Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive
uint8_t i = 0;
Wire.requestFrom(address, count); // Read bytes from slave register address
while (Wire.available()) {
dest[i++] = Wire.read(); } // Put read results in the Rx buffer
}
这些是我尝试更改的代码;
void writeByte(uint16_t address, uint16_t subAddress, uint8_t data)
{
writeI2C0(address, subAddress, data);
}
uint8_t readByte(uint16_t address, uint16_t subAddress)
{
uint8_t data;
return readI2C0(address, subAddress);
}
void readBytes(uint16_t address, uint16_t subAddress, uint8_t count, uint8_t * dest)
{
writeByte(address, subAddress, count);
uint8_t i = 0;
while (I2CMasterBusy(I2C0_BASE)) {
dest[i++] = readByte(address, subAddress); }// Put read results in the Rx buffer
}
我认为,如果将Arduino代码移植到Tiva-c代码,则可以读取陀螺仪数据。