中断 – 无法通过UART传输每个字符

我正在使用stm32f0 MCU.

我想传输从uart收到的每个字节.我在从uart收到的每个字节上启用一个中断.

我的代码非常简单.

uint8_t Rx_data[5]; 

//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)  //current UART
    {
        HAL_UART_Transmit(&huart1, &Rx_data[0], 1, 100);        
        HAL_UART_Receive_IT(&huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}

我的电脑将ASCII 12345678发送到stm32.如果事情按预期工作,PC应该收到12345678.但是,PC收到了1357.代码有什么问题?

重新启用中断可能效率低下.通过几次修改,可以使中断保持活动状态,而无需再次编写处理程序.请参阅从stm32cubemx生成器更改的以下示例.

/**
* @brief This function handles USART3 to USART6 global interrupts.
*/
void USART3_6_IRQHandler(void)
{
  InterruptGPS(&huart5);
}

void InterruptGPS(UART_HandleTypeDef *huart) {
    uint8_t rbyte;
    if (huart->Instance != USART5) {
        return;
    }
    /* UART in mode Receiver ---------------------------------------------------*/
    if((__HAL_UART_GET_IT(huart, UART_IT_RXNE) == RESET) || (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) == RESET)) {
        return;
    }
    rbyte = (uint8_t)(huart->Instance->RDR & (uint8_t)0xff);
    __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);

    // do your stuff

}

static void init_gps() {
    __HAL_UART_ENABLE_IT(&huart5, UART_IT_RXNE);
}
https://stackoverflow.com/questions/37297318/cannot-transmit-every-characters-through-uart

转载注明原文:中断 – 无法通过UART传输每个字符