小弟初学STM32,在学到串口章节的时候,发现一个问题。例程里将uint16_t格式的USART_ReceiveData函数返回值赋给了u8格式的res变量。又将res作为入口参数给了 USART_SendData(USART_TypeDef* USARTx, uint16_t Data)函数,可是此函数的入口参数也是uint16_t格式的,这样不会出现错误吗?
代码已贴在底部,往各位大佬指点。
void USART2_IRQHandler()
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Receive Data */
return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
}
/**
* @brief Transmits break characters.
* @param USARTx: Select the USART or the UART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @retval None
*/
void USART_SendBreak(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Send break characters */
USARTx->CR1 |= CR1_SBK_Set;
}
void USART_SendData(USART_TypeDef* USARTx,
uint16_t Data)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->DR = (Data & (uint16_t)0x01FF);
}
/**
* @brief Returns the most recent received data by the USARTx peripheral.
* @param USARTx: Select the USART or the UART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @retval The received data.
*/
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Receive Data */
return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
}
DR寄存器实际上只有低9位有效,位8是奇偶校验不是数据,所以用u8来接收没问题,当然你改成u16也行
万分感谢!
一周热门 更多>