|
|
第1行: |
第1行: |
? | [[文件:02s03702.jpg|400px|縮略圖|右]]
| |
? | ==產(chǎn)品概述==
| |
? | ADXL345 數(shù)字三軸加速度計(jì)是一款小而薄的超低功耗3軸加速度計(jì),分辨率高達(dá)(13位),測(cè)量范圍達(dá)± 16g。數(shù)字輸出數(shù)據(jù)為16位二進(jìn)制補(bǔ)碼格式,可通過(guò)SPI(3線或4線)或I2C數(shù)字接口訪問(wèn)。ADXL345非常適合移動(dòng)設(shè)備應(yīng)用。它可以在傾斜檢測(cè)應(yīng)用中測(cè)量靜態(tài)重力加速度,還可以測(cè)量運(yùn)動(dòng)或沖擊導(dǎo)致的動(dòng)態(tài)加速度。其高分辨率(3.9mg/LSB),能夠測(cè)量不到1.0°的傾斜角度變化。該器件提供多種特殊檢測(cè)功能?;顒?dòng)和非活動(dòng)檢測(cè)功能通過(guò)比較任意軸上的加速度與用戶設(shè)置的閾值來(lái)檢測(cè)有無(wú)運(yùn)動(dòng)發(fā)生。敲擊檢測(cè)功能可以檢測(cè)任意方向的單振和雙振動(dòng)作。自由落體檢測(cè)功能可以檢測(cè)器件是否正在掉落。這些功能可以獨(dú)立映射到兩個(gè)中斷輸出引腳中的一個(gè)。正在申請(qǐng)專利的集成式存儲(chǔ)器管理系統(tǒng)采用一個(gè)32級(jí)先進(jìn)先出(FIFO)緩沖器,可用于存儲(chǔ)數(shù)據(jù),從而將主機(jī)處理器負(fù)荷降至最低,并降低整體系統(tǒng)功耗。低功耗模式支持基于運(yùn)動(dòng)的智能電源管理,從而以極低的功耗進(jìn)行閾值感測(cè)和運(yùn)動(dòng)加速度測(cè)量。
| |
? | ==規(guī)格參數(shù)==
| |
? | # 工作電壓:3.3-5v
| |
? | # 超低功耗:測(cè)量模式下40uA電流損耗,待機(jī)模式下0.1uA@2.5v
| |
? | # 通訊接口:I2C、SPI(3線or4線)
| |
? | # 接口類型:0.1"插針孔
| |
? | ==接口定義==
| |
? | * VCC:電源引腳
| |
? | * GND:該引腳必須接地
| |
? | * CS:片選端,低電平有效
| |
? | * INT1:中斷1輸出
| |
? | * INT2:中斷2輸出
| |
? | * SDO:備用I2C地址選擇
| |
? | * SDA:I2C接口數(shù)據(jù)端
| |
? | * SCL:I2C接口時(shí)鐘端
| |
? | ==使用方法==
| |
? | ===接線方法===
| |
? | [[文件:b1.png|423px|縮略圖|居中]]
| |
? | 接線圖:
| |
? | [[文件:Adxl345jiexian.jpg|700px|縮略圖|居中]]
| |
? | ===例子程序===
| |
? | [http://pan.baidu.com/s/1o6CI1ZG wire庫(kù)文件下載]
| |
? | <pre style='color:blue'>#include <Wire.h>
| |
| | | |
? | #define DEVICE (0x53) //ADXL345 device address
| |
? | #define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
| |
? |
| |
? | byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
| |
? | char str[512]; //string buffer to transform data before sending it to the serial port
| |
? | int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
| |
? | int x, y, z; //three axis acceleration data
| |
? | double roll = 0.00, pitch = 0.00; //Roll & Pitch are the angles which rotate by the axis X and y
| |
? | //in the sequence of R(x-y-z),more info visit
| |
? | void setup() {
| |
? | Wire.begin(); // join i2c bus (address optional for master)
| |
? | Serial.begin(9600); // start serial for output
| |
? |
| |
? | //Turning on the ADXL345
| |
? | writeTo(DEVICE, 0x2D, 0);
| |
? | writeTo(DEVICE, 0x2D, 16);
| |
? | writeTo(DEVICE, 0x2D, 8);
| |
? | }
| |
? |
| |
? | void loop() {
| |
? |
| |
? | readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
| |
? | //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
| |
? | //thus we are converting both bytes in to one int
| |
? | x = (((int)buff[1]) << 8) | buff[0];
| |
? | y = (((int)buff[3])<< 8) | buff[2];
| |
? | z = (((int)buff[5]) << 8) | buff[4];
| |
? | //we send the x y z values as a string to the serial port
| |
? | Serial.print("The acceleration info of x, y, z are:");
| |
? | sprintf(str, "%d %d %d", x, y, z);
| |
? | Serial.print(str);
| |
? | Serial.write(10);
| |
? | //Roll & Pitch calculate
| |
? | RP_calculate();
| |
? | Serial.print("Roll:"); Serial.println( roll );
| |
? | Serial.print("Pitch:"); Serial.println( pitch );
| |
? | Serial.println("");
| |
? | //It appears that delay is needed in order not to clog the port
| |
? | delay(50);
| |
? | }
| |
? |
| |
? | //---------------- Functions
| |
? | //Writes val to address register on device
| |
? | void writeTo(int device, byte address, byte val) {
| |
? | Wire.beginTransmission(device); //start transmission to device
| |
? | Wire.write(address); // send register address
| |
? | Wire.write(val); // send value to write
| |
? | Wire.endTransmission(); //end transmission
| |
? | }
| |
? |
| |
? | //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
| |
? | }
| |
? |
| |
? | //calculate the Roll&Pitch
| |
? | void RP_calculate(){
| |
? | double x_Buff = float(x);
| |
? | double y_Buff = float(y);
| |
? | double z_Buff = float(z);
| |
? | roll = atan2(y_Buff , z_Buff) * 57.3;
| |
? | pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
| |
? | }</pre>
| |
? | ===程序效果===
| |
? | 打開(kāi)串口監(jiān)視窗口,可以看到類似下圖的數(shù)據(jù),分別為:三軸加速度的數(shù)據(jù),按照R-xyz旋轉(zhuǎn)順序時(shí)的姿態(tài)角。按各軸旋轉(zhuǎn)可以觀測(cè)到相應(yīng)的數(shù)據(jù)變化。
| |