本帖最后由 lhy995 于 2015-7-17 11:56 编辑
我修改了官方例程里面的I2C_MasterInt_demo 想用它来读取MPU6050的原始数据 但是读出来的一直是53713,函数是模仿的51例程写的 其中
I2C_Start();
I2C_WriteOneByte();
I2C_Stop();
I2C_ReadOneByte();
这四个函数是官方给的库函数 个人认为是可以对应于51例程里面的
I2C_Start();
I2C_SendByte();
I2C_Stop();
I2C_RecvByte();
目前的现象是
不管总线是否接6050 读取出的数都是53713
我写的程序里给了6050足够的启动时间,而且在初始化6050的命令发出后也有一定的延时, 应该不是这个方面的问题。
跪求各位大神帮忙看看是哪里的问题
除此以外关于库函数还有一些问题 就是I2C_ReadOneByte(); 中的第三个参数取什么时会在接受完一个字节的数据后发送ACK信号
下面贴上我写的程序和51例程
先是我改的程序
/******************************************************************************
*
* Freescale Semiconductor Inc.
* (c) Copyright 2013 Freescale Semiconductor, Inc.
* ALL RIGHTS RESERVED.
*
***************************************************************************
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
***************************************************************************//*!
*
* @file I2C_MasterInt_demo.c
*
* @author Freescale
*
* @version 0.0.1
*
* @date Jun. 25, 2013
*
* @brief providing framework of demo cases for MCU.
*
*******************************************************************************/
#include "common.h"
#include "ics.h"
#include "rtc.h"
#include "uart.h"
#include "i2c.h"
#include "i2C_app.h"
#include "sysinit.h"
/******************************************************************************
* Global variables
******************************************************************************/
/******************************************************************************
* Constants and macros
******************************************************************************/
#define I2C_READ_DATA_LENGTH 64
#define I2C_SLAVE_ADDRESS1 0x50
/******************************************************************************
* Local types
******************************************************************************/
#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
/******************************************************************************
* Local function prototypes
******************************************************************************/
/******************************************************************************
* Local variables
******************************************************************************/
uint8_t u8I2C_SendBuff[64];
uint8_t u8I2C_ReceiveBuff[64];
uint32_t u32I2C_ReceiveLength;
/******************************************************************************
* Local functions
******************************************************************************/
int main (void);
void RTC_Task(void);
void Single_WriteI2C(uint8_t REG_Address,uint8_t REG_data)
{
I2C_Start(I2C0);
I2C_WriteOneByte(I2C0,SlaveAddress);
I2C_WriteOneByte(I2C0,REG_Address);
I2C_WriteOneByte(I2C0,REG_data);
I2C_Stop(I2C0);
}//向I2C设备写一个字节的数据
uint8_t Single_ReadI2C(uint8_t REG_Address)
{
uint8_t REG_data;
I2C_Start(I2C0);
I2C_WriteOneByte(I2C0,SlaveAddress);
I2C_WriteOneByte(I2C0,REG_Address);
I2C_Start(I2C0);
I2C_WriteOneByte(I2C0,SlaveAddress+1);
I2C_ReadOneByte(I2C0,®_data,0);
I2C_Stop(I2C0);
return REG_data;
}//从I2C设备读取一个字节的数据
void InitMPU6050()
{
Single_WriteI2C(PWR_MGMT_1, 0x00);
Single_WriteI2C(SMPLRT_DIV, 0x07);
Single_WriteI2C(CONFIG, 0x06);
Single_WriteI2C(GYRO_CONFIG, 0x18);
Single_WriteI2C(ACCEL_CONFIG, 0x01);
}//初始化6050
int GetData(uint8_t REG_Address)
{
uint8_t H=0x00,L=0x00;
H=Single_ReadI2C(REG_Address);
L=Single_ReadI2C(REG_Address+1);
return (H<<8)+L;
}//数据合成
/******************************************************************************
* Global functions
******************************************************************************/
/********************************************************************/
int main (void)
{
uint8_t u8I2C_ErrorStatus;
I2C_ConfigType sI2C_Config = {0};
volatile uint32_t i;
sysinit();
printf("\nRunning the I2C_MasterInt_demo project.\r\n");
LED0_Init();
LED2_Init();
UART_WaitTxComplete(TERM_PORT);
/* initialize I2C global variable and call back function*/
I2C_InitGlobalVariable( );
/* Initialize I2C module with poll mode */
sI2C_Config.u16Slt = 0;
sI2C_Config.u16F = 0x1F;
sI2C_Config.sSetting.bIntEn = 1;
sI2C_Config.sSetting.bI2CEn = 1;
I2C_Init(I2C0,&sI2C_Config );
/* echo chars received from terminal */
for(i=0;i<0xffffff;i++);.//等待6050启动完成
InitMPU6050();//初始化6050
for(i=0;i<0xfffff;i++);//等待6050初始化完成
ile(1)
{
printf("%d\n",GetData(ACCEL_XOUT_H));//输出x轴加速度到串口
for(i=0;i<0xfffff;i++);
}
}
/*****************************************************************************//*!
+FUNCTION----------------------------------------------------------------
* @function name: RTC_Task
*
* @brief callback routine of RTC driver which does what you want to do at
* every RTC period.
*
* @param none
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void RTC_Task(void)
{
/* toggle LED1 */
LED0_Toggle();
}
/********************************************************************/
|