USART_GetITStatus()和USART_GetFlagStatus()的區別
都是訪問串口的SR狀態寄存器,唯一不同是,USART_GetITStatus()會判斷中斷是否開啟,如果沒開啟,也會返回false。
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT)
該函數不僅會判斷標志位是否置1,同時還會判斷是否使能了相應的中斷。所以在串口中斷函數中,如果要獲取中斷標志位,通常使用該函數。------串口中斷函數中使用。
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint32_t USART_FLAG)
該函數只判斷標志位。在沒有使能相應的中斷時,通常使用該函數來判斷標志位是否置1。------做串口輪詢時使用。
1 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG) 2 { 3 FlagStatus bitstatus = RESET; 4 /* Check the parameters */ 5 assert_param(IS_USART_ALL_PERIPH(USARTx)); 6 assert_param(IS_USART_FLAG(USART_FLAG)); 7 8 /* The CTS flag is not available for UART4 and UART5 */ 9 if (USART_FLAG == USART_FLAG_CTS) 10 { 11 assert_param(IS_USART_1236_PERIPH(USARTx)); 12 } 13 14 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET) 15 { 16 bitstatus = SET; 17 } 18 else 19 { 20 bitstatus = RESET; 21 } 22 return bitstatus; 23 }
1 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT) 2 { 3 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00; 4 ITStatus bitstatus = RESET; 5 /* Check the parameters */ 6 assert_param(IS_USART_ALL_PERIPH(USARTx)); 7 assert_param(IS_USART_GET_IT(USART_IT)); 8 9 /* The CTS interrupt is not available for UART4 and UART5 */ 10 if (USART_IT == USART_IT_CTS) 11 { 12 assert_param(IS_USART_1236_PERIPH(USARTx)); 13 } 14 15 /* Get the USART register index */ 16 usartreg = (((uint8_t)USART_IT) >> 0x05); 17 /* Get the interrupt position */ 18 itmask = USART_IT & IT_MASK; 19 itmask = (uint32_t)0x01 << itmask; 20 21 if (usartreg == 0x01) /* The IT is in CR1 register */ 22 { 23 itmask &= USARTx->CR1; 24 } 25 else if (usartreg == 0x02) /* The IT is in CR2 register */ 26 { 27 itmask &= USARTx->CR2; 28 } 29 else /* The IT is in CR3 register */ 30 { 31 itmask &= USARTx->CR3; 32 } 33 34 bitpos = USART_IT >> 0x08; 35 bitpos = (uint32_t)0x01 << bitpos; 36 bitpos &= USARTx->SR; 37 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET)) 38 { 39 bitstatus = SET; 40 } 41 else 42 { 43 bitstatus = RESET; 44 } 45 46 return bitstatus; 47 }