“(SKU:RB-02S037)ADXL345 三轴加速度计”的版本间的差异

来自ALSROBOT WiKi
跳转至: 导航搜索
相关推荐
例子程序
第40行: 第40行:
 
==例子程序==
 
==例子程序==
 
<pre style="color:blue">
 
<pre style="color:blue">
 
 
#include <Wire.h>
 
#include <Wire.h>
+
#define ADXLAddress (0x53)     //ADXL345的I2C地址(ADDR接地)
#define DEVICE (0x53)   //ADXL345 device address
+
int xAcc,  yAcc,  zAcc;      // 存放加速度值
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
+
int buff[6];                 //存放寄存器高低位值,X、Y、Z轴共6个
+
// 加速度传感器误差修正的偏移量
byte buff[TO_READ] ;   //6 bytes buffer for saving data read from the device
+
int a_offx = 0;
char str[512];                      //string buffer to transform data before sending it to the serial port
+
int a_offy = 0;
   
+
int a_offz = 0;
void setup()
+
void writeRegister(int deviceAddress, byte address, byte val)
 +
  {
 +
  Wire.beginTransmission(deviceAddress);
 +
  Wire.write(address);     
 +
  Wire.write(val);       
 +
  Wire.endTransmission();
 +
}
 +
void readRegister(int deviceAddress, byte address)  
 
{
 
{
   Wire.begin();       // join i2c bus (address optional for master)
+
   Wire.beginTransmission(deviceAddress);
   Serial.begin(9600); // start serial for output
+
  Wire.write(address);       
+
   Wire.endTransmission();  
   //Turning on the ADXL345
+
   Wire.beginTransmission(deviceAddress);
   writeTo(DEVICE, 0x2D, 0);    
+
   Wire.requestFrom(deviceAddress, 6);  
   writeTo(DEVICE, 0x2D, 16);
+
   int i = 0;
   writeTo(DEVICE, 0x2D, 8);
+
  while(Wire.available())   
 +
  {  buff[i++] = Wire.read(); }
 +
   Wire.endTransmission();  
 
}
 
}
+
void initAcc()  
void loop()
+
 
{
 
{
   int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
+
   //配置ADXL345,ADXL345采用默认的+-2g量程,10位分辨率
  int x, y, z;
+
writeRegister (ADXLAddress, 0x2C, 0x09);//设置输出数据速率50Hz,带宽25Hz。
+
//默认值为0x0A,对应输出数据速率100Hz,带宽50Hz
  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
+
writeRegister (ADXLAddress, 0x2D, 0x08); //设置ADXL345为测量模式。
+
  }
   //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
+
void getAccData()
   //thus we are converting both bytes in to one int
+
{
  x = (((int)buff[1]) << 8) | buff[0];   
+
   readRegister(ADXLAddress, 0x32);  
   y = (((int)buff[3])<< 8) | buff[2];
+
   xAcc = ((buff[1] << 8) | buff[0] )+ a_offx;   
   z = (((int)buff[5]) << 8) | buff[4];
+
   yAcc = ((buff[3] << 8) | buff[2] )+ a_offy;
+
   zAcc = ((buff[5] << 8) | buff[4]) + a_offz;
  //we send the x y z values as a string to the serial port
+
}
  sprintf(str, "%d %d %d", x, y, z)
+
void setup()
   Serial.print(str);
+
{
   Serial.write(10);
+
   Serial.begin(9600);
+
   Wire.begin();
   //It appears that delay is needed in order not to clog the port
+
   initAcc();
 
   delay(50);
 
   delay(50);
 
}
 
}
+
void loop()
//---------------- Functions
+
{
//Writes val to address register on device
+
    getAccData();
void writeTo(int device, byte address, byte val) {
+
    Serial.print("xAcc=");
  Wire.beginTransmission(device); //start transmission to device
+
    Serial.print(xAcc);
  Wire.write(address);       // send register address
+
    Serial.print(" yAcc=");
  Wire.write(val);       // send value to write
+
    Serial.print(yAcc);
  Wire.endTransmission(); //end transmission
+
     Serial.print(" zAcc=");
}
+
     Serial.println(zAcc);
   
+
  delay(200);
//reads num bytes starting from address register on device in to buff array
+
void readFrom(int device, byte address, int num, byte buff[]) {
+
  Wire.beginTransmission(device); //start transmission to device
+
  Wire.write(address);       //sends address to read from
+
  Wire.endTransmission(); //end transmission
+
+
     Wire.beginTransmission(device); //start transmission to device
+
  Wire.requestFrom(device, num);    // request 6 bytes from device
+
   
+
  int i = 0;
+
  while(Wire.available())    //device may send less than requested (abnormal)
+
  {
+
     buff[i] = Wire.read(); // receive a byte
+
    i++;
+
  }
+
  Wire.endTransmission(); //end transmission
+
 
}
 
}
 
 
</pre>
 
</pre>
  

2015年9月30日 (三) 10:35的版本


02s03702.jpg

目录

产品概述

ADXL345 数字三轴加速度计是一款小而薄的超低功耗3轴加速度计,分辨率高达(13位),测量范围达± 16g。数字输出数据为16位二进制补码格式,可通过SPI(3线或4线)或I2C数字接口访问。ADXL345非常适合移动设备应用。它可以在倾斜检测应用中测量静态重力加速度,还可以测量运动或冲击导致的动态加速度。其高分辨率(3.9mg/LSB),能够测量不到1.0°的倾斜角度变化。该器件提供多种特殊检测功能。活动和非活动检测功能通过比较任意轴上的加速度与用户设置的阈值来检测有无运动发生。敲击检测功能可以检测任意方向的单振和双振动作。自由落体检测功能可以检测器件是否正在掉落。这些功能可以独立映射到两个中断输出引脚中的一个。正在申请专利的集成式存储器管理系统采用一个32级先进先出(FIFO)缓冲器,可用于存储数据,从而将主机处理器负荷降至最低,并降低整体系统功耗。低功耗模式支持基于运动的智能电源管理,从而以极低的功耗进行阈值感测和运动加速度测量。

规格参数

  1. 工作电压:3.3-5v
  2. 超低功耗:测量模式下40uA电流损耗,待机模式下0.1uA@2.5v
  3. 通讯接口:I2C、SPI(3线or4线)
  4. 接口类型:0.1"插针孔

接线方法

ADXL345 Arduino
VCC 3V3
GND GND
CS 3V3
SDO GND
SDA A4
SCL A5


实物接线图:

Adxl345jiexian.jpg

例子程序

#include <Wire.h>
#define ADXLAddress (0x53)     //ADXL345的I2C地址(ADDR接地)
int xAcc,  yAcc,  zAcc;       // 存放加速度值
int buff[6];                  //存放寄存器高低位值,X、Y、Z轴共6个
// 加速度传感器误差修正的偏移量
int a_offx = 0;
int a_offy = 0;
int a_offz = 0;
void writeRegister(int deviceAddress, byte address, byte val)
 {
  Wire.beginTransmission(deviceAddress); 
  Wire.write(address);       
  Wire.write(val);        
  Wire.endTransmission();
} 
void readRegister(int deviceAddress, byte address) 
{
  Wire.beginTransmission(deviceAddress);  
  Wire.write(address);        
  Wire.endTransmission(); 
  Wire.beginTransmission(deviceAddress); 
  Wire.requestFrom(deviceAddress, 6);   
  int i = 0;
  while(Wire.available())    
  {  buff[i++] = Wire.read();  }
  Wire.endTransmission(); 
}
void initAcc() 
{
  //配置ADXL345,ADXL345采用默认的+-2g量程,10位分辨率
writeRegister (ADXLAddress, 0x2C, 0x09);//设置输出数据速率50Hz,带宽25Hz。
//默认值为0x0A,对应输出数据速率100Hz,带宽50Hz
writeRegister (ADXLAddress, 0x2D, 0x08);  //设置ADXL345为测量模式。
  } 
void getAccData()
{
  readRegister(ADXLAddress, 0x32);  
  xAcc = ((buff[1] << 8) | buff[0] )+ a_offx;   
  yAcc = ((buff[3] << 8) | buff[2] )+ a_offy;
  zAcc = ((buff[5] << 8) | buff[4]) + a_offz;
}
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  initAcc();
  delay(50);
}
void loop()
{
    getAccData();
    Serial.print("xAcc=");
    Serial.print(xAcc);
    Serial.print("  yAcc=");
    Serial.print(yAcc);
    Serial.print("  zAcc=");
    Serial.println(zAcc);
   delay(200);
}

产品相关推荐

Erweima.png

产品购买地址

Arduino ADXL345 三轴加速度计

周边产品推荐

Carduino UNO R3 控制器
V5.0传感器扩展板

相关问题解答

如何使用stm8s103f3单片机控制adxl345三轴加速度传感器

相关学习资料

视频: AS-4WD轮式机器人三轴加速度计操控功能演示
视频:Arduino ADXL345 三轴加速度计
奥松机器人技术论坛