你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

ST I2C 的例子怎么不能用啊  关闭

[复制链接]
guanjianguo 提问时间:2008-12-17 21:48 /
 
 
  I2C_GenerateSTART(I2C1, ENABLE);
  /* Test on I2C1 EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
 
 大多数时候死在循环那里啦,

Example2.rar

下载

314.23 KB, 下载次数: 25, 下载积分: ST金币 -1

收藏 评论4 发布时间:2008-12-17 21:48

举报

4个回答
得蜜 回答时间:2008-12-18 09:59:11

RE:ST I2C 的例子怎么不能用啊

附件里面是什么?
私奔 回答时间:2008-12-18 11:39:23

RE:ST I2C 的例子怎么不能用啊

void I2C_RTC_ByteWrite(u8 pBuffer, u16 WriteAddr)
{
  u8 aH,bL;

  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
   
  /* Test on EV5 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
  
  /* Send RTC address for write */
  I2C_Send7bitAddress(I2C1, RTC_ADDRESS, I2C_Direction_Transmitter);
   
  /* Test on EV6 and clear it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));  
  bL = WriteAddr & 0x00ff;   //高8位地址
  aH = WriteAddr >> 8;                 //低8位地址
  /* Send the RTC's 高8位地址 */   
  I2C_SendData(I2C1, aH);
   
  /* Test on EV8 and clear it */
  while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  /* Send the RTC's 低8位地址 */   
  I2C_SendData(I2C1, bL);
   
  /* Test on EV8 and clear it */
  while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send the current byte */
  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);
}
牛人说这样是好的。说你是否从机没有返回。
我代为转述下。
萧星-96138 回答时间:2008-12-18 11:44:11

可以做如下修改,把I2C_EE_WaitEepromStandbyState放在前面...

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define I2C_PageSize           16
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
static void I2C_EE_WaitEepromStandbyState(CPU_INT08U ucAddr);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : I2C_EE_WaitEepromStandbyState
* Description    : Wait for EEPROM Standby state
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_EE_WaitEepromStandbyState(CPU_INT08U ucAddr)      
{
  vu16 SR1_Tmp = 0;
  do
  {
    /* Send START condition */
    I2C_GenerateSTART(I2C1, ENABLE);
    /* Read I2C1 SR1 register */
    SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
    /* Send EEPROM address for write */
    I2C_Send7bitAddress(I2C1, ucAddr, I2C_Direction_Transmitter);
  }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & I2C_FLAG_ADDR));
  
  /* Clear AF flag */
  I2C_ClearFlag(I2C1, I2C_FLAG_AF);
}
/*******************************************************************************
* Function Name  : I2C_EE_BufferWrite
* Description    : Writes buffer of data to the I2C EEPROM.
* Input          : - pBuffer : pointer to the buffer  containing the data to be
*                    written to the EEPROM.
*                  - WriteAddr : EEPROM's internal address to write to.
*                  - NumByteToWrite : number of bytes to write to the EEPROM.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_EE_BufferWrite(CPU_INT08U ucDeviceAddr,CPU_INT16U WriteAddr, CPU_INT08U* pBuffer, u16 NumByteToWrite)
{
  CPU_INT08U NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;
  Addr = WriteAddr % I2C_PageSize;
  count = I2C_PageSize - Addr;
  NumOfPage =  NumByteToWrite / I2C_PageSize;
  NumOfSingle = NumByteToWrite % I2C_PageSize;
  /* If WriteAddr is I2C_PageSize aligned  */
  if(Addr == 0)
  {
    /* If NumByteToWrite < I2C_PageSize */
    if(NumOfPage == 0)
    {
      I2C_EE_PageWrite(ucDeviceAddr,WriteAddr,pBuffer,  NumOfSingle);
    }
    /* If NumByteToWrite > I2C_PageSize */
    else  
    {
      while(NumOfPage--)
      {
        I2C_EE_PageWrite(ucDeviceAddr,WriteAddr, pBuffer, I2C_PageSize);
        I2C_EE_WaitEepromStandbyState(ucDeviceAddr);
        WriteAddr +=  I2C_PageSize;
        pBuffer += I2C_PageSize;
      }
      if(NumOfSingle!=0)
      {
        I2C_EE_PageWrite(ucDeviceAddr,WriteAddr,pBuffer,  NumOfSingle);
      }
    }
  }
  /* If WriteAddr is not I2C_PageSize aligned  */
  else
  {
    /* If NumByteToWrite < I2C_PageSize */
    if(NumOfPage== 0)
    {
      I2C_EE_PageWrite(ucDeviceAddr,WriteAddr, pBuffer, NumOfSingle);
    }
    /* If NumByteToWrite > I2C_PageSize */
    else
    {
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / I2C_PageSize;
      NumOfSingle = NumByteToWrite % I2C_PageSize;   
      
      if(count != 0)
      {  
        I2C_EE_PageWrite(ucDeviceAddr, WriteAddr,pBuffer, count);
        WriteAddr += count;
        pBuffer += count;
      }
      
      while(NumOfPage--)
      {
        I2C_EE_PageWrite(ucDeviceAddr, WriteAddr, pBuffer,I2C_PageSize);
        WriteAddr +=  I2C_PageSize;
        pBuffer += I2C_PageSize;  
      }
      if(NumOfSingle != 0)
      {
        I2C_EE_PageWrite(ucDeviceAddr,WriteAddr,pBuffer,  NumOfSingle);
      }
    }
  }  
}
/*******************************************************************************
* Function Name  : I2C_EE_ByteWrite
* Description    : Writes one byte to the I2C EEPROM.
* Input          : - pBuffer : pointer to the buffer  containing the data to be
*                    written to the EEPROM.
*                  - WriteAddr : EEPROM's internal address to write to.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_EE_ByteWrite(CPU_INT08U ucDeviceAddr,CPU_INT16U WriteAddr,CPU_INT08U* pBuffer)
{
  I2C_EE_WaitEepromStandbyState(ucDeviceAddr);
  
  /* Send STRAT condition */
  I2C_GenerateSTART(I2C1, ENABLE);
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));  
  /* Send EEPROM address for write */
  I2C_Send7bitAddress(I2C1, ucDeviceAddr, I2C_Direction_Transmitter);
  
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
      
  /* Send the EEPROM's internal address to write to */   
  I2C_SendData(I2C1, (WriteAddr&0xff00)>>8);  
  /* Test on EV8 and clear it */
  while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  /* Send the EEPROM's internal address to write to */   
  I2C_SendData(I2C1, WriteAddr&0xff );  
  /* 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_EE_PageWrite
* Description    : Writes more than one byte to the EEPROM with a single WRITE
*                  cycle. The number of byte can't exceed the EEPROM page size.
* Input          : - pBuffer : pointer to the buffer containing the data to be
*                    written to the EEPROM.
*                  - WriteAddr : EEPROM's internal address to write to.
*                  - NumByteToWrite : number of bytes to write to the EEPROM.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_EE_PageWrite(CPU_INT08U ucDeviceAddr,CPU_INT16U WriteAddr, CPU_INT08U* pBuffer, CPU_INT08U NumByteToWrite)
{
  I2C_EE_WaitEepromStandbyState(ucDeviceAddr);
  
  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
  
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
  
  /* Send EEPROM address for write */
  I2C_Send7bitAddress(I2C1, ucDeviceAddr, I2C_Direction_Transmitter);
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));  
  /* Send the EEPROM's internal address to write to */   
  I2C_SendData(I2C1, (WriteAddr&0xff00)>>8);  
  /* Test on EV8 and clear it */
  while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  /* Send the EEPROM's internal address to write to */   
  I2C_SendData(I2C1, WriteAddr&0xff );  
  /* Test on EV8 and clear it */
  while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  /* While there is data to be written */
  while(NumByteToWrite--)  
  {
    /* Send the current byte */
    I2C_SendData(I2C1, *pBuffer);
    /* Point to the next byte to be written */
    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_EE_BufferRead
* Description    : Reads a block of data from the EEPROM.
* Input          : - pBuffer : pointer to the buffer that receives the data read
*                    from the EEPROM.
*                  - ReadAddr : EEPROM's internal address to read from.
*                  - NumByteToRead : number of bytes to read from the EEPROM.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_EE_BufferRead(CPU_INT08U ucDeviceAddr,CPU_INT16U ReadAddr,CPU_INT08U* pBuffer,  u16 NumByteToRead)
{  
  
  I2C_EE_WaitEepromStandbyState(ucDeviceAddr);
  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
  
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
  
  /* In the case of a single data transfer disable ACK before reading the data */
  if(NumByteToRead==1)
  {
    I2C_AcknowledgeConfig(I2C1, DISABLE);
  }
  
  /* Send EEPROM address for write */
  I2C_Send7bitAddress(I2C1, ucDeviceAddr, 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 EEPROM's internal address to write to */
  I2C_SendData(I2C1, (ReadAddr&0xff00)>>8);  
  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  /* Send the EEPROM's internal address to write to */
  I2C_SendData(I2C1, (ReadAddr&0xff));  
  /* 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 EEPROM address for read */
  I2C_Send7bitAddress(I2C1, ucDeviceAddr, 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)  
  {
    /* Test on EV7 and clear it */
    if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))  
    {
      if(NumByteToRead == 2)
      {
        /* Disable Acknowledgement */
        I2C_AcknowledgeConfig(I2C1, DISABLE);
      }
      if(NumByteToRead == 1)
      {
        /* Send STOP Condition */
        I2C_GenerateSTOP(I2C1, ENABLE);
      }
      
      /* Read a byte from the EEPROM */
      *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);
}
火雷达 回答时间:2011-11-19 14:14:09

回复:ST I2C 的例子怎么不能用啊

硬件的I2C好像是有点问题的,最好用软件模拟I2C

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版