cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F411] I want UART waiting for RXNE interrupt,but it can't stop to keep entering UART_IRQ_Handler and alway readed the last data even I'm not trans any data to MCU, Where am I doing wrong?

WYang.5
Associate

UART_HandleTypeDef huart2;

void MX_USART2_UART_Init(void){

 huart2.Instance = USART2;

 huart2.Init.BaudRate = 9600;

 huart2.Init.WordLength = UART_WORDLENGTH_8B;

 huart2.Init.StopBits = UART_STOPBITS_1;

 huart2.Init.Parity = UART_PARITY_NONE;

 huart2.Init.Mode = UART_MODE_TX_RX;

 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart2.Init.OverSampling = UART_OVERSAMPLING_16;

 if (HAL_UART_Init(&huart2) != HAL_OK)

 {

  Error_Handler();

 }

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

}

int main(void){

 HAL_Init();

 MX_USART2_UART_Init();

 while (1)

 { }

}

void USART2_IRQHandler(void){

static uint8_t temp;

HAL_UART_IRQHandler(&huart2);

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

}

1 REPLY 1
TDK
Guru

If you want to use HAL, you'll have to use its way of doing things. In this case, that means using the HAL_UART_Receive_IT function to start the transfer.

See example here:

https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Projects/STM32F411E-Discovery/Examples/UART/UART_TwoBoards_ComIT/Src/main.c

If you feel a post has answered your question, please click "Accept as Solution".