使用MPU9250获得加速,角速度及地磁计数据。

接线使用Arduino上的SCL接Mpu9250的SCL,SDA接SDA,3.3v或5v供电。

使用I2C连接,通过MPU9250不同地址,我们可以获得不同的数据,即加速度和角速度地址为0x68。

而地磁计地址为0x0C。

下面是代码: 

#include <Wire.h>
 
#define    MPU9250_ADDRESS            0x68
#define    MAG_ADDRESS                0x0C
 
#define    GYRO_FULL_SCALE_250_DPS    0x00  
#define    GYRO_FULL_SCALE_500_DPS    0x08
#define    GYRO_FULL_SCALE_1000_DPS   0x10
#define    GYRO_FULL_SCALE_2000_DPS   0x18
 
#define    ACC_FULL_SCALE_2_G        0x00  
#define    ACC_FULL_SCALE_4_G        0x08
#define    ACC_FULL_SCALE_8_G        0x10
#define    ACC_FULL_SCALE_16_G       0x18
 
 
 
// This function read Nbytes bytes from I2C device at address Address. 
// Put read bytes starting at register Register in the Data array. 
void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data)
{
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.endTransmission();
 
  // Read Nbytes
  Wire.requestFrom(Address, Nbytes); 
  uint8_t index=0;
  while (Wire.available())
    Data[index++]=Wire.read();
}
void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data)
{
  // Set register address
  Wire.beginTransmission(Address);
  Wire.write(Register);
  Wire.write(Data);
  Wire.endTransmission();
}
 
 
// Initializations
void setup()
{
  // Arduino initializations
  Wire.begin();
  Serial.begin(115200);
 
  // Configure gyroscope range
  I2CwriteByte(MPU9250_ADDRESS,27,GYRO_FULL_SCALE_2000_DPS);
  // Configure accelerometers range
  I2CwriteByte(MPU9250_ADDRESS,28,ACC_FULL_SCALE_16_G);
  // Set by pass mode for the magnetometers
  I2CwriteByte(MPU9250_ADDRESS,0x37,0x02);
 
  // Request first magnetometer single measurement
  I2CwriteByte(MAG_ADDRESS,0x0A,0x16);
 
 
}


long int cpt=0;
// Main loop, read and display data
void loop()
{
   // ____________________________________
  // :::  accelerometer and gyroscope ::: 
 
  // Read accelerometer and gyroscope
  uint8_t Buf[14];
  I2Cread(MPU9250_ADDRESS,0x3B,14,Buf);
 
  // Create 16 bits values from 8 bits data
 
  // Accelerometer
  int16_t ax=-(Buf[0]<<8 | Buf[1]);
  int16_t ay=-(Buf[2]<<8 | Buf[3]);
  int16_t az=Buf[4]<<8 | Buf[5];
 
  // Gyroscope
  int16_t gx=-(Buf[8]<<8 | Buf[9]);
  int16_t gy=-(Buf[10]<<8 | Buf[11]);
  int16_t gz=Buf[12]<<8 | Buf[13];
 
    // Display values
 
  // Accelerometer
//  Serial.print (ax,DEC); 
//  Serial.print ("\t");
//  Serial.print (ay,DEC);
//  Serial.print ("\t");
//  Serial.print (az,DEC);  
//  Serial.print ("\t");
// 
//  // Gyroscope
//  Serial.print (gx,DEC); 
//  Serial.print ("\t");
//  Serial.print (gy,DEC);
//  Serial.print ("\t");
//  Serial.print (gz,DEC);  
//  Serial.print ("\t");
  //delay(100); 


  


  // Read magnetometer data  
  I2CwriteByte(MAG_ADDRESS,0x37,0x02);
  uint8_t Mag[7];  
  I2Cread(MAG_ADDRESS,0x03,7,Mag);
  // Magnetometer
  int16_t mx=-(Mag[0]<<8 | Mag[1]);
  int16_t my=-(Mag[2]<<8 | Mag[3]);
  int16_t mz=-(Mag[4]<<8 | Mag[5]);
  
  
  // Magnetometer
  Serial.print (mx,DEC); 
  Serial.print ("\t");
  Serial.print (my,DEC);
  Serial.print ("\t");
  Serial.print (mz,DEC);  
  Serial.print ("\t");
  
  // End of line
  Serial.println("");
  //delay(100);    
}
View Code

相关文章:

  • 2021-06-19
  • 2021-05-18
  • 2021-06-10
  • 2021-11-28
  • 2021-11-28
  • 2021-12-06
  • 2021-05-15
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2021-10-16
  • 2022-02-17
  • 2022-01-22
  • 2021-10-05
  • 2022-12-23
  • 2021-06-16
相关资源
相似解决方案