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

📄 i2c.c

📁 freemodbus-v1-1-1-0.zip v1.1.1版本的代码 支持多个平台
💻 C
📖 第 1 页 / 共 2 页
字号:
  else  // 7 bit addressing mode  {    if (Direction == I2C_RX) Address|=0x01; else Address&=~0x01;    I2Cx->DR=(u8)Address;  }}/******************************************************************************** Function Name  : I2C_ByteSend* Description    : Send a single byte of data.* Input          : I2Cx ( I2C0 or I2C1 )*                  Data : the byte to be sent to the slave* Return         : None.*******************************************************************************/void I2C_ByteSend (I2C_TypeDef *I2Cx, u8 Data){  //Write in the DR register the byte to be sent  I2Cx->DR = Data;}/******************************************************************************** Function Name  : I2C_BufferSend* Description    : Send data from a buffer whose number of bytes is known* Input          : I2Cx ( I2C0 or I2C1 )*                  PtrToBuffer :pointer to the byte of buffer to be transmitted.*                  NbOfBytes:Number of byte of the buffer* Return         : I2C_Tx_Status :transmission status (I2C_TX_AF, I2C_TX_ARLO,*                  I2C_TX_BERR,I2C_TX_DATA_OK)*******************************************************************************/I2C_Tx_Status I2C_BufferSend (I2C_TypeDef *I2Cx, u8 *PtrToBuffer, u8 NbOfBytes){  u8 Interruption_Status;  u8 SentBytes;  u8 error;  I2C_Tx_Status I2C_TXTMP;  //Test of the interrupt status  Interruption_Status=0;  Interruption_Status=I2Cx->CR & 0x01;  I2C_ITConfig (I2Cx, DISABLE);  //Wait till I2C_BTF bit is set  while ((I2Cx->SR1 & 0x08 )==0);  SentBytes=0;  while (SentBytes<NbOfBytes)  {    I2Cx->DR= *(PtrToBuffer+SentBytes);    //Wait till I2C_BTF bit is set or error detected    do      error = I2Cx->SR2 & 0x16;    while ((I2Cx->SR1 & 0x08)==0 && error==0);    // In case of error exit    if (error) break;    // increment SentBytes counter    SentBytes++;  }  if (error)  {    if (error & 0x10)      // I2C_TX_AF if Acknowledge failure is detected      I2C_TXTMP = I2C_TX_AF;    if (error & 0x02)      //I2C_TX_ARLO if the ARLO bit is set in the SR2 register      I2C_TXTMP = I2C_TX_ARLO;    if (error & 0x04)      // I2C_TX_BERR if the BERR bit is set in the SR2 register      I2C_TXTMP=  I2C_TX_BERR;  }  else    //I2C_TX_DATA_OK to show that the buffer is well sent    I2C_TXTMP= I2C_TX_DATA_OK;  //Restore the interrupt status  if (Interruption_Status==1)  I2C_ITConfig (I2Cx, ENABLE);  return I2C_TXTMP;}/******************************************************************************** Function Name  : I2C_StringSend* Description    : Send data from a buffer* Input          : I2Cx ( I2C0 or I2C1 )*                  PtrToBuffer :pointer to the byte of string to be transmitted.* Return         : I2C_Tx_Status :transmission status (I2C_TX_AF, I2C_TX_ARLO,*                  I2C_TX_BERR,I2C_TX_DATA_OK)*******************************************************************************/I2C_Tx_Status I2C_StringSend (I2C_TypeDef *I2Cx, char *PtrToString){  u8 NbOfBytes=0;  // count the number of byte composing the string passed as parameter.  while (*(PtrToString+NbOfBytes)!= '\0')    NbOfBytes++;  // call I2C_BufferSend function to execute the send part  return I2C_BufferSend (I2Cx,(u8 *)PtrToString,NbOfBytes);}/******************************************************************************** Function Name  : I2C_TransmissionStatus* Description    : Report the NewState of the transmission* Input          : I2Cx ( I2C0 or I2C1 )* Return         : I2C_Tx_Status :transmission status (I2C_TX_NO, I2C_TX_SB,*                   I2C_TX_AF, I2C_TX_ARLO, I2C_TX_BERR,I2C_TX_ADD_OK,*                   I2C_TX_DATA_OK,I2C_TX_ONGOING)*******************************************************************************/I2C_Tx_Status I2C_TransmissionStatus (I2C_TypeDef *I2Cx){  u8 SR1value;  u8 SR2value;  I2C_Tx_Status NewState;  SR1value = I2Cx->SR1;  SR2value = I2Cx->SR2;  if ((I2Cx->SR1&0x10)==0)    NewState=I2C_TX_NO;  else if (I2Cx->SR1&0x01)    //I2C_SB bit is set    NewState=I2C_TX_SB;  else if ((SR2value & 0x10)&&(I2Cx->CR&0x04))    //I2C_ACK &I2C_AF are both set    NewState=I2C_TX_AF;  else if (SR2value & 0x04)    //I2C_ARLO is set in multimaster mode    NewState=I2C_TX_ARLO;  else if (SR2value & 0x02)    //I2C_BERR bit is set    NewState=I2C_TX_BERR;  else if ((SR1value & 0x80)&& (I2Cx->SR2&0x20))    //I2C_EVF and I2C_ENDAD are both set    NewState=I2C_TX_ADD_OK;  else if ((I2Cx->SR1&0x20)&& (I2Cx->SR1&0x08))    //I2C_TRA and I2C_BTF are both set    NewState=I2C_TX_DATA_OK;  else    NewState=I2C_TX_ONGOING;  return NewState;}/******************************************************************************** Function Name  : I2C_ByteReceive* Description    : Returns the received byte.* Input          : I2Cx ( I2C0 or I2C1 )* Return         : the byte received*******************************************************************************/u8 I2C_ByteReceive (I2C_TypeDef *I2Cx){   //Wait till I2C_BTF bit is set  while ((I2Cx->SR1 & 0x08)==0);  return I2Cx->DR;}/******************************************************************************** Function Name  : I2C_BufferReceive* Description    : received a buffer. and return the status of error.* Input          : I2Cx ( I2C0 or I2C1 )*                  PtrToBuffer :pointer to the byte of buffer received.*                  NbOfBytes:Number of byte to be received* Return         : I2C_Rx_Status:the NewState of the reception (,I2C_RX_AF,*                               I2C_RX_ARLO,I2C_RX_BERR, I2C_RX_DATA_OK)*******************************************************************************/I2C_Rx_Status I2C_BufferReceive (I2C_TypeDef *I2Cx, u8 *PtrToBuffer, u8 NbOfBytes){  u8 Interruption_Status;  u8 ReceivedBytes;  u8 error;  I2C_Rx_Status I2C_RXTMP;  //Test of the interrupt status  Interruption_Status=0;  Interruption_Status=I2Cx->CR & 0x01;  I2C_ITConfig (I2Cx, DISABLE);  ReceivedBytes=0;  while (ReceivedBytes<NbOfBytes)  {    do      error = I2Cx->SR2 & 0x16;    while ((I2Cx->SR1 & 0x08)==0 && !error);    if (error==0)    // No error detected    {      *(PtrToBuffer+ReceivedBytes) = I2Cx->DR;      ReceivedBytes++;    }    else break;  }  if (error)  {    if (error & 0x10)      // I2C_RX_AF if Acknowledge failure is detected      I2C_RXTMP= I2C_RX_AF;    else if (error & 0x02)      // I2C_RX_ARLO if the ARLO bit is set in the SR2 register      I2C_RXTMP=  I2C_RX_ARLO;    else if (error & 0x04)      // I2C_RX_BERR if the BERR bit is set in the SR2 register      I2C_RXTMP=  I2C_RX_BERR;  }  else    //I2C_RX_DATA_OK to show that the buffer is well sent    I2C_RXTMP= I2C_RX_DATA_OK;  //Restore the interrupt status  if (Interruption_Status==1)  I2C_ITConfig (I2Cx, ENABLE);  return I2C_RXTMP;}/******************************************************************************** Function Name  :I2C_ReceptionStatus* Description    : Report the reception NewState.* Input          : I2Cx ( I2C0 or I2C1 )* Return         : I2C_Rx_Status:the NewState of the reception ( I2C_RX_NO,*                  I2C_RX_SB,I2C_RX_AF,I2C_RX_ARLO,I2C_RX_BERR,I2C_RX_ADD_OK,*                  I2C_RX_DATA_OK, I2C_RX_ONGOING)*******************************************************************************/I2C_Rx_Status I2C_ReceptionStatus (I2C_TypeDef *I2Cx){  u8 SR1value;  u8 SR2value;  I2C_Rx_Status NewState;  SR1value= I2Cx->SR1;  SR2value= I2Cx->SR2;  if ((I2Cx->SR1&0x10) == 0)    NewState=I2C_RX_NO;  else if (I2Cx->SR1&0x01)    //I2C_SB bit is set    NewState=I2C_RX_SB;  else if ((SR2value & 0x10) && (I2Cx->CR&0x04))    //I2C_ACK &I2C_AF are both set    NewState=I2C_RX_AF;  else if (SR2value & 0x04)    //I2C_ARLO is set    NewState=I2C_RX_ARLO;  else if (SR2value & 0x02)    //I2C_BERR bit is set    NewState=I2C_RX_BERR;  else if ((SR1value & 0x80) && (I2Cx->SR1&0x08)==0)    //2C_EVF is set & I2C_BTF is not set    NewState=I2C_RX_ADD_OK;  else if ((I2Cx->SR1&0x20)==0 && (I2Cx->SR1&0x08))    //II2C_TRA is cleared & I2C_BTF is set    NewState=I2C_RX_DATA_OK;  else  NewState=I2C_RX_ONGOING;  return NewState;}/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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