⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 73x_i2c.c

📁 国外LPC2000系列的一些源程序,请大家快快下载
💻 C
📖 第 1 页 / 共 2 页
字号:
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral. 
* Output         : None	                           
* Return         : A 16 bit register flag status.                                    
*        Reg bits: Ack-ENDAD-AF-STOPF-ARLO-BERR-GCAL-EVF-ADD10-TRA-BUSY-BTF-ADSL-M/SL-SB 
******************************************************************************************/
u16 I2C_GetStatus(I2C_TypeDef* I2Cx)
{
  u16 Flag1=0, Flag2=0;

  Flag1 = I2Cx->SR2;
  Flag1 = Flag1<<8;
  Flag2 = I2Cx->CR&0x04;
  return (((I2Cx->SR1|(Flag1)) & 0x3FFF) | (Flag2<<12));
}

/*****************************************************************************************
* Function Name  : I2C_GetLastEvent                                                     
* Description    : Get the Last happened I2C Event.                                              
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.  
* Output         : None	                          
* Return         : The Last happened Event.                                                    
*****************************************************************************************/
u16 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
{
  u16 Flag1=0, LastEvent=0;

  Flag1 = I2Cx->SR2;
  Flag1 = Flag1<<8;
  LastEvent = (((I2Cx->SR1|(Flag1)) & 0x3FFF));
  return LastEvent;
}

/*****************************************************************************************
* Function Name  : I2C_EventCheck                                                       
* Description    : Get the Last I2C Event.                                              
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.                            
*                : - I2C_Event: the event to check. This parameter can be one of the following
*                    values:
*                         - I2C_EVENT_SLAVE_ADDRESS_MATCHED
*                         - I2C_EVENT_SLAVE_BYTE_RECEIVED
*                         - I2C_EVENT_SLAVE_BYTE_TRANSMITTED
*                         - I2C_EVENT_MASTER_MODE_SELECT
*                         - I2C_EVENT_MASTER_MODE_SELECTED
*                         - I2C_EVENT_MASTER_BYTE_RECEIVED
*                         - I2C_EVENT_MASTER_BYTE_TRANSMITTED
*                         - I2C_EVENT_MASTER_MODE_ADDRESS10
*                         - I2C_EVENT_SLAVE_STOP_DETECTED
*                         - I2C_EVENT_SLAVE_ACK_FAILURE  
* Output         : None	                                        
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Last event is equal to the I2C_Event
*                         - ERROR: Last event is different from the I2C_Event        
*****************************************************************************************/
ErrorStatus I2C_EventCheck(I2C_TypeDef* I2Cx, u16 I2C_Event )
{
  u16  LastEvent = I2C_GetLastEvent(I2Cx);

  if (LastEvent == I2C_Event)
  {
    return SUCCESS;
  }
  else
  {
    return ERROR;
  }
}

/*****************************************************************************************
* Function Name  : I2C_FlagStatus  
* Description    : Checks whether the specified I2C flag is set or not.
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral. 
*                  - I2C_Flag: flag to check. This parameter can be one of the
*                    following values:
*                         - I2C_FLAG_SB: Start bit flag    
*                         - I2C_FLAG_M_SL: Master/Slave flag   
*                         - I2C_FLAG_ADSL: Adress matched flag    
*                         - I2C_FLAG_BTF: Byte transfer finished flag    
*                         - I2C_FLAG_BUSY: Bus busy flag    
*                         - I2C_FLAG_TRA: Transmitter/Receiver flag    
*                         - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag   
*                         - I2C_FLAG_EVF: Event flag     
*                         - I2C_FLAG_GCAL: General call flag    
*                         - I2C_FLAG_BERR: Bus error flag    
*                         - I2C_FLAG_ARLO: Arbitration lost flag    
*                         - I2C_FLAG_STOPF: Stop detection flag   
*                         - I2C_FLAG_AF: Acknowledge failure flag      
*                         - I2C_FLAG_ENDAD: End of address transmission flag   
*                         - I2C_FLAG_ACK: Acknowledge enable flag     
* Output         : None	                                                                                 
* Return         : The NewState of the I2C_Flag (SET or RESET).                              
*****************************************************************************************/
FlagStatus I2C_FlagStatus (I2C_TypeDef *I2Cx, u16 I2C_Flag)
{
  u16 Tmp= I2C_GetStatus(I2Cx) & I2C_Flag;

  if (Tmp != 0)
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*****************************************************************************************
* Function Name  : I2C_FlagClear                                                        
* Description    : Clears the I2C Flag passed as a parameter                            
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral. 
*                  - I2C_Flag: flag to check. This parameter can be one of the
*                    following values:
*                         - I2C_FLAG_SB: Start bit flag    
*                         - I2C_FLAG_M_SL: Master/Slave flag   
*                         - I2C_FLAG_ADSL: Adress matched flag    
*                         - I2C_FLAG_BTF: Byte transfer finished flag    
*                         - I2C_FLAG_BUSY: Bus busy flag    
*                         - I2C_FLAG_TRA: Transmitter/Receiver flag    
*                         - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag   
*                         - I2C_FLAG_EVF: Event flag     
*                         - I2C_FLAG_GCAL: General call flag    
*                         - I2C_FLAG_BERR: Bus error flag    
*                         - I2C_FLAG_ARLO: Arbitration lost flag    
*                         - I2C_FLAG_STOPF: Stop detection flag   
*                         - I2C_FLAG_AF: Acknowledge failure flag      
*                         - I2C_FLAG_ENDAD: End of address transmission flag   
*                         - I2C_FLAG_ACK: Acknowledge enable flag                                      
*                  - parameter needed in the case that the flag to be cleared need   
*                  a write in one register  
* Output         : None	                                             
* Return         : None.                                                                
*****************************************************************************************/
void I2C_FlagClear (I2C_TypeDef *I2Cx, u16 I2C_Flag, ...)
{
  u8 Tmp = (u8)*((u32 *) & I2C_Flag + sizeof(I2C_Flag));

  /* flags that need a read of the SR2 register to be cleared */
  if (I2C_Flag==I2C_FLAG_ADD10 || I2C_Flag==I2C_FLAG_EVF || I2C_Flag==I2C_FLAG_BERR || I2C_Flag==I2C_FLAG_ARLO ||
      I2C_Flag==I2C_FLAG_STOPF || I2C_Flag==I2C_FLAG_AF  || I2C_Flag==I2C_FLAG_ENDAD)
  {
    /* Read the SR2 register */
    (void)I2Cx->SR2;

    /* Two flags need a second step to be cleared */
    switch (I2C_Flag)
    {
       case  I2C_FLAG_ADD10: I2Cx->DR = Tmp; break;
       case  I2C_FLAG_ENDAD: I2Cx->CR|=0x20; break;
    }
  }

  /* flags that need a read of the SR1 register to be cleared */
  else if (I2C_Flag==I2C_FLAG_SB || I2C_Flag==I2C_FLAG_ADSL || I2C_Flag==I2C_FLAG_BTF || I2C_Flag==I2C_FLAG_TRA)
       {
         /* Read the SR1 register */
         (void)I2Cx->SR1;

         /* three flags need a second step to be cleared */
         if (I2C_Flag == I2C_FLAG_SB)
           I2Cx->DR=Tmp;
         else
           if (I2C_Flag==I2C_FLAG_BTF || I2C_Flag==I2C_FLAG_TRA)
             Tmp=I2Cx->DR;
       }

       /* flags that need the PE bit to be cleared */
       else if ( I2C_Flag==I2C_FLAG_M_SL || I2C_Flag==I2C_FLAG_GCAL)
            {
              I2C_Cmd(I2Cx, DISABLE);
              I2C_Cmd(I2Cx, ENABLE);
            }
}

/********************************************************************************
* Function Name  : I2C_ByteSend                                                
* Description    : Send a data byte.                                 
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.                  
*                  - I2C_Data : the byte to be sent
* Output         : None	             
* Return         : None.                                                       
********************************************************************************/
void I2C_ByteSend (I2C_TypeDef* I2Cx, u8 I2C_Data)
{
  /* Write in the DR register the byte to be sent */
  I2Cx->DR = I2C_Data;
}

/********************************************************************************
* Function Name  : I2C_ByteReceive                                             
* Description    : Read the received byte. 
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.  
* Output         : None	                                                                  
* Return         : The received byte                                      
********************************************************************************/
u8 I2C_ByteReceive (I2C_TypeDef* I2Cx)
{
  /* Return the byte in the DR register */
  return I2Cx->DR;
}

/********************************************************************************
* Function Name  : I2C_AddressSend                                             
* Description    : Transmits the address byte to select the slave device.      
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.                   
*                  - I2C_Address: the slave address                                     
*                  - I2C_AddMode: the addressing mode. This parameter can be one 
*                    of the following values
*                         - I2C_MODE_ADDRESS7: 7-bit addressing mode 
*                         - I2C_MODE_ADDRESS10: 10-bit addressing mode         
*                  - I2C_Direction: the transmission mode. This parameter can be one 
*                    of the following values
*                         - I2C_MODE_TRANSMITTER: Transmitter mode 
*                         - I2C_MODE_RECEIVER: Receiver mode  
* Output         : None	
* Return         : None.                                                       
********************************************************************************/
void I2C_AddressSend (I2C_TypeDef* I2Cx, u16 I2C_Address, u8 I2C_AddMode, u8 I2C_Direction)
{
  if (I2C_AddMode == I2C_MODE_ADDRESS10 )
  /* 10 bit addressing mode */
  {
    /* Send the Header with bit0 Reset for write */
    I2Cx->DR = ((I2C_Address>>7) | 0xf0) & 0xfe;
    /* Test on EV9 and clear it */
    while( ! I2C_EventCheck(I2Cx, I2C_EVENT_MASTER_MODE_ADDRESS10) );  /* EV9*/
    /* Send the low byte of the slave address */
    I2C_ByteSend(I2Cx, (u8)I2C_Address);

    /* Test on the direction to define the read/write bit */
    if (I2C_Direction == I2C_MODE_RECEIVER)
    {
      /* Test on EV6 and clear it */
      while( ! I2C_EventCheck(I2Cx, I2C_EVENT_MASTER_MODE_SELECTED) );  /* EV6 */
      /* Clear EV6 by set again the PE bit */
      I2C_Cmd (I2Cx, ENABLE);
      /* Repeated START Generate */
      I2C_STARTGenerate (I2Cx, ENABLE);
      /* Test on EV5 and clear it */
      while( ! I2C_EventCheck(I2Cx, I2C_EVENT_MASTER_MODE_SELECT) );  /* EV5 */
      /* Send the Header with bit0 Set for read */
      I2Cx->DR = ((I2C_Address>>7) | 0xf1);
    }
  }
  else
  /* 7 bit addressing mode */
  {
    if (I2C_Direction == I2C_MODE_RECEIVER)
      I2C_Address|=0x01;
    else
      I2C_Address&=~0x01;

    I2Cx->DR=(u8)I2C_Address;
  }
}

/********************************************************************************
* Function Name  : I2C_BufferSend                                              
* Description    : Transmits data from buffer. This function will return an error
*                  in case of a transmission failure.      
* Input          : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.                 
*                  - PtrToBuffer: 憉8

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -