Exemple #1
0
float get_acel_value(Axis_Op axis, ADXL_Addr_Dev dev){
	status_code_t result;
	float acel_value = -16000;
	uint16_t adxl = 0;
	uint8_t b[2];
	memset(b, 0, sizeof(b));
	
	switch (axis) {
		case Axis_X:
		result = adxl_read(dev, b, ADXL_DataX0, sizeof(b));
		break;
		case Axis_Y:
		result = adxl_read(dev, b, ADXL_DataY0, sizeof(b));
		break;
		case Axis_Z:
		result = adxl_read(dev, b, ADXL_DataZ0, sizeof(b));
		break;
	}
	
	if (result != STATUS_OK){
		return acel_value;
	}
	
	adxl = (uint16_t)( (b[1] << 8) | b[0] );
	
	if ( !(adxl & 0xFC00) ){
		acel_value = CONST_ADXL * ((float) adxl);
		} else {
		adxl = ( ( (~adxl) +1 ) & 0x03FF);
		acel_value = -(CONST_ADXL * ((float) adxl) );
	}
	
	return acel_value;
}
int main(int argc, char* argv[])
{
	int fd;
	int ret = 0;
	signed short int x,y,z;//三轴加速度
	// 1  打开 ,从设备打开
	fd = open(DEV, O_RDWR);
	if (fd<0) {
		perror("open:");
		return -1;
	}
	// 2 设置从设备地址
	ret = ioctl(fd, I2C_SLAVE, ADDRESS);
	if (ret<0) {
		perror("ioctl:");
		goto CLOSE;
	}
	// 3 操作:读取器件id
	ret = adxl_read(fd, DEVID);
	printf("器件ID[0x%02x]=0x%02x\n", DEVID, ret);
	// 4 设置->启动,具体参见芯片手册.
	//   这里仅设置有限的读取数值功能.其实太复杂的还没搞懂:P
	adxl_write(fd, POWER_CTL, 0x28);
	// 5 循环测量(读取三轴加速度)
	for (;;) {
		x = adxl_read_short(fd, DATAX0);
		y = adxl_read_short(fd, DATAY0);
		z = adxl_read_short(fd, DATAZ0);
		printf("x=%5d y=%5d z=%5d\n", x,y,z);
		usleep(100*1000);
	}
CLOSE:
	close(fd);
	return 0;
}
Exemple #3
0
void getAllAcelValue(ADXL_Addr_Dev dev, double *acel){
	status_code_t result;
	memset(acel, (-16000), NUM_AXIS);
	uint8_t b[6] = {0};
	uint16_t adxl = 0;
	uint8_t i = 0;
	
	/* Check if the dev exists: */
	if (imu_probe(dev) != TWI_SUCCESS){
		return;
	}
	
	//Read all axis address:
	result = adxl_read(dev, b, ADXL_DataX0, sizeof(b));
	
	//If there is an error in read return with wrong values:
	if (result != STATUS_OK) return;
	
	//Convert each axis from two complements to float:
	for (i = 0; i < NUM_AXIS; i++){
		adxl = (uint16_t)( (b[(2*i)+1] << 8) | b[(2*i)] );
		if ( !(adxl & 0xFC00) ){
			acel[i] = CONST_ADXL * ((float) adxl);
			} else {
			adxl = ( ( (~adxl) +1 ) & 0x03FF);
			acel[i] = -(CONST_ADXL * ((float) adxl) );
		}
		acel[i] += offsetAcel[i];	//Apply Offset
	}
}
Exemple #4
0
int print_regs(int fd, int from, int to)
{
	int i;
	int ret;
	for (i = from; i<=to; i++) {
		ret = adxl_read(fd, i);
		if (ret<0) {
			perror("读取测试寄存器");
		}
		printf("[0x%02X]=0x%02X\t", i, ret);
		if ((i-from+1)%4==0) {
			printf("\n");
		}
	}
	printf("\n");
	return 0;
}
Exemple #5
0
// 主函数
int main(int argc, char* argv[])
{
	int fd;
	int ret = 0;
	int funcs;
	int i;
	// 1  打开 ,从设备打开
	if (argc==2) {
		fd = open(argv[1], O_RDWR);
	} else {
		fd = open(DEV, O_RDWR);
	}
	if (fd<0) {
		perror("打开:");
		printf("fd=%d\n", fd);
		return -1;
	}
	// 1.1 获得支持函数列表<不必须>但是可以知道很多有用的信息
	// 支持的函数返回,并保存在 funcs 中.
	if ((ret = ioctl(fd, I2C_FUNCS, &funcs)<0))
	{
		perror("获得支持函数列表");
		return -1;
	}
	// check for req funcs 通过 & 操作得到是不是支持这些函数.
	// 应用层可以判断什么函数能用,什么不能用.类似向下兼容的作用
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_READ_WORD_DATA);
	CHECK_I2C_FUNC(funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA);
	// 2 设置从设备地址
	ret = ioctl(fd, I2C_SLAVE, ADDRESS);
	if (ret<0) {
		perror("ioctl:");
		printf("i2c_slave=%d\n", I2C_SLAVE);
		goto CLOSE;
	}
	//读取器件id
	ret = adxl_read(fd, REG_DEVID);
	if (ret<0) {
		perror("器件id");
	}
	printf("器件ID[0x%X]=0x%X\n", REG_DEVID, ret);
#if 0
	//其他寄存器测试
	printf("所有寄存器读取测试:\n");
	print_regs(fd, 0x1E, 0x39);
#endif
	adxl_write(fd, DATA_FORMAT, 0b01);
	adxl_write(fd, POWER_CTL, 0x28);
	adxl_write(fd, BW_RATE, 0x0B);
	adxl_write(fd, INT_ENABLE, 0x00);
#if 1
	__u16 x;
	for (;;) {
		x = adxl_read_byte(fd, DATAX0);
		printf("x=%X\n", x);
		usleep(100*1000);
	}
#endif

#if 0
	int x, y, z;
	int x0, x1, y0, y1, z0, z1;
	for (;;) {
		x0 = adxl_read(fd, DATAX0);
		x1 = adxl_read(fd, DATAX1);
		y0 = adxl_read(fd, DATAY0);
		y1 = adxl_read(fd, DATAY1);
		z0 = adxl_read(fd, DATAZ0);
		z1 = adxl_read(fd, DATAZ1);
		x = x0;
		y = y0;
		z = z0;
		guixyz('x', x);
		guixyz('y', y);
		guixyz('z', z);
		printf("\n");
		//printf("x0:%03d x1:%03d \n",x0,x1);
		//printf("y0:%03d y1:%03d \n",y0,y1);
		//printf("z0:%03d z1:%03d \n",z0,z1);
		//printf("x=%03d y=%03d z=%03d \n",x,y,z);
		usleep(100*1000);
		printf("\f");
	}
#endif
	CLOSE:
	close(fd);
	return 0;
}