野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9686|回复: 5

利用STM32F103硬件I2C驱动MPU6050,求救火哥!

[复制链接]
发表于 2015-7-14 16:59:40 | 显示全部楼层 |阅读模式
程序很简单,就是先初始化GPIO,然后模式设置,结果程序就卡在while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));一直不出来。麻烦大家帮我分析一下!

#define MPU6050_I2C I2C1

void MPU6050_I2C_BufferRead(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead)
{
// ENTR_CRT_SECTION();

  /* While the bus is busy */
  while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_BUSY));

  /* Send START condition */
  I2C_GenerateSTART(MPU6050_I2C, ENABLE);

  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));

  /* Send MPU6050 address for write */
  I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Transmitter);

  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  /* Clear EV6 by setting again the PE bit */
  I2C_Cmd(MPU6050_I2C, ENABLE);

  /* Send the MPU6050's internal address to write to */
  I2C_SendData(MPU6050_I2C, readAddr);

  /* Test on EARM and clear it */
  while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send STRAT condition a second time */
  I2C_GenerateSTART(MPU6050_I2C, ENABLE);

  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));

  /* Send MPU6050 address for read */
  I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Receiver);

  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  /* While there is data to be read */
  while(NumByteToRead)
  {
    if(NumByteToRead == 1)
    {
      /* Disable Acknowledgement */
      I2C_AcknowledgeConfig(MPU6050_I2C, DISABLE);

      /* Send STOP Condition */
      I2C_GenerateSTOP(MPU6050_I2C, ENABLE);
    }

    /* Test on EV7 and clear it */
    if(I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
    {
      /* Read a byte from the MPU6050 */
      *pBuffer = I2C_ReceiveData(MPU6050_I2C);

      /* Point to the next location where the byte read will be saved */
      pBuffer++;

      /* Decrement the read bytes counter */
      NumByteToRead--;
    }
  }

  /* Enable Acknowledgement to be ready for another reception */
  I2C_AcknowledgeConfig(MPU6050_I2C, ENABLE);
//  EXT_CRT_SECTION();

}


回复

使用道具 举报

发表于 2015-7-15 08:34:38 | 显示全部楼层
检查硬件接线,看看有没有接上拉电阻
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-7-15 15:18:41 | 显示全部楼层
接了1K的上拉电阻。
回复 支持 反对

使用道具 举报

发表于 2016-1-12 20:04:31 | 显示全部楼层
我也遇到了同样的问题
回复 支持 反对

使用道具 举报

发表于 2017-7-2 20:31:02 | 显示全部楼层
驱动ds1307时钟模块时,我用硬件iic也得不到应答,不知道为什么,网上说f1的硬件iic有点问题,不知道是不是这样。
回复 支持 反对

使用道具 举报

发表于 2017-9-8 09:19:54 | 显示全部楼层
我也遇到了同样的问题,请问楼主解决了吗
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-1 22:40 , Processed in 0.036832 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表