2 qq 34784245 qq_34784245 于 2016.04.25 14:24 提问

edison连接mpu6050代码看不大懂

#include "math.h"
#include "stdio.h"
#include "mraa.h"
#include "unistd.h"

#define MPU6050_I2C_ADDR 0x68
#define PWR_MGMT_1 0x6B
#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C

int main(int argc, char *argv[]) {
uint8_t rx[6];
mraa_init();
mraa_i2c_context i2c;
i2c = mraa_i2c_init(0);
uint8_t rx_tx_buf[2];
if(mraa_i2c_address(i2c, MPU6050_I2C_ADDR)==MRAA_SUCCESS){
printf("found sensor!\n");
}

rx_tx_buf[0] = PWR_MGMT_1;
rx_tx_buf[1] = 0x00;
if(mraa_i2c_write(i2c,rx_tx_buf,2)==MRAA_SUCCESS){
    printf("initial POWER success!\n");
}

rx_tx_buf[0] = SMPLRT_DIV;
rx_tx_buf[1] = 0x07;//为什么要设置成0X07??
if(mraa_i2c_write(i2c,rx_tx_buf,2)==MRAA_SUCCESS){
    printf("initial SMPLRT_DIV success!\n");
}

rx_tx_buf[0] = CONFIG;
rx_tx_buf[1] = 0x06;//为什么是0x06?
if(mraa_i2c_write(i2c,rx_tx_buf,2)==MRAA_SUCCESS){
    printf("initial CONFIG_RIG success!\n");
}

rx_tx_buf[0] = GYRO_CONFIG;
rx_tx_buf[1] = 0x18;//为什么是0x18
if(mraa_i2c_write(i2c,rx_tx_buf,2)==MRAA_SUCCESS){
    printf("initial GYRO_CONFIG success!\n");
}

rx_tx_buf[0] = ACCEL_CONFIG;
rx_tx_buf[1] = 0x10;
if(mraa_i2c_write(i2c,rx_tx_buf,2)==MRAA_SUCCESS){
    printf("initial ACCEL_CONFIG success!\n");
}

while(1)
{
    if(mraa_i2c_write_byte(i2c,0x3B)==MRAA_SUCCESS){
    //  printf("write reg success!\n");
    }

    mraa_i2c_read(i2c,rx,6);
    printf("x=%f, ",(float)(short)((rx[0]<<8)+rx[1])/4096.0); //这个不明白
    printf("y=%f, ",(float)(short)((rx[2]<<8)+rx[3])/4096.0);
    printf("z=%f, ",(float)(short)((rx[4]<<8)+rx[5])/4096.0);
    printf("\n");
    sleep(1);
} 
return 0; 

}
代码如上 是可以运行的。但是有几处不懂. 我是大二学生 刚开始接触edison板子 请大神解答 越详细越好!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!