NRF51822自学笔记(四)mpu6050读取原始数据

先看一下主函数= =

 

#include 
#include 
#include "nrf.h"
#include "simple_uart.h"
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h"
#include "nrf_delay.h"
#include "twi_master.h"
#include "twi_master_config.h"
#include "mpu6050.h"
#include "nrf_gpio.h"
#include "boards.h"

//#define ENABLE_LOOPBACK_TEST           /*!< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback */

#define ERROR_PIN                (LED_0)   /*!< gpio pin number to show error if loopback is enabled */
#define MAX_TEST_DATA_BYTES      (15U) /*!< max number of test bytes to be used for tx and rx */

int main(void)
{
	char q[265];
  float Yaw=0,Roll=0,Pitch=0;
	
  simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);
  twi_master_init();   //初始化iic
    if(mpu6050_init(0x68))
   {
     simple_uart_putstring((const uint8_t *)" \n\r initsucceeded! ");
   }
	  mpu_init(0);
    mpu_dmp_init();
	 while(1)
		 {
	    simple_uart_putstring((const uint8_t *)" \n\rStart: ");
			 
			MPU_Get_Temperature();
			MPU_Get_Accelerometer();
			MPU_Get_Gyroscope();
			 
			mpu_dmp_get_data(&Pitch,&Roll,&Yaw);
			sprintf(q,"\n\r pitch:%f,roll:%f,yaw:%f",Pitch,Roll,Yaw);
			simple_uart_putstring((const uint8_t *)q);
			nrf_delay_ms(300);
     }		 
}

这里得到温度,加速度,陀螺仪三个函数是改的原子的程序

 

 

void MPU_Get_Temperature(void)        //读出摄氏度
{
    uint8_t buf[2]; 
    short raw;
	  float temp;
	  char t[256];
	  mpu6050_register_read(MPU_TEMP_OUTH_REG,buf,2); 
    raw=((uint16_t)buf[0]<<8)|buf[1]; 
    temp=36.53+((double)raw)/340;  
	  sprintf(t,"\n\r mpu_temp:%f",temp);
		simple_uart_putstring((const uint8_t *)t);
}

void MPU_Get_Gyroscope(void)
{
	int16_t gyro[3];	
	uint8_t buf[6];
	mpu6050_register_read(MPU_GYRO_XOUTH_REG,buf,6);
	char g[256];
    gyro[0]=((uint16_t)buf[0]<<8)|buf[1];  
		gyro[1]=((uint16_t)buf[2]<<8)|buf[3];  
		gyro[2]=((uint16_t)buf[4]<<8)|buf[5];
			memset(g,0,256);
			sprintf(g,"\n\r mpu_gyro:%d,%d,%d",gyro[0],gyro[1],gyro[2]);
	    simple_uart_putstring((const uint8_t *)g); 		
}

void MPU_Get_Accelerometer(void)
{
	int16_t acce[3];
	uint8_t buf[6];
	mpu6050_register_read(MPU_ACCEL_XOUTH_REG,buf,6);
	char a[256];
	  acce[0]=((uint16_t)buf[0]<<8)|buf[1];  
		acce[1]=((uint16_t)buf[2]<<8)|buf[3];  
		acce[2]=((uint16_t)buf[4]<<8)|buf[5];
			memset(a,0,256);
			sprintf(a,"\n\r mpu_acce:%d,%d,%d",acce[0],acce[1],acce[2]);		
	    simple_uart_putstring((const uint8_t *)a);
}


mpu的初始化设置如下

int mpu_init(struct int_param_s *int_param)
{
    unsigned char data[6], rev;

    /* Reset device. */
    data[0] = BIT_RESET;
    if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
        return -1;
    delay_ms(100);

    /* Wake up chip. */
    data[0] = 0x00;
    if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, data))
        return -1;

    /* Check product revision. */
    if (i2c_read(st.hw->addr, st.reg->accel_offs, 6, data))
        return -1;
    rev = ((data[5] & 0x01) << 2) | ((data[3] & 0x01) << 1) |
        (data[1] & 0x01);

    if (rev) {
        /* Congrats, these parts are better. */
        if (rev == 1)
            st.chip_cfg.accel_half = 1;
        else if (rev == 2)
            st.chip_cfg.accel_half = 0;
        else {
            log_e("Unsupported software product rev %d.\n", rev);
            return -1;
        }
    } else {
        if (i2c_read(st.hw->addr, st.reg->prod_id, 1, data))
            return -1;
        rev = data[0] & 0x0F;
        if (!rev) {
            log_e("Product ID read as 0 indicates device is either "
                "incompatible or an MPU3050.\n");
            return -1;
        } else if (rev == 4) {
            log_i("Half sensitivity part found.\n");
            st.chip_cfg.accel_half = 1;
        } else
            st.chip_cfg.accel_half = 0;
    }

    /* Set to invalid values to ensure no I2C writes are skipped. */
    st.chip_cfg.sensors = 0xFF;
    st.chip_cfg.gyro_fsr = 0xFF;
    st.chip_cfg.accel_fsr = 0xFF;
    st.chip_cfg.lpf = 0xFF;
    st.chip_cfg.sample_rate = 0xFFFF;
    st.chip_cfg.fifo_enable = 0xFF;
    st.chip_cfg.bypass_mode = 0xFF;
    /* mpu_set_sensors always preserves this setting. */
    st.chip_cfg.clk_src = INV_CLK_PLL;
    /* Handled in next call to mpu_set_bypass. */
    st.chip_cfg.active_low_int = 1;
    st.chip_cfg.latched_int = 0;
    st.chip_cfg.int_motion_only = 0;
    st.chip_cfg.lp_accel_mode = 0;
    memset(&st.chip_cfg.cache, 0, sizeof(st.chip_cfg.cache));
    st.chip_cfg.dmp_on = 0;
    st.chip_cfg.dmp_loaded = 0;
    st.chip_cfg.dmp_sample_rate = 0;

    if (mpu_set_gyro_fsr(2000))    //陀螺仪2000DPS
        return -1;
    if (mpu_set_accel_fsr(2))      //加速度计2G
        return -1;
    if (mpu_set_lpf(42))           //DLPF42Hz
        return -1;
    if (mpu_set_sample_rate(50))   //FIFOrate50Hz
        return -1;
    if (mpu_configure_fifo(0))     
        return -1;

    if (int_param)
        reg_int_cb(int_param);

    /* Already disabled by setup_compass. */
    if (mpu_set_bypass(0))
        return -1;
    mpu_set_sensors(0);
    return 0;
}


串口读出来的数据如下图
NRF51822自学笔记(四)mpu6050读取原始数据_第1张图片

 

 

 

手指放在mpu6050芯片上 会看到温度升高

 本来想用dmp读出四元数,但是在dmp初始化的加载固件一步总是原因不明的失败……

NRF51822自学笔记(四)mpu6050读取原始数据_第2张图片

 

/问题已经解决///

问题来源:i2c不能连续的读写

特此感谢指导我解决问题的热心人士!

 

 

你可能感兴趣的:(NRF51822,nrf51822,芯片,数据)