我正在尝试使用能量将ADXL 345与tm4c1294nctpd配对,
引脚的配置是下一个
使用J1(3.3V)和J21(5V)以及GND的PB3引脚的SDA和PB2的SCL(I2C0端口)。
在ergya的串行监控下,输出为x = 0,y = 0,z = 0,
,电路在SDA和SCL引脚中具有上拉电阻,
传感器的地址为0x53。我了解我正在将该设备作为I2C协议的从设备(Wire.begin(ADXAddress);)
该代码是
#include "Wire.h"
#define Power_Register 0x2D
#define BW_rate 0x2C
#define Int_enable 0x2E
#define dataFormat 0x31
#define X_Axis_Register_DATAX0 0x32
#define X_Axis_Register_DATAX1 0x33
#define Y_Axis_Register_DATAY0 0x34
#define Y_Axis_Register_DATAY1 0x35
#define Z_Axis_Register_DATAZ0 0x36
#define Z_Axis_Register_DATAZ1 0x37
#define fifoCntrl 0x38
int ADXAddress = 0x53;
int X0,X1,X_out;
int Y0,Y1,Y_out;
int Z1,Z0,Z_out;
float Xa,Ya,Za;
void setup() {
Wire.setModule(0);
Wire.begin(ADXAddress);
Serial.begin(9600);
delay(100);
Wire.beginTransmission(ADXAddress);
Wire.write(Power_Register);
Wire.write(8);
Wire.write(BW_rate);
Wire.write(10);
Wire.write(dataFormat);
Wire.write(11);
Wire.write(fifoCntrl);
Wire.write(0);
Wire.endTransmission();
}
void loop() {
Wire.beginTransmission(ADXAddress);
Wire.write(X_Axis_Register_DATAX0);
Wire.write(X_Axis_Register_DATAX1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress,2);
if(Wire.available()<=2) {
X0 = Wire.read();
X1 = Wire.read();
X1=X1<<8;
X_out =X0+X1;
}
Wire.beginTransmission(ADXAddress);
Wire.write(Y_Axis_Register_DATAY0);
Wire.write(Y_Axis_Register_DATAY1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress,2);
if(Wire.available()<=2) {
Y0 = Wire.read();
Y1 = Wire.read();
Y1=Y1<<8;
Y_out =Y0+Y1;
}
Wire.beginTransmission(ADXAddress);
Wire.write(Z_Axis_Register_DATAZ0);
Wire.write(Z_Axis_Register_DATAZ1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress,2);
if(Wire.available()<=2) {
Z0 = Wire.read();
Z1 = Wire.read();
Z1=Z1<<8;
Z_out =Z0+Z1;
}
Serial.print("Xa= ");
Serial.print(Xa);Serial.print("\t");
Serial.print("Ya= ");
Serial.print(Ya);Serial.print("\t");
Serial.print("Za= ");
Serial.println(Za);
delay(1000);
Serial.println();
}
最佳答案
如果要写入随机I2C寄存器,则需要重新开始传输。只需创建一个函数即可简化此过程。
void writeReg ( int reg, int value )
{
Wire.beginTransmission(ADXAddress);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
void setup()
{
Wire.setModule(0);
Wire.begin(ADXAddress);
Serial.begin(9600);
delay(100);
writeReg(Power_Register,8);
writeReg(BW_rate,10); // <-- should this be 0x10 ?
writeReg(dataFormat,11); // <-- should this be 0x11 ?
writeReg(fifoCntrl,0); // <-- this isn't strictly needed as fifoCntrl will be 0 at power on reset anyway
}
其次,您使用的教程(我已经在网上看到)实际上不是100%正确的。读取寄存器时,它只能写入一个字节-读取前不能写入两个字节,一次只能设置寄存器指针。简化代码以提供另一个阅读功能,以使代码更易于阅读。
int readAxis ( int reg )
{
Wire.beginTransmission(ADXAddress);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(ADXAddress,2);
int d0 = Wire.read();
int d1 = Wire.read();
return (d1<<8) + d0;
}
void loop ()
{
X_out = readAxis(X_Axis_Register_DATAX0); // this will read TWO bytes starting from X0 register (LSB + MSB)
Y_out = readAxis(X_Axis_Register_DATAY0);
Z_out = readAxis(X_Axis_Register_DATAZ0);
:
第三,您实际上并没有打印任何内容,因为您忘记设置Xa,Ya和Za值
:
Xa = (float)X_out/RANGE;
Ya = (float)Y_out/RANGE;
Za = (float)Z_out/RANGE;
Serial.print("Xa= ");
Serial.print(Xa);Serial.print("\t");
Serial.print("Ya= ");
Serial.print(Ya);Serial.print("\t");
Serial.print("Za= ");
Serial.println(Za);
您需要根据所选ADXL输出的满量程(以G为单位)定义
RANGE
。请检查您的操作模式下的ADXL数据表(即2 G或16 G操作等...每个的满量程值是多少?)#define ADXL_FULLSCALE 4096 // 2^(13 bits- 1 signed bit)
#define ADXL_FULLSCALE_G 16 // +/- 16 G operation
#define RANGE (ADXL_FULLSCALE /ADXL_FULLSCALE_G )
关于c - ADXL345只读ceros,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58584996/