打印
[STM32F4]

STM32F4 模拟i2c的问题

[复制链接]
2724|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
qq8605578|  楼主 | 2016-2-17 14:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我把stm32F1的模拟i2c的程序拿过来,放在F4上面跑,但是怎么都跑不通。
F1和F4不同的地方也就是IO配置这个地方了,还有延时不一样。
  GPIO_InitTypeDef  GPIO_InitStructure;  

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);              //使能LED时钟
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);



#define SCL_H    GPIOC->BSRRH=GPIO_Pin_6      //GPIO_SetBits(GPIOB , GPIO_Pin_6)  
#define SCL_L    GPIOC->BSRRL=GPIO_Pin_6      //GPIO_ResetBits(GPIOB , GPIO_Pin_6)


#define SDA_H    GPIOC->BSRRH=GPIO_Pin_7      //GPIO_SetBits(GPIOB , GPIO_Pin_7)   
#define SDA_L    GPIOC->BSRRL=GPIO_Pin_7      //GPIO_ResetBits(GPIOB , GPIO_Pin_7)

#define SCL_read     GPIOC->IDR  & GPIO_Pin_6  //GPIO_ReadInputDataBit(GPIOC , GPIO_Pin_6)
#define SDA_read     GPIOC->IDR  & GPIO_Pin_7  //GPIO_ReadInputDataBit(GPIOC , GPIO_Pin_7)


static void I2C_delay(void)
{       
   uint8_t i=20;    //F1的时候填5-15都可以。
   while(i)
   {
     i--;
   }
}

这样配置对么?
沙发
qq8605578|  楼主 | 2016-2-17 14:06 | 只看该作者
在线等大神给看看

使用特权

评论回复
板凳
yklstudent| | 2016-2-17 14:11 | 只看该作者
自己在线跑下看挂在哪里了

使用特权

评论回复
地板
ningling_21| | 2016-2-17 14:27 | 只看该作者
有硬件为啥要模拟时序?

使用特权

评论回复
5
qq8605578|  楼主 | 2016-2-17 15:09 | 只看该作者
ningling_21 发表于 2016-2-17 14:27
有硬件为啥要模拟时序?

硬件听说有bug,就一直在用模拟,时序在F1上面跑是正常的。到f4上就不行了

使用特权

评论回复
6
qq8605578|  楼主 | 2016-2-17 15:18 | 只看该作者
yklstudent 发表于 2016-2-17 14:11
自己在线跑下看挂在哪里了

u8 flag=0;
        flag = (Single_Read(ACCEL_ADDRESS,INT_STATUS));
        while(!(flag & 0x01))
        {
            Delay(0x1F);
            flag = (Single_Read(ACCEL_ADDRESS,INT_STATUS));
        }

我用示波器看不到波形。。担心是配置的错误

使用特权

评论回复
7
yklstudent| | 2016-2-17 15:27 | 只看该作者
在线跑下看看到底死在那边了

使用特权

评论回复
8
icecut| | 2016-2-17 15:46 | 只看该作者
f4硬件的好用了.bug 是在外界干扰严重的时候 f1出现的.解决办法官方也说了...

使用特权

评论回复
9
qq8605578|  楼主 | 2016-2-18 01:47 | 只看该作者
icecut 发表于 2016-2-17 15:46
f4硬件的好用了.bug 是在外界干扰严重的时候 f1出现的.解决办法官方也说了... ...

:'(BUG果断被我碰上了。。。。刚运**在while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));这出不去了。。

使用特权

评论回复
10
qq8605578|  楼主 | 2016-2-18 01:48 | 只看该作者
icecut 发表于 2016-2-17 15:46
f4硬件的好用了.bug 是在外界干扰严重的时候 f1出现的.解决办法官方也说了... ...

版主有没有防守程序给参考一下

使用特权

评论回复
11
klffnj| | 2016-2-18 09:19 | 只看该作者
读数据之前,IO模式改为输入。

使用特权

评论回复
12
qq8605578|  楼主 | 2016-2-18 10:26 | 只看该作者
GPIO_InitTypeDef  GPIO_InitStructure;
    I2C_InitTypeDef  I2C_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);              //使能LED时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);  /** CODEC_I2C peripheral configuration */


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);


    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;   
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;  
    I2C_InitStructure.I2C_OwnAddress1 = 0xC0;  
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;   
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;  
    I2C_InitStructure.I2C_ClockSpeed = 1000000;  /** Enable the I2C peripheral */  
    I2C_Init(I2C1, &I2C_InitStructure);
    I2C_Cmd(I2C1, ENABLE);

}

/*******************************************************************************
* Function Name  : I2C_Configuration
* Description    : EEPROM管脚配置
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void Single_Write(u8 slaveAddr,u8 writeAddr,u8 pBuffer)
{
  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);          //发送开始信号
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  /* Send MPU6050 address for write */
  I2C_Send7bitAddress(I2C1, slaveAddr, I2C_Direction_Transmitter);          // 发送 MPU6050 地址、状态(写)
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  /* Send the MPU6050's internal address to write to */
  I2C_SendData(I2C1, writeAddr);                   //发送 MPU6050内部某个待写寄存器地址
  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send the byte to be written */
  I2C_SendData(I2C1, pBuffer);                     //发送要写入的内容
  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send STOP condition */
  I2C_GenerateSTOP(I2C1, ENABLE);          //发送结束信号
}

/*******************************************************************************
* Function Name  : I2C_Configuration
* Description    : EEPROM管脚配置
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/

void Single_Read(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead)
{

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

  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  /* Send MPU6050 address for write */
  I2C_Send7bitAddress(I2C1, slaveAddr, I2C_Direction_Transmitter);
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  /* Clear EV6 by setting again the PE bit */
  I2C_Cmd(I2C1, ENABLE);
  /* Send the MPU6050's internal address to write to */
  I2C_SendData(I2C1, readAddr);
  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send STRAT condition a second time */
  I2C_GenerateSTART(I2C1, ENABLE);
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  /* Send MPU6050 address for read */
  I2C_Send7bitAddress(I2C1, slaveAddr, I2C_Direction_Receiver);
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

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

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

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

      /* 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(I2C1, ENABLE);
}



使用特权

评论回复
13
qq8605578|  楼主 | 2016-2-18 12:46 | 只看该作者
klffnj 发表于 2016-2-18 09:19
读数据之前,IO模式改为输入。

改了还是不行。。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

74

帖子

0

粉丝