Firefly开源社区

陀螺仪mpu6050 数据采集实现

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2017-5-11 16:30:50     
/************************************************************/
//文件名:mpu6050.c
//功能:测试linux下iic读写mpu6050程序
//使用说明: (1)
//          (2)
//          (3)
//          (4)
//作者:huangea
//日期:2016-10-03
/************************************************************/
//包含头文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

//宏定义

#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-1", 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-1 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
int GetData(unsigned char REG_Address)
{
        char H,L;
        uint8 ret;
        int return_value = 0;
        ret = i2c_read(fd, REG_Address, &H);
        if (ret)
        {
                printf("Read error\n");
        }
        i2c_read(fd, REG_Address + 1, &L);

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

        close(fd);
}
回复

使用道具 举报

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2017-5-11 16:32:29     
从网上找的代码,稍微修改了下,分享给大家。
回复

使用道具 举报

459

积分

0

威望

0

贡献

技术达人

Rank: 2

积分
459
发表于 2017-6-15 22:24:25     
驱动程序在哪
回复

使用道具 举报

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2017-6-16 20:32:05     

看下我另外一个帖子,下面是链接地址
http://developer.t-firefly.com/thread-12363-1-1.html
回复

使用道具 举报

216

积分

0

威望

0

贡献

技术达人

Rank: 2

积分
216
发表于 2017-11-6 15:25:12     
z3j6w9 发表于 2017-6-16 20:32
看下我另外一个帖子,下面是链接地址
http://developer.t-firefly.com/thread-12363-1-1.html

怎么提示mpu6050B1 4-0068: get irq-gpio error?
回复

使用道具 举报

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2017-11-24 09:51:09     
今夕何夕 发表于 2017-11-6 15:25
怎么提示mpu6050B1 4-0068: get irq-gpio error?

中断获取失败,仔细检查下代码.
回复

使用道具 举报

165

积分

0

威望

0

贡献

技术小白

积分
165
发表于 2021-12-28 10:03:58     
陀螺仪怎么调试才是成功的了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表