Arduino的ADXL345库文件(附下载链接)

Arduino的库文件用起来很方便,但是偏偏要用的几个器件没有,之前清洗过电脑,好多原来搜集的库文件都丢失了,为不再犯错,特在此备份,也共享给需要的朋友。

用到的芯片是328的芯片,A4、A5连接IIC。(PS:这个库文件是根据陀螺仪修改过来的,有改进意见请不吝赐教~)


ADXL345.cpp:

#include "ADXL345.h"
#include 
#include 

void ADXL345::enableDefault(void)
{
	writeReg(ADXL345_DATA_FORMAT, 0x00);//测量范围,正负16g,13位模式
	writeReg(ADXL345_BW_RATE, 0x0A);    //速率设定为100 参考pdf13页
	writeReg(ADXL345_POWER_CTL, 0x08);  //选择电源模式   参考pdf24页
	writeReg(ADXL345_INT_ENABLE, 0x80); //使能 DATA_READY 中断
	writeReg(ADXL345_OFSX, 0x00);       //X 偏移量 根据测试传感器的状态写入pdf29页
	writeReg(ADXL345_OFSY, 0x00);       //Y 偏移量 根据测试传感器的状态写入pdf29页
	writeReg(ADXL345_OFSZ, 0x00);       //Z 偏移量 根据测试传感器的状态写入pdf29页
}

// Writes a register
void ADXL345::writeReg(byte reg, byte value)
{
	Wire.beginTransmission(ADXL345_SLA_ADDR);
	Wire.write(reg);
	Wire.write(value);
	Wire.endTransmission();
}

// Reads a register
byte ADXL345::readReg(byte reg)
{
	byte value;

	Wire.beginTransmission(ADXL345_SLA_ADDR);
	Wire.write(reg);
	Wire.endTransmission();
	Wire.requestFrom(ADXL345_SLA_ADDR, 1);
	value = Wire.read();
	Wire.endTransmission();

	return value;
}

// Reads the 3 channels and stores them in vector g
void ADXL345::read()
{
	Wire.beginTransmission(ADXL345_SLA_ADDR);
	// assert the MSB of the address to get the gyro 
	// to do slave-transmit subaddress updating.
	Wire.write(ADXL345_DATAX0 | (1 << 7)); 
	Wire.endTransmission();
	Wire.requestFrom(ADXL345_SLA_ADDR, 6);

	while (Wire.available() < 6);

	uint8_t xla = Wire.read();
	uint8_t xha = Wire.read();
	uint8_t yla = Wire.read();
	uint8_t yha = Wire.read();
	uint8_t zla = Wire.read();
	uint8_t zha = Wire.read();

	a.x = xha << 8 | xla;
	a.y = yha << 8 | yla;
	a.z = zha << 8 | zla;
}
 
  

ADXL345.h:

#ifndef __ADXL345__
#define __ADXL345__

#include  // for byte data type

#define	ADXL345_SLA_ADDR        	0xA6>>1
#define ADXL345_DEVID		        0x00
#define ADXL345_THRESH_TAP	   	0x1D
#define ADXL345_OFSX		        0x1E
#define ADXL345_OFSY		        0x1F
#define ADXL345_OFSZ		        0x20
#define ADXL345_DUR			0x21
#define ADXL345_LATENT		      	0x22
#define ADXL345_WINDOW		      	0x23
#define ADXL345_THRESH_ACT	    	0x24
#define ADXL345_THRESH_INACT    	0x25
#define ADXL345_TIME_INACT	    	0x26
#define ADXL345_ACT_INACT_CTL	  	0x27
#define ADXL345_THRESH_FF	      	0x28
#define ADXL345_TIME_FF		      	0x29
#define ADXL345_TAP_AXES	      	0x2A
#define ADXL345_ACT_TAP_STATUS		0x2B
#define ADXL345_BW_RATE		      	0x2C
#define ADXL345_POWER_CTL	      	0x2D
#define ADXL345_INT_ENABLE	    	0x2E
#define ADXL345_INT_MAP		      	0x2F
#define ADXL345_INT_SOURCE	    	0x30
#define ADXL345_DATA_FORMAT	    	0x31
#define ADXL345_DATAX0        		0x32
#define ADXL345_DATAX1        		0x33
#define ADXL345_DATAY0        		0x34
#define ADXL345_DATAY1       		0x35
#define ADXL345_DATAZ0        		0x36
#define ADXL345_DATAZ1        		0x37
#define ADXL345_FIFO_CTL	      	0x38
#define ADXL345_FIFO_STATUS	    	0x39

#define ADXL345_Z_OFF  10
#define ADXL345_Y_OFF  -7
#define ADXL345_X_OFF  -9

//percent of 1g per LSB
#define ADXL345_Z_SCALE 0.004F
#define ADXL345_Y_SCALE 0.00389F
#define ADXL345_X_SCALE 0.003937F

class ADXL345
{
	public:
		typedef struct vector
		{
			float x, y, z;
		} vector;

		vector a; // gyro angular velocity readings

		void enableDefault(void);

		void writeReg(byte reg, byte value);
		byte readReg(byte reg);

		void read(void);

};


#endif



下载链接: ADXL345库文件

Arduino的ADXL345库文件(附下载链接)_第1张图片


你可能感兴趣的:(Arduino)