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

📄 st79_usart.c

📁 st公司新出的一款8位单片机st79的lib库
💻 C
📖 第 1 页 / 共 3 页
字号:
        }
		break;

        /*< Returns the Receiver Interrupt Enable bit state              */
    case USART_IT_RIEN:
        if (USARTx->CR2 & USART_CR2_RIEN)
        {
            return SET;
        }
        else
        {
            return RESET;
        }
		break;

        /*< Returns the Idle Line Interrupt Enable bit state             */
    case USART_IT_ILIEN:
        if (USARTx->CR2 & USART_CR2_ILIEN)
        {
            return SET;
        }
        else
        {
            return RESET;
        }
		break;
        /*< Returns the LIN Break Detection Interrupt Enable bit state   */
    case USART_IT_LBDIEN:
        if (USARTx->CR4 & USART_CR4_LBDIEN)
        {
            return SET;
        }
        else
        {
            return RESET;
        }
		break;
    default:
			return SET;
        break;
    }
}


/**
  * @brief Enables or disables the USART抯 Half Duplex communication.
  * @par Full description:
  * Enables or disables the USART抯 Half Duplex communication.
  * @param[in] USARTx can be only USART.
  * @param[in] NewState new state of the USART Communication.
*                    This parameter can be: ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_HalfDuplexCmd(USART, ENABLE);
  * @endcode
  */
void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
    assert(IS_STATE_VALUE_OK(NewState));

    if (NewState)
    {
        USARTx->CR5 |= USART_CR5_HDSEL;  /**< USART Half Duplex Enable  */
    }
    else
    {
        USARTx->CR5 &= (u8)~USART_CR5_HDSEL; /**< USART Half Duplex Disable */
    }
}

/**
  * @brief Initializes the USART peripheral.
  * @par Full description:
  * Initializes the USART peripheral according to the specified
  * parameters .
  * @param[in] USARTx can be only USART,
  * @param[in] USART_WordLength can be USART_WordLength_8D or USART_WordLength_9D to select the lenght of data frame,
  * @param[in] USART_StopBits can 0.5, 1, 1.5 or 2 stopbits,
  * @param[in] USART_Parity can be USART_Parity_No, USART_Parity_Odd or USART_Parity_Even,
  * @param[in] USART_SyncMode USART_SyncMode_TypeDef values,
  * @param[in] USART_BaudRate the baudrate,
  * @param[in] MasterFrequency can be obtained using the CLK_GetClocksFreq function,
  * @param[in] USART_Mode USART_Mode_TypeDef,
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_Init(USART, USART_InitStruct)
  * @endcode
  */
void USART_Init(USART_TypeDef* USARTx,
                USART_WordLength_TypeDef USART_WordLength,
                USART_StopBits_TypeDef USART_StopBits,
                USART_Parity_TypeDef USART_Parity,
                USART_SyncMode_TypeDef USART_SyncMode,
                u32 USART_BaudRate,
                u32 MasterFrequency,
                USART_Mode_TypeDef USART_Mode)
{

    u32 BaudRate_Mantissa, BaudRate_Mantissa100;

    assert(IS_USART_WordLength_VALUE_OK(USART_WordLength));

    assert(IS_USART_StopBits_VALUE_OK(USART_StopBits));

    assert(IS_USART_Parity_VALUE_OK(USART_Parity));

    /* Assert: BaudRate value should be <= 625000 bps */
    assert(IS_USART_BAUDRATE_OK(USART_BaudRate));

    /* Assert: USART_Mode value should exclude values such as  USART_ModeTx_Enable|USART_ModeTx_Disable */
    assert(IS_USART_MODE_VALUE_OK((u8)USART_Mode));

    /* Assert: USART_SyncMode value should exclude values such as
       USART_Clock_Enable|USART_Clock_Disable */
    assert(IS_USART_SyncMode_VALUE_OK((u8)USART_SyncMode));

    /* Wait for no Transmition before modifying the M bit */
    /* while(!(USARTx->SR&USART_SR_TC));      */

    USARTx->CR1 &= (u8)(~USART_CR1_M);     /**< Clear the word length bit */
    USARTx->CR1 |= (u8)USART_WordLength; /**< Set the word length bit according to USART_WordLength value */

    USARTx->CR3 &= (u8)(~USART_CR3_STOP);  /**< Clear the STOP bits */
    USARTx->CR3 |= (u8)USART_StopBits;  /**< Set the STOP bits number according to USART_StopBits value  */

    USARTx->CR1 &= (u8)(~(USART_CR1_PCEN| USART_CR1_PS  ));  /**< Clear the Parity Control bit */
    USARTx->CR1 |= (u8)USART_Parity;     /**< Set the Parity Control bit to USART_Parity value */

    USARTx->BRR1 &= (u8)(~USART_BRR1_DIVM);  /**< Clear the LSB mantissa of USARTDIV  */
    USARTx->BRR2 &= (u8)(~USART_BRR2_DIVM);  /**< Clear the MSB mantissa of USARTDIV  */
    USARTx->BRR2 &= (u8)(~USART_BRR2_DIVF);  /**< Clear the Fraction bits of USARTDIV */

    /**< Set the USART BaudRates in BRR1 and BRR2 registers according to USART_BaudRate value */
    BaudRate_Mantissa    = (MasterFrequency / (USART_BaudRate << 4));
    BaudRate_Mantissa100 = ((MasterFrequency * 100) / (USART_BaudRate << 4));
    USARTx->BRR2 |= (u8)((u8)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100)) << 4) / 100) & (u8)0x0F); /**< Set the fraction of USARTDIV  */
    USARTx->BRR2 |= (u8)((BaudRate_Mantissa >> 4) & (u8)0xF0); /**< Set the MSB mantissa of USARTDIV  */
    USARTx->BRR1 |= (u8)BaudRate_Mantissa;           /**< Set the LSB mantissa of USARTDIV  */

    USARTx->CR2 &= (u8)~(USART_CR2_TEN | USART_CR2_REN); /**< Disable the Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits */
    USARTx->CR3 &= (u8)~(USART_CR3_CPOL | USART_CR3_CPHA | USART_CR3_LBCL); /**< Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
    USARTx->CR3 |= (u8)((u8)USART_SyncMode & (u8)(USART_CR3_CPOL | USART_CR3_CPHA| USART_CR3_LBCL));  /**< Set the Clock Polarity, lock Phase, Last Bit Clock pulse */

    if ((u8)USART_Mode & (u8)USART_Mode_Tx_Enable)
    {
        USARTx->CR2 |= (u8)USART_CR2_TEN;  /**< Set the Transmitter Enable bit */
    }
    else
    {
        USARTx->CR2 &= (u8)(~USART_CR2_TEN);  /**< Clear the Transmitter Disable bit */
    }
    if ((u8)USART_Mode & (u8)USART_Mode_Rx_Enable)
    {
        USARTx->CR2 |= (u8)USART_CR2_REN;  /**< Set the Receiver Enable bit */
    }
    else
    {
        USARTx->CR2 &= (u8)(~USART_CR2_REN);  /**< Clear the Receiver Disable bit */
    }
    /**< Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit Clock pulse bits according to USART_Mode value */
    if ((u8)USART_SyncMode&(u8)USART_Clock_Disable)
    {
        USARTx->CR3 &= (u8)(~USART_CR3_CLKEN); /**< Clear the Clock Enable bit */
        /**< configure in Push Pull or Open Drain mode the Tx I/O line by setting the correct I/O Port register according the product package and line configuration*/
    }
    else
    {
        USARTx->CR3 |= (u8)((u8)USART_SyncMode & USART_CR3_CLKEN);
        /* USARTx->CR2 &= (u8)(~USART_CR2_REN);*/ /*TBD */
    }
}

/**
  * @brief Configures the USART抯 IrDA interface.
  * @par Full description:
  * Configures the USART抯 IrDA interface.
  * @par This function is valid only for USART.
  * @param[in] USARTx can be only USART .
  * @param[in] USART_IrDAMode specifies the IrDA mode.
  *                    This parameter can be one of the following values:
  *                       - USART_IrDAMode_LowPower
  *                       - USART_IrDAMode_Normal
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_IrDAConfig(USART, USART_IrDAMode_LowPower)
  * @endcode
  */
void USART_IrDAConfig(USART_TypeDef* USARTx, USART_IrDAMode_TypeDef USART_IrDAMode)
{
    assert(IS_USART_IrDAMode_VALUE_OK(USART_IrDAMode));

    if (USART_IrDAMode == USART_IrDAMode_Normal)
    {
        USARTx->CR5 &= ((u8)~USART_CR5_IRLP);
    }
    else
    {
        USARTx->CR5 |= USART_CR5_IRLP;
    }
}

/**
  * @brief Enables or disables the USART抯 IrDA interface.
  * @par Full description:
  * Enables or disables the USART抯 IrDA interface.
  *@par This function is valid only for USART
 * @param[in] USARTx can be only USART (USART has no IrDA interface).
  * @param[in] NewState new state of the IrDA mode.
  *                    This parameter can be: ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_IrDACmd(USART, ENABLE)
  * @endcode
  */
void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
    assert(IS_STATE_VALUE_OK(NewState));

    if (NewState)
    {
        /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
        USARTx->CR5 |= USART_CR5_IREN;
    }
    else
    {
        /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
        USARTx->CR5 &= ((u8)~USART_CR5_IREN);
    }
}

/**
  * @brief Enables or disables the specified USART interrupts.
  * @par Full description:
  * Enables or disables the specified USART interrupts.
  * @param[in] USARTx can be only USART to select the peripheral.
  * @param[in] USART_IT specifies the USART interrupt sources to be
  *                    enabled or disabled.
  *                    This parameter can be one of the following values:
  *                       - USART_IT_PIEN
  *                       - USART_IT_TCIEN
  *                       - USART_IT_RIEN
  *                       - USART_IT_ILIEN
  *                       - USART_IT_RIEN
   *                      - USART_IT_LBDIEN
   *		        - USART_IT_EIE
  * @param[in] NewState new state of the specified USART interrupts.
 *                    This parameter can be: ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_ITConfig(USART, USART_IT_PIEN, ENABLE)
  * @endcode
  */
void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT, FunctionalState NewState)
{
    assert(IS_USART_IT_VALUE_OK(USART_IT));
    assert(IS_STATE_VALUE_OK(NewState));

    if (NewState)
    {
        /**< Enable the Interrupt bits according to USART_IT mask */
        USARTx->CR2 |= (u8)((u8)(USART_IT) & (u8)(USART_CR2_TIEN | USART_CR2_TCIEN | USART_CR2_RIEN | USART_CR2_ILIEN));
        USARTx->CR1 |= (u8)((u8)USART_IT & USART_CR1_PIEN);
        USARTx->CR4 |= (u8)((u8)((u8)USART_IT << 4) & USART_CR4_LBDIEN);
    }
    else
    {
        /**< Disable the interrupt bits according to USART_IT mask */
        USARTx->CR2 &= (u8)(~((u8)(USART_IT) & (u8)(USART_CR2_TIEN | USART_CR2_TCIEN | USART_CR2_RIEN | USART_CR2_ILIEN)));
        USARTx->CR1 &= (u8)(~((u8)USART_IT & USART_CR1_PIEN));
        USARTx->CR4 &= (u8)(~((u8)((u8)USART_IT << 4) & USART_CR4_LBDIEN));
    }

}


/**
  * @brief Sets the USART LIN Break detection length.
  * @par Full description:
  * Sets the USART LIN Break detection length.
  * @param[in] USARTx can be USART or USART to select the peripheral.
  * @param[in] USART_LINBreakDetectionLength specifies the LIN break
  *                    detection length.
  *                    This parameter can be one of the following values:
  *                       - USART_LINBreakDetectionLength10
  *                       - USART_LINBreakDetectionLength11
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_LINBreakDetectionConfig(USART, USART_LINBreakDetectionLength11)
  * @endcode
  */
void USART_LINBreakDetectionConfig(USART_TypeDef* USARTx, USART_LINBreakDetectionLength_TypeDef USART_LINBreakDetectionLength)
{
    assert(IS_USART_LINBreakDetectionLength_VALUE_OK(USART_LINBreakDetectionLength));

    if (USART_LINBreakDetectionLength == USART_LINBreakDetectionLength10)
    {
        USARTx->CR4 &= ((u8)~USART_CR4_LBDL);
    }
    else
    {
        USARTx->CR4 |= USART_CR4_LBDL;
    }
}


/**
  * @brief Enables or disables the USART抯 LIN mode.
  * @par Full description:
  * Enables or disables the USART抯 LIN mode.
  * @param[in] USARTx can be USART or USART to select the peripheral.
  * @param[in] NewState is new state of the USART LIN mode.
  *                    This parameter can be: ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * USART_LINCmd(USART, ENABLE)
  * @endcode
  */
void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
    assert(IS_STATE_VALUE_OK(NewState));

    if (NewState)
    {
        /* Enable the LIN mode by setting the LINE bit in the CR2 register */
        USARTx->CR3 |= USART_CR3_LINEN;
    }
    else
    {
        /* Disable the LIN mode by clearing the LINE bit in the CR2 register */

⌨️ 快捷键说明

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