文章目录
- 共同点
- 不同点
- USART_GetITStatus函数详解
- USART_GetFlagStatus函数
共同点
都能访问串口的SR寄存器
不同点
USART_GetFlagStatus(USART_TypeDef USARTx, uint16_t USART_FLAG):*
该函数只判断标志位(访问串口的SR寄存器)。在没有使能相应的中断函数时,通常使用该函数来判断标志位是否置1
USART_GetITStatus(USART_TypeDef USARTx, uint16_t USART_IT) :*
不仅会判断标志位是否置1(访问串口的SR寄存器),同时还会判断是否使能了相应的中断(访问串口的CR1寄存器)。所以在串口中断函数中,如果要获取中断标志位,通常使用该函数。USART_GetITStatus判定的是事件标志和使能控制位,这两个位同时为1返回值才为1
USART_GetITStatus函数详解
#define USART_IT_PE ((uint16_t)0x0028)
#define USART_IT_TXE ((uint16_t)0x0727)
#define USART_IT_TC ((uint16_t)0x0626)
#define USART_IT_RXNE ((uint16_t)0x0525)
#define USART_IT_ORE_RX ((uint16_t)0x0325) /* In case interrupt is generated if the RXNEIE bit is set */
#define USART_IT_IDLE ((uint16_t)0x0424)
#define USART_IT_LBD ((uint16_t)0x0846)
#define USART_IT_CTS ((uint16_t)0x096A)
#define USART_IT_ERR ((uint16_t)0x0060)
#define USART_IT_ORE_ER ((uint16_t)0x0360) /* In case interrupt is generated if the EIE bit is set */
#define USART_IT_NE ((uint16_t)0x0260)
#define USART_IT_FE ((uint16_t)0x0160)
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
{
uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
ITStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_GET_IT(USART_IT));
/* The CTS interrupt is not available for UART4 and UART5 */
if (USART_IT == USART_IT_CTS)
{
assert_param(IS_USART_1236_PERIPH(USARTx));
}
/* Get the USART register index */
usartreg = (((uint8_t)USART_IT) >> 0x05);
/* Get the interrupt position */
itmask = USART_IT & IT_MASK;
itmask = (uint32_t)0x01 << itmask;
if (usartreg == 0x01) /* The IT is in CR1 register */
{
itmask &= USARTx->CR1;
}
else if (usartreg == 0x02) /* The IT is in CR2 register */
{
itmask &= USARTx->CR2;
}
else /* The IT is in CR3 register */
{
itmask &= USARTx->CR3;
}
bitpos = USART_IT >> 0x08;
bitpos = (uint32_t)0x01 << bitpos;
bitpos &= USARTx->SR;
if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return bitstatus;
}
以RXNE为例:
一开始是把USART_IT_RXNE 0x0505写成2进制,然后只要它的低八位,然后低八位右移5位,然后得到usartreg的值为0b001,然后单击鼠标可以找到IT_MASK的值,它与我们的USART_IT相与得到它的值为5,接下来就是IT_MASK等于0x01左移5位,为什么是5位,因为要判定RXNEIE,他就是bit5。我们得到了IT_MASK的第5位为1,然后和CR1寄存器相与,进而判定RXNEIE是不是1itmask &= USARTx->CR1;也就是判断是否打开了中断。接下来就是通过bitpos &= USARTx->SR;判断下面寄存器SR的第五位。
综上所述
USART_GetITStatus判定的是事件标志和使能控制位,这两个位同时为1返回值才为1,
USART_GetFlagStatus函数
#define USART_FLAG_CTS ((uint16_t)0x0200)
#define USART_FLAG_LBD ((uint16_t)0x0100)
#define USART_FLAG_TXE ((uint16_t)0x0080)
#define USART_FLAG_TC ((uint16_t)0x0040)
#define USART_FLAG_RXNE ((uint16_t)0x0020)
#define USART_FLAG_IDLE ((uint16_t)0x0010)
#define USART_FLAG_ORE ((uint16_t)0x0008)
#define USART_FLAG_NE ((uint16_t)0x0004)
#define USART_FLAG_FE ((uint16_t)0x0002)
#define USART_FLAG_PE ((uint16_t)0x0001)
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_FLAG(USART_FLAG));
/* The CTS flag is not available for UART4 and UART5 */
if (USART_FLAG == USART_FLAG_CTS)
{
assert_param(IS_USART_1236_PERIPH(USARTx));
}
if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return bitstatus;
}
假设判定的是RXNE 标志位:
#define USART_FLAG_RXNE ((uint16_t)0x0020) if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET) {
bitstatus = SET; }
这不就是判定的SR寄存器的bit5-RXNE 标志位是不是1
转载不声明,谭泥小几及