STM32串列埠中 USART_GetITStatus 與 USART_GetFlagStatus的區別
阿新 • • 發佈:2018-12-27
一、ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)在判斷相應位是否置1(讀SR暫存器)前會先判斷相應位的中斷是否使能(讀CR暫存器)。
所以若使用 while(USART_GetITStatus(USART1,USART_IT_TC) != SET); 來進行判斷資料是否傳送完成,
注意需使能TC中斷 USART_ITConfig(USART1, USART_IT_TC, ENABLE); 否則會一直返回0導致死在while裡。
/** * @brief Checks whether the specified USART interrupt has occurred or not. * @param USARTx: Select the USART or the UART peripheral. * This parameter can be one of the following values: * USART1, USART2, USART3, UART4 or UART5. * @param USART_IT: specifies the USART interrupt source to check. * This parameter can be one of the following values: * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5) * @arg USART_IT_LBD: LIN Break detection interrupt * @arg USART_IT_TXE: Tansmit Data Register empty interrupt * @arg USART_IT_TC: Transmission complete interrupt * @arg USART_IT_RXNE: Receive Data register not empty interrupt * @arg USART_IT_IDLE: Idle line detection interrupt * @arg USART_IT_ORE: OverRun Error interrupt * @arg USART_IT_NE: Noise Error interrupt * @arg USART_IT_FE: Framing Error interrupt * @arg USART_IT_PE: Parity Error interrupt * @retval The new state of USART_IT (SET or RESET). */ 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_123_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; }
二、FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)直接判斷相應位是否置1(讀SR暫存器),而不會判斷相應中斷是否開啟,通常可用於沒開啟相應中斷時進行判斷,比如只使用了串列埠接收到資料進入中斷時,只需使能 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 無需再使能多餘的中斷。
void USART1_IRQHandler(void) { u8 res; if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)//Receive Data register not empty interrupt { res = USART_ReceiveData(USART1); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) != SET);//判斷髮送緩衝區是否空 USART_SendData(USART1,res); while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET); //while(USART_GetITStatus(USART1,USART_IT_TC) != SET);//需使能TC中斷 } USART_ClearFlag(USART1,USART_FLAG_TC); //USART_ClearITPendingBit(USART1,USART_IT_TC); //與USART_ClearFlag同種效果 }
/** * @brief Checks whether the specified USART flag is set or not. * @param USARTx: Select the USART or the UART peripheral. * This parameter can be one of the following values: * USART1, USART2, USART3, UART4 or UART5. * @param USART_FLAG: specifies the flag to check. * This parameter can be one of the following values: * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5) * @arg USART_FLAG_LBD: LIN Break detection flag * @arg USART_FLAG_TXE: Transmit data register empty flag * @arg USART_FLAG_TC: Transmission Complete flag * @arg USART_FLAG_RXNE: Receive data register not empty flag * @arg USART_FLAG_IDLE: Idle Line detection flag * @arg USART_FLAG_ORE: OverRun Error flag * @arg USART_FLAG_NE: Noise Error flag * @arg USART_FLAG_FE: Framing Error flag * @arg USART_FLAG_PE: Parity Error flag * @retval The new state of USART_FLAG (SET or RESET). */ 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_123_PERIPH(USARTx)); } if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET) { bitstatus = SET; } else { bitstatus = RESET; } return bitstatus; }
總結:我認為在GetITStatus和GetFlagStatus不僅在串列埠中是這種區別,在其他地方比如外部中斷中使用也是類似道理。
以上為本人現階段學習中所認為,若有錯誤希望指出相互學習謝謝!