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

📄 stm8s_uart2.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 3 页
字号:
  * None
  * @par Example:
  * The UART2 guard time counter count up to 0x44 until TC is assert_paramed high.
  * @code
    * UART2_SetGuardTime(0x44);
  * @endcode
  */
void UART2_SetGuardTime(u8 UART2_GuardTime)
{
  /* Set the UART2 guard time */
  UART2->GTR = UART2_GuardTime;
}

/**
  * @brief Sets the system clock prescaler.
  * @par Full description:
  * Sets the system clock prescaler.
  * @par This function is related to SmartCard and IrDa mode.
  * @param[in] UART2_Prescaler: specifies the prescaler clock.
  *                    This parameter can be one of the following values:
  *                       @par IrDA Low Power Mode
  *   The clock source is diveded by the value given in the register (8 bits)
  *                       - 0000 0000 Reserved
  *                       - 0000 0001 divides the clock source by 1
  *                       - 0000 0010 divides the clock source by 2
  *                       - ...........................................................
  *                       @par Smart Card Mode
  *   The clock source is diveded by the value given in the register (5 significant bits) multipied by 2
  *                       - 0 0000 Reserved
  *                       - 0 0001 divides the clock source by 2
  *                       - 0 0010 divides the clock source by 4
  *                       - 0 0011 divides the clock source by 6
  *                       - ...........................................................
  * @retval void None
  * @par Required preconditions:
  * IrDA Low Power mode or smartcard mode enabled
  * @par Called functions:
  * None
  * @par Example:
  * Sets UART2 prescalerfor SmartCard divided by 2.
  * @code
  * UART2_SmartCardCmd(ENABLE);
  * UART2_SetPrescaler(0x01);
  * @endcode
  */
void UART2_SetPrescaler(u8 UART2_Prescaler)
{
  /* Load the UART2 prescaler value*/
  UART2->PSCR = UART2_Prescaler;
}

/**
  * @brief Checks whether the specified UART2 flag is set or not.
  * @par Full description:
  * Checks whether the specified UART2 flag is set or not.
  * @param[in] UART2_FLAG specifies the flag to check.
  * This parameter can be any of the @ref UART2_Flag_TypeDef enumeration.
  * @retval FlagStatus (SET or RESET)
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Check the status of TC flag.
  * @code
  * FlagStatus TC_flag;
  * TC_Flag = UART2_GetFlagStatus(UART2_FLAG_TC);
  * @endcode
  */

FlagStatus UART2_GetFlagStatus(UART2_Flag_TypeDef UART2_FLAG)
{
  FlagStatus status = RESET;

  /* Check parameters */
  assert_param(IS_UART2_FLAG_OK(UART2_FLAG));

  /* Check the status of the specified UART2 flag*/
  if (UART2_FLAG == UART2_FLAG_LBDF)
  {
    if ((UART2->CR4 & (u8)UART2_FLAG) != (u8)0x00)
    {
      /* UART2_FLAG is set*/
      status = SET;
    }
    else
    {
      /* UART2_FLAG is reset*/
      status = RESET;
    }
  }
  else if (UART2_FLAG == UART2_FLAG_SBK)
  {
    if ((UART2->CR2 & (u8)UART2_FLAG) != (u8)0x00)
    {
      /* UART2_FLAG is set*/
      status = SET;
    }
    else
    {
      /* UART2_FLAG is reset*/
      status = RESET;
    }
  }
  else if ((UART2_FLAG == UART2_FLAG_LHDF) || (UART2_FLAG == UART2_FLAG_LSF))
  {
    if ((UART2->CR6 & (u8)UART2_FLAG) != (u8)0x00)
    {
      /* UART2_FLAG is set*/
      status = SET;
    }
    else
    {
      /* UART2_FLAG is reset*/
      status = RESET;
    }
  }
  else
  {
    if ((UART2->SR & (u8)UART2_FLAG) != (u8)0x00)
    {
      /* UART2_FLAG is set*/
      status = SET;
    }
    else
    {
      /* UART2_FLAG is reset*/
      status = RESET;
    }
  }

  /* Return the UART2_FLAG status*/
  return  status;
}
/**
 * @brief Clears the UART2 flags.
 * @par Full description:
 * Clears the UART2 flags.
 * @param[in] UART2_FLAG specifies the flag to clear
 * This parameter can be any combination of the following values:
 *   - UART2_FLAG_LBDF: LIN Break detection flag.
 *   - UART2_FLAG_LHDF: LIN Header detection flag.
 *   - UART2_FLAG_LSF: LIN synchrone field flag.
 *   - UART2_FLAG_RXNE: Receive data register not empty flag.
 * @par Notes:
 *   - PE (Parity error), FE (Framing error), NE (Noise error), OR (OverRun error)
 *     and IDLE (Idle line detected) flags are cleared by software sequence: a read
 *     operation to UART2_SR register (UART2_GetFlagStatus())followed by a read operation
 *     to UART2_DR register(UART2_ReceiveData8() or UART2_ReceiveData9()).
 *   - RXNE flag can be also cleared by a read to the UART2_DR register
 *     (UART2_ReceiveData8()or UART2_ReceiveData9()).
 *   - TC flag can be also cleared by software sequence: a read operation to UART2_SR
 *     register (UART2_GetFlagStatus()) followed by a write operation to UART2_DR register
 *     (UART2_SendData8() or UART2_SendData9()).
 *   - TXE flag is cleared only by a write to the UART2_DR register (UART2_SendData8() or
 *     UART2_SendData9()).
 *   - SBK flag is cleared during the stop bit of break.
 * @retval void None
 * @par Required preconditions:
 * None
 * @par Called functions:
 * None
 * @par Example:
 * Clear the UART2 LBDF flag
 * @code
 * UART2_ClearFlag(UART2_FLAG_LBDF);
 * @endcode
 */

void UART2_ClearFlag(UART2_Flag_TypeDef UART2_FLAG)
{
  assert_param(IS_UART2_CLEAR_FLAG_OK(UART2_FLAG));

  /*< Clear the Receive Register Not Empty flag */
  if (UART2_FLAG == UART2_FLAG_RXNE)
  {
    UART2->SR = (u8)~(UART2_SR_RXNE);
  }
  /*< Clear the LIN Break Detection flag */
  else if (UART2_FLAG == UART2_FLAG_LBDF)
  {
    UART2->CR4 &= (u8)(~UART2_CR4_LBDF);
  }
  /*< Clear the LIN Header Detection Flag */
  else if (UART2_FLAG == UART2_FLAG_LHDF)
  {
    UART2->CR6 &= (u8)(~UART2_CR6_LHDF);
  }
  /*< Clear the LIN Synch Field flag */
  else
  {
    UART2->CR6 &= (u8)(~UART2_CR6_LSF);
  }

}

/**
  * @brief Checks whether the specified UART2 interrupt has occurred or not.
  * @par Full description:
  * Checks whether the specified UART2 interrupt has occurred or not.
  * @param[in] UART2_IT: Specifies the UART2 interrupt pending bit to check.
  * This parameter can be one of the following values:
  *   - UART2_IT_LBDF:  LIN Break detection interrupt
  *   - UART2_IT_TXE:  Tansmit Data Register empty interrupt
  *   - UART2_IT_TC:   Transmission complete interrupt
  *   - UART2_IT_RXNE: Receive Data register not empty interrupt
  *   - UART2_IT_IDLE: Idle line detection interrupt
  *   - UART2_IT_OR:  OverRun Error interrupt
  *   - UART2_IT_PE:   Parity Error interrupt
  * @retval
  * ITStatus The new state of UART2_IT (SET or RESET).
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * IT_Status Status;
  * Status = UART2_GetITStatus(UART2_IT_TC);
  * @endcode
  */
ITStatus UART2_GetITStatus(UART2_IT_TypeDef UART2_IT)
{
  ITStatus pendingbitstatus = RESET;
  u8 itpos = 0;
  u8 itmask1 = 0;
  u8 itmask2 = 0;
  u8 enablestatus = 0;

  /* Check parameters */
  assert_param(IS_UART2_GET_IT_OK(UART2_IT));

  /* Get the UART2 IT index*/
  itpos = (u8)((u8)1 << (u8)((u8)UART2_IT & (u8)0x0F));
  /* Get the UART2 IT index*/
  itmask1 = (u8)((u8)UART2_IT >> (u8)4);
  /* Set the IT mask*/
  itmask2 = (u8)((u8)1 << itmask1);



  /* Check the status of the specified UART2 pending bit*/
  if (UART2_IT == UART2_IT_PE)
  {
    /* Get the UART2_ITPENDINGBIT enable bit status*/
    enablestatus = (u8)((u8)UART2->CR1 & itmask2);
    /* Check the status of the specified UART2 interrupt*/

    if (((UART2->SR & itpos) != (u8)0x00) && enablestatus)
    {
      /* Interrupt occurred*/
      pendingbitstatus = SET;
    }
    else
    {
      /* Interrupt not occurred*/
      pendingbitstatus = RESET;
    }
  }

  else if (UART2_IT == UART2_IT_LBDF)
  {
    /* Get the UART2_IT enable bit status*/
    enablestatus = (u8)((u8)UART2->CR4 & itmask2);
    /* Check the status of the specified UART2 interrupt*/
    if (((UART2->CR4 & itpos) != (u8)0x00) && enablestatus)
    {
      /* Interrupt occurred*/
      pendingbitstatus = SET;
    }
    else
    {
      /* Interrupt not occurred*/
      pendingbitstatus = RESET;
    }
  }
  else if (UART2_IT == UART2_IT_LHDF)
  {
    /* Get the UART2_IT enable bit status*/
    enablestatus = (u8)((u8)UART2->CR6 & itmask2);
    /* Check the status of the specified UART2 interrupt*/
    if (((UART2->CR6 & itpos) != (u8)0x00) && enablestatus)
    {
      /* Interrupt occurred*/
      pendingbitstatus = SET;
    }
    else
    {
      /* Interrupt not occurred*/
      pendingbitstatus = RESET;
    }
  }
  else
  {
    /* Get the UART2_IT enable bit status*/
    enablestatus = (u8)((u8)UART2->CR2 & itmask2);
    /* Check the status of the specified UART2 interrupt*/
    if (((UART2->SR & itpos) != (u8)0x00) && enablestatus)
    {
      /* Interrupt occurred*/
      pendingbitstatus = SET;
    }
    else
    {
      /* Interrupt not occurred*/
      pendingbitstatus = RESET;
    }
  }
  /* Return the UART2_IT status*/
  return  pendingbitstatus;
}

/**
 * @brief Clears the UART2 pending flags.
 * @par Full description:
 * Clears the UART2 pending bit.
 * @param[in] UART2_IT specifies the pending bit to clear
 * This parameter can be one of the following values:
 *   - UART2_IT_LBDF:  LIN Break detection interrupt
 *   - UART2_IT_LHDF:  LIN Header detection interrupt
 *   - UART2_IT_RXNE: Receive Data register not empty interrupt.
 *
 * @par Notes:
 *   - PE (Parity error), FE (Framing error), NE (Noise error), OR (OverRun error) and
 *     IDLE (Idle line detected) pending bits are cleared by software sequence: a read
 *     operation to UART2_SR register (UART2_GetITStatus()) followed by a read operation
 *     to UART2_DR register (UART2_ReceiveData8() or UART2_ReceiveData9() ).
 *   - RXNE pending bit can be also cleared by a read to the UART2_DR register
 *     (UART2_ReceiveData8() or UART2_ReceiveData9() ).
 *   - TC (Transmit complet) pending bit can be cleared by software sequence: a read
 *     operation to UART2_SR register (UART2_GetITStatus()) followed by a write operation
 *     to UART2_DR register (UART2_SendData8()or UART2_SendData9()).
 *   - TXE pending bit is cleared only by a write to the UART2_DR register
 *     (UART2_SendData8() or UART2_SendData9()).
 * @retval void None
 * @par Required preconditions:
 * None
 * @par Called functions:
 * None
 * @par Example:
 * Clear the UART2 LBDF pending bit
 * @code
 * UART2_ClearITPendingBit(UART2_IT_LBDF);
 * @endcode
 */
void UART2_ClearITPendingBit(UART2_IT_TypeDef UART2_IT)
{
  u8 dummy = 0;
  assert_param(IS_UART2_CLEAR_IT_OK(UART2_IT));

  /*< Clear the Receive Register Not Empty pending bit */
  if (UART2_IT == UART2_IT_RXNE)
  {
    UART2->SR = (u8)~(UART2_SR_RXNE);
  }
  /*< Clear the LIN Break Detection pending bit */
  else if (UART2_IT == UART2_IT_LBDF)
  {
    UART2->CR4 &= (u8)~(UART2_CR4_LBDF);
  }
  /*< Clear the LIN Header Detection pending bit */
  else
  {
    UART2->CR6 &= (u8)(~UART2_CR6_LHDF);
  }
}
/**
  * @}
  */


/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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