Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
void adxl_init(void) 
{
	//Low Address:
	adxl_write(ADXL_ADDR_DATAFORMAT,0x0B,ADXL_ADDR_LOW);	//16-bit, 13-bit mode
	//adxl_write(ADXL_ADDR_BW_RATE,0x0F,ADXL_ADDR_LOW);		//Sample rate = 3200Hz = 0,3125ms LPF = 1600Hz
	//adxl_write(ADXL_ADDR_BW_RATE,0x0D,ADXL_ADDR_LOW);		//Sample rate = 800Hz = 1,25ms LPF = 400Hz
	//adxl_write(ADXL_ADDR_BW_RATE,0x0C,ADXL_ADDR_LOW);		//Sample rate = 400Hz = 2,5ms LPF = 200Hz
	//adxl_write(ADXL_ADDR_BW_RATE,0x0A,ADXL_ADDR_LOW);		//Sample rate = 100Hz = 10ms LPF = 50Hz
	adxl_write(ADXL_ADDR_BW_RATE,0x09,ADXL_ADDR_LOW);		//Sample rate = 50Hz = 20ms LPF = 25Hz
	adxl_write(ADXL_ADDR_POWERCTL,0x08,ADXL_ADDR_LOW);		//Start Measurement
	
	//High Address:
	adxl_write(ADXL_ADDR_DATAFORMAT,0x0B,ADXL_ADDR_HIGH);	//16-bit, 13-bit mode
	//adxl_write(ADXL_ADDR_BW_RATE,0x0F,ADXL_ADDR_HIGH);		//Sample rate = 3200Hz = 0,3125ms
	//adxl_write(ADXL_ADDR_BW_RATE,0x0D,ADXL_ADDR_HIGH);		//Sample rate = 800Hz = 1,25ms
	//adxl_write(ADXL_ADDR_BW_RATE,0x0C,ADXL_ADDR_HIGH);		//Sample rate = 400Hz = 2,5ms LPF = 200Hz
	//adxl_write(ADXL_ADDR_BW_RATE,0x0A,ADXL_ADDR_HIGH);		//Sample rate = 100Hz = 10ms LPF = 50Hz
	adxl_write(ADXL_ADDR_BW_RATE,0x09,ADXL_ADDR_LOW);		//Sample rate = 50Hz = 20ms LPF = 25Hz
	adxl_write(ADXL_ADDR_POWERCTL,0x08,ADXL_ADDR_HIGH);		//Start Measurement	
}
Ejemplo n.º 3
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;
}