极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17949|回复: 6

如何使用Arduino Mega板I2C连接多个MPU9250 九轴传感器?

[复制链接]
发表于 2016-11-30 20:37:19 | 显示全部楼层 |阅读模式
由于项目的需要,需要连接三个MPU9250模块,模块支持I2C接口磁场MPU9250 9DOF 九轴/9轴姿态 加速度 陀螺仪 指南针磁场传感器   

由于项目比较大还有许多传感器,所以使用了Arduino Mega2560,使用扩展版可以直接连接I2C接口。
我计划使用这个扩展版
Grove - Mega Shield v1.2 Arduino mega扩展板   
  

[size=18.018px]数字引脚区还包括一些复用的功能:I2C(有三个),硬串口(Uart0 - Uart3),PWM(PWM2 - PWM11)以及ICSP。
[size=18.018px]但是不知道三个MPU9250模块接上去以后 怎么分别获取九轴数据!
MPU9250获取九轴的代码如下:
#include "Wire.h"


// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"


// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;


int16_t ax, ay, az;//加速度acceleration
int16_t gx, gy, gz;//陀螺仪显示参数gyroscope
int16_t mx, my, mz;//磁力计magnetometer


#define LED_PIN 13
bool blinkState = false;


void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();


    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(57600);


    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();


    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");


    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}


void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);


    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);


    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g/m:\t");
  //  Serial.print(ax); Serial.print("\t");
  //  Serial.print(ay); Serial.print("\t");
  //  Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.print(gz); Serial.print("\t");
   //Serial.print(mx); Serial.print("\t");
   // Serial.print(my); Serial.print("\t");
  //  Serial.println(mz);
  // Serial.();
    delay(500);
    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

//
在#include "MPU6050.h"头文件里面可以看到
// #define MPU6050_ADDRESS_AD0_LOW     0x68 // address pin low (GND), default for InvenSense evaluation board
#define MPU6050_ADDRESS_AD0_HIGH    0x69 // address pin high (VCC)
#define MPU6050_DEFAULT_ADDRESS     MPU6050_ADDRESS_AD0_LOW


在MPU6050.cpp里面可以看到
默认地址使用的是MPU6050_DEFAULT_ADDRESS
代码如下:


#include "MPU6050.h"

/** Default constructor, uses default I2C address.
* @see MPU6050_DEFAULT_ADDRESS
*/
MPU6050::MPU6050() {
    devAddr = MPU6050_DEFAULT_ADDRESS;
}

我该如何编写代码,才能分别获取三个九轴传感器的数据呢?
请教各位高手,不吝赐教!非常感谢!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-12-1 05:37:27 | 显示全部楼层

就你需要三個不同硬體位址的九軸感應器,然後調整讀寫位址就好啦
http://www.leaptronix.com/PDF/LA%20series%20I2C.pdf
回复 支持 反对

使用道具 举报

发表于 2016-12-1 15:30:07 | 显示全部楼层
多个一样的I2C传感器使用就是麻烦在I2C的地址分配上,这个还真不能随意改,按照使用手册只能同时使用两个9250,剩下还有一个用6050和hmc5883l组合起来代替吧。额,我在群里也看见你了,不是很精通,只能帮你到这里了
回复 支持 反对

使用道具 举报

发表于 2016-12-1 22:50:16 | 显示全部楼层
看了一下 MPU9250 只有一條位址線 AD0 所以只能有兩個~ 乖乖走 SPI 通訊吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-2 18:29:40 | 显示全部楼层
谢谢大家的回复 我去看看!!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-14 15:53:38 | 显示全部楼层
Dreamer-Men 发表于 2016-12-1 15:30
多个一样的I2C传感器使用就是麻烦在I2C的地址分配上,这个还真不能随意改,按照使用手册只能同时使用两个92 ...

谢谢您!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 18:59 , Processed in 0.048089 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表