欢迎光临
我们一直在努力

基于linux I2C MPU6050 demo样例 | Linux mpu6050 rk3128

系统平台Ubuntu
硬件平台RK3128

下载链接
mpu6050.c
或者命令行下载:
wget http://www.huangea.com/wp-content/uploads/2016/10/mpu6050.c
编译命令:gcc mpu6050.c
*********************************************************

/************************************************************/
//文件名:mpu6050.c
//功能:测试linux下iic读写mpu6050程序
//使用说明: (1)
//          (2)
//          (3)
//          (4)
//作者:huangea
//日期:2016-10-03
/************************************************************/

//包含头文件
#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

typedef unsigned char uint8;

int fd = -1;

//函数声明
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);
static uint8 printarray(uint8 Array[], uint8 Num);


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

	if(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(fd, I2C_SLAVE, Address)<0) {    //set i2c address 
		printf("fail to set i2c device slave address!\n");
		close(fd);
		return -1;
	}
	printf("set slave address to 0x%x success!\n", Address);

	i2c_write(fd,PWR_MGMT_1,0X00);    
	i2c_write(fd,SMPLRT_DIV,0X07); 
	i2c_write(fd,CONFIG,0X06); 
	i2c_write(fd,ACCEL_CONFIG,0X01); 

	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(fd, REG_Address, &H);
	i2c_read(fd, REG_Address + 1, &L);

	return (H<<8)+L;
}
// main
int main(int argc, char *argv[])
{
	MPU6050_Init();
	printf("init success!\n");
	usleep(1000*100);
	while(1)
	{
		printf("*********************\n");
		usleep(1000*200);
		printf("ACCE_X:%6d\n ",GetData(ACCEL_XOUT_H));
		printf("ACCE_Y:%6d\n ",GetData(ACCEL_YOUT_H));
		printf("ACCE_Z:%6d\n ",GetData(ACCEL_ZOUT_H));
		printf("GYRO_X:%6d\n ",GetData(GYRO_XOUT_H));
		printf("GYRO_Y:%6d\n ",GetData(GYRO_YOUT_H));
		printf("GYRO_Z:%6d\n ",GetData(GYRO_ZOUT_H));
	}

	close(fd);
}




赞(0) 打赏
未经允许不得转载:huangea的博客 » 基于linux I2C MPU6050 demo样例 | Linux mpu6050 rk3128

评论 3

  1. #1

    谢谢,用到了~~~~~~~~~~~~~~~~~~~~

    米课外贸培训8年前 (2016-10-19)回复
  2. #2

    博主的很多文章给了我很大帮助,加了博主微信后,人也很亲切,耐心的给我指导,真是特别感谢您!

    qq6765378346年前 (2017-12-19)回复
  3. #3

    注意:下载附件里的,static uint8 i2c_read(int fd, uint8 reg, uint8 *val) 函数里的两个注释需要取消,这个调试的时候忘记关掉了。谢谢友 qq676537834 提醒。

    huangea6年前 (2017-12-20)回复

有趣的网站

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

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

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

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册