在Arduino环境中实例化类时串行通信不起作用

时间:2014-12-24 15:00:29

标签: c++ arduino

我试图为我的Arduino Mega编写代码,使用c ++样式库与ADXL345加速度计进行通信。 这是我的Accelerometer.h文件:

#include <Wire.h>
#define ADXL345 (0x53) // I2C Address of ADXL345 accelerometer

struct Acceleration
{
    float x;
    float y;
    float z;
};

class Accelerometer
{
  public:
    Accelerometer(void);              // Constructor
    Acceleration readData(void);                  // Read sensor data
  private:
    char buffer[6];   // Buffer to store data (x, y, z: LSB and MSB of each)
    char DATA_FORMAT; // Address of DATA_FORMAT Register
    char POWER_CTL;   // Address of POWER_CTL Register
    char DATAX0;      // Address of X-Axis LSB Data
    char DATAX1;      // Address of X-Axis MSB Data
    char DATAY0;      // Address of Y-Axis LSB Data
    char DATAY1;      // Address of Y-Axis MSB Data
    char DATAZ0;      // Address of Z-Axis LSB Data
    char DATAZ1;      // Address of Z-Axis MSB Data
    void writeToAccelerometer(char address, char value);
    void readFromAccelerometer(char address, int numOfBytes);
};

这是我的Accelerometer.cpp文件:

#include "Accelerometer.h"
#include <Wire.h>

Accelerometer::Accelerometer()
{
  Wire.begin();                                // Initialize I2C bus
  writeToAccelerometer(DATA_FORMAT, 0x01);     // +/- 4g range 
  writeToAccelerometer(POWER_CTL, 0x08);       // Measurement Mode

  DATA_FORMAT = 0x31;      // Address of DATA_FORMAT Register
  POWER_CTL   = 0x2D;      // Address of POWER_CTL Register
  DATAX0      = 0x32;      // Address of X-Axis LSB Data
  DATAX1      = 0x33;      // Address of X-Axis MSB Data
  DATAY0      = 0x34;      // Address of Y-Axis LSB Data
  DATAY1      = 0x35;      // Address of Y-Axis MSB Data
  DATAZ0      = 0x36;      // Address of Z-Axis LSB Data
  DATAZ1      = 0x37;      // Address of Z-Axis MSB Data
}

void Accelerometer::writeToAccelerometer(char address, char value) 
{
  Wire.beginTransmission(ADXL345); // start transmission to ADXL345 
  Wire.write(address);             // send register address
  Wire.write(value);               // send value to write
  Wire.endTransmission();          // end transmission
}

void Accelerometer::readFromAccelerometer(char address, int numOfBytes)
{
  Wire.beginTransmission(ADXL345); // start transmission to ADXL345 
  Wire.write(address);             // send register address
  Wire.endTransmission();          // end transmission

  Wire.beginTransmission(ADXL345);          // start transmission to ADXL345
  Wire.requestFrom(ADXL345, numOfBytes);    // request some bytes from device

  int i = 0;
  while(Wire.available())           // while there is data
  { 
    buffer[i] = Wire.read();        // receive a byte and save it in the buffer
    i++;
  }
  Wire.endTransmission();           // end transmission
}

Acceleration Accelerometer::readData()
{
  Acceleration R;
  readFromAccelerometer(DATAX0, 6);          // Read data from sensor

  // Merge of data and conversion to int format
  int x_acceleration = (((int)buffer[1]) << 8) | buffer[0];
  int y_acceleration = (((int)buffer[3]) << 8) | buffer[2];
  int z_acceleration = (((int)buffer[5]) << 8) | buffer[4];

  R.x = x_acceleration*0.0078;
  R.y = x_acceleration*0.0078;
  R.z = x_acceleration*0.0078;

  return R;
}

我使用此库在我的Arduino草图中使用此代码:

#include <Wire.h>
#include "Accelerometer.h"

Accelerometer accel;
Acceleration Racc;

void setup()
{
  Serial.begin(9600);   // Start serial for outbut at 9600 bps
}

void loop()
{
  Racc = accel.readData();

  // Print data
  Serial.print("ACCELERATION - X: ");
  Serial.print( Racc.x );
  Serial.print(" G / Y: ");
  Serial.print( Racc.y );
  Serial.print(" G / Z: ");
  Serial.print( Racc.z );
  Serial.print(" G\n");

  delay(100);
}

此代码编译没有错误。但是,由于Accelerometer类的实例化,串行通信无法正常工作(我无法在串行监视器中看到任何文本)。当我删除代码的类实例(只允许代码中的串行通信)时,我可以看到串行监视器中的打印内容。

有没有人知道发生了什么?我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

由于ADXL345可以运行SPI或I2C,因此需要对其进行配置。在您尝试将其用作I2C的情况下,ADXL345的CS需要连接到3.3V

  

如果CS引脚连接到VDD I / O,则使能I2C模式。 CS引脚   应始终连接到VDD I / O高电平或由外部驱动   控制器,因为如果CS引脚被保留,则存在节点故障模式   悬空。因此,不采取这些预防措施可能会导致   无法与部分沟通。

在上面的代码中,我没有看到任何引脚被配置为驱动CS的输出。

根据我的经验,当I2C(wire.h)库未正确连接到设备时,它可能会阻塞。这与您描述的症状类似。


您可能希望引用您的代码,例如https://github.com/jenschr/Arduino-libraries/tree/master/ADXL345。这似乎完全支持ADXL345基于I2C的许多功能