问题描述
- 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 0x1Cint 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板子 请大神解答 越详细越好!
时间: 2024-10-27 22:13:21