本帖最后由 SS15 于 2012-2-21 11:04 编辑
原来想着ADXL345应该很简单,实际弄了一下才发现不是那么回事。345有I2C和SPI两种接法,I2C的SDO脚接不同的地方设备地址又不一样,简单说就是不同设计的模块需要不同的程序代码
简直就是坑害小白啊,不过总算搞定了。
谢谢版主
[code=cpp]#include "Wire.h"
#define DEVICE (0x53) // ADXL345 device address when the SDO pin (12) is grounded
// 推荐的I2C模式电气连接。器件的7位I2C地址是 0x53
void setup()
{
Serial.begin(115200); // start serial for output
Wire.begin(); // join i2c bus (address optional for master)
// Wake up the accelerometer
Wire.beginTransmission(DEVICE); // Start talking to the ADXL345 accelerometer on the SEN-10183 board: http://www.sparkfun.com/products/10183
Wire.send(0x2D); // The address on the accel we want to set: POWER_CTL
Wire.send(0x08); // The value on the address: Measure(8), START MEASUREMENT
Wire.endTransmission();
}
void loop()
{
// Ask the accel to send us it's XYZ values
Wire.beginTransmission(DEVICE); // Start talking to the ADXL345 accelerometer on the SEN-10183 board: http://www.sparkfun.com/products/10183
Wire.send(0x32); // The address on the accel we want to read
Wire.endTransmission();
// Receive the XYZ values
Wire.requestFrom(DEVICE, 6); // Request bytes from another device, Wire.requestFrom(address, quantity)
byte data[6];
for (int i=0;i<6 && Wire.available();i++)
{
data = Wire.receive();
}
Wire.endTransmission();
// Parse them
int x = data[0] | (((int)data[1])<<8);
int y = data[2] | (((int)data[3])<<8);
int z = data[4] | (((int)data[5])<<8);
char buf[100];
sprintf(buf, "x:%d, y:%d, z:%d\r\n", x,y,z);
Serial.print(buf);
delay(200);
}
[/code] |