新手入门
- 积分
- 15
- 金钱
- 15
- 注册时间
- 2016-6-13
- 在线时间
- 2 小时
|
1金钱
本帖最后由 惨绿青年 于 2016-6-13 17:37 编辑
小弟这几天开始学习stm32,在阅读 USART 清除标志位的代码的时候无法理解为什么清除标志位的代码是
USARTx->SR = (uint16_t)~USART_FLAG;
比如我想要清除TXE标志位,那代码不就变成了
#define USART_FLAG_TXE ((uint16_t)0x0080)
USARTx->SR = (uint16_t)~USART_FLAG_TXE;
这样TXE位确实是清零了,可是其它位的状态不就都被置1了吗?
我觉得增加一个按位与运算符比较合适:
USARTx->SR &= (uint16_t)~USART_FLAG_TXE;
请问是我哪里理解错了吗?
在此先谢谢各位。我尝试过搜索但并没有搜到相同的问题,所以只有来向各位请教了!
*************************************************************************************
此处附上 stm32f10x_usart.c 中 USART_ClearFlag 的代码及注释
void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
/* The CTS flag is not available for UART4 and UART5 */
if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
{
assert_param(IS_USART_123_PERIPH(USARTx));
}
USARTx->SR = (uint16_t)~USART_FLAG;
}
/**
* @brief Clears the USARTx's pending flags.
* @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 clear.
* This parameter can be any combination 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_TC: Transmission Complete flag.
* @arg USART_FLAG_RXNE: Receive data register not empty flag.
*
* @note
* - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
* error) and IDLE (Idle line detected) flags are cleared by software
* sequence: a read operation to USART_SR register (USART_GetFlagStatus())
* followed by a read operation to USART_DR register (USART_ReceiveData()).
* - RXNE flag can be also cleared by a read to the USART_DR register
* (USART_ReceiveData()).
* - TC flag can be also cleared by software sequence: a read operation to
* USART_SR register (USART_GetFlagStatus()) followed by a write operation
* to USART_DR register (USART_SendData()).
* - TXE flag is cleared only by a write to the USART_DR register
* (USART_SendData()).
* @retval None
*/
|
|