在定时中断OnTimer()内调用MPU6050的getMotion6时,串口输出都会停下来,注释掉这个调用,只保留loop()中的getMotion6才能串口正常输出。
gy-521连接了VCC、GND、SCL、SDA。
请大侠赐教,问题出在哪里了?如何解决?
==================================
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <MsTimer2.h>
#define INTERVAL 1000
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
float Ax, Ay, Az;
void setup()
{
Serial.begin(9600);
InitMPU();
MsTimer2::set(INTERVAL, OnTimer);
MsTimer2::start();
}
void loop()
{
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
}
//初始化MPU6050
void InitMPU()
{
Wire.begin();
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void OnTimer()
{
Serial.println("------------");
//accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Ax=ax/16384.00;
Ay=ay/16384.00;
Az=az/16384.00;
Serial.print(atan(Ax/sqrt(Az*Az+Ay*Ay))*180/3.14);Serial.print(",");Serial.println(gy);
}
|