欢迎光临
我们一直在努力

Linux RTC 定时中断 多线程 读取MPU6050

硬件环境:RK3128
软件环境:Ubuntu
下载链接:mpu6050_rtc
命令行下载:wget http://www.huangea.com/wp-content/uploads/2016/10/mpu6050_rtc.c
编译命令: gcc mpu6050_rtc -pthread

 /************************************************************/
//文件名:mpu6050_rtc.c
//功能:Read mpu6050 data based Linux and RTC 
//使用说明: (1) gcc mpu6050_rtc -pthread
// (2) 10ms Rrint once data 
// (3)
// (4)
//作者:huangea
//日期:2016-10-07
/************************************************************/
//包含头文件
#include   
#include   
#include   
#include   
#include   
#include   
#include 
#include 
#include 
#include   
#include   
#include  

//宏定义

#define SMPLRT_DIV 0x19 
#define CONFIG 0x1A 
#define GYRO_CONFIG 0x1B 
#define ACCEL_CONFIG 0x1C
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B
#define WHO_AM_I 0x75 
#define SlaveAddress 0xD0 

#define Address 0x68 //MPU6050地址

#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_SLAVE 0x0703 //IIC从器件的地址设置
#define I2C_BUS_MODE 0x0780
#define RTC_HZ 100


typedef unsigned char uint8;

int I2C_fd = -1;
int RTC_fd;

//函数声明
static uint8 MPU6050_Init(void);
static uint8 i2c_write(int fd, uint8 reg, uint8 val);
static uint8 i2c_read(int fd, uint8 reg, uint8 *val);
short GetData(unsigned char REG_Address);


//MPU6050初始化
static uint8 MPU6050_Init(void)
{
	I2C_fd = open("/dev/i2c-0", O_RDWR);   // open file and enable read and  write

	if(I2C_fd < 0)
	{
		perror("Can't open /dev/MPU6050 \n"); // open i2c dev file fail
		exit(1);
	}
	printf("open /dev/i2c-0 success !\n");   // open i2c dev file succes

	if(ioctl(I2C_fd, I2C_SLAVE, Address)<0) {    //set i2c address 
		printf("fail to set i2c device slave address!\n");
		close(I2C_fd);
		return -1;
	}
	printf("set slave address to 0x%x success!\n", Address);

	i2c_write(I2C_fd,PWR_MGMT_1,0X00);    
	i2c_write(I2C_fd,SMPLRT_DIV,0X00); 
	i2c_write(I2C_fd,CONFIG,0X01); 
	i2c_write(I2C_fd,ACCEL_CONFIG,0X01); 
	i2c_write(I2C_fd,GYRO_CONFIG,0X18); 

	return(1);
}



//MPU6050 wirte byte
static uint8 i2c_write(int fd, uint8 reg, uint8 val)
{
	int retries;
	uint8 data[2];

	data[0] = reg;
	data[1] = val;
	for(retries=5; retries; retries--) {
		if(write(fd, data, 2)==2)
			return 0;
		usleep(1000*10);
	}
	return -1;
}

//MPU6050 read byte
static uint8 i2c_read(int fd, uint8 reg, uint8 *val)
{
	int retries;

	for(retries=5; retries; retries--)
		if(write(fd, &reg, 1)==1)
			if(read(fd, val, 1)==1)
				return 0;
	return -1;
}

//get data
short GetData(unsigned char REG_Address)
{
	char H,L;
	i2c_read(I2C_fd, REG_Address, &H);
	i2c_read(I2C_fd, REG_Address + 1, &L);

	return (H<<8)+L;
}

void *thread(void * threadid)
{
	unsigned long data = 0;  
	unsigned int i;
	int RTC_fd = open ("/dev/rtc", O_RDONLY);  

	if(RTC_fd < 0)  
	{  
		perror("open");  
		exit(errno);  
	}  

	/*Set the freq as  Hz*/  
	if(ioctl(RTC_fd, RTC_IRQP_SET, RTC_HZ) < 0)  
	{  
		perror("ioctl(RTC_IRQP_SET)");  
		close(RTC_fd);  
		exit(errno);  
	}  
	/* Enable periodic interrupts */  
	if(ioctl(RTC_fd, RTC_PIE_ON, 0) < 0)  
	{  
		perror("ioctl(RTC_PIE_ON)");  
		close(RTC_fd);  
		exit(errno);  
	}  
	while(1)
	{
		//10ms RTC waiting
		if(read(RTC_fd, &data, sizeof(unsigned long)) < 0)  
		{  
			perror("read");  
			close(RTC_fd);  
			exit(errno);  

		}  
		i++;
		printf("\033[2J");
		printf("ACCE_X:%6d",GetData(ACCEL_XOUT_H));
		printf("| ACCE_Y:%6d",GetData(ACCEL_YOUT_H));
		printf("| ACCE_Z:%6d",GetData(ACCEL_ZOUT_H));
		printf("| GYRO_X:%6d",GetData(GYRO_XOUT_H));
		printf("| GYRO_Y:%6d",GetData(GYRO_YOUT_H));
		printf("| GYRO_Z:%6d   number:%10d\n ",GetData(GYRO_ZOUT_H),i);
	}  
	/* Disable periodic interrupts */  
	ioctl(RTC_fd, RTC_PIE_OFF, 0);  
	close(RTC_fd);  
	pthread_exit(NULL);
}

int main(int argc, char* argv[])  
{  

	int retval = 0;
	pthread_t id;
	void *ret;
	int  retv,t = 3;

	MPU6050_Init();

	retv = pthread_create(&id,NULL,(void *)thread,(void *)t);
	if(retv != 0)
	{
		printf("Create pthread error\n");
		return 1;
	}

	while(1)
		;

	close(I2C_fd);
	return 0;  
} 
赞(0) 打赏
未经允许不得转载:huangea的博客 » Linux RTC 定时中断 多线程 读取MPU6050
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

有趣的网站

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏