51uwb.cn

标题: 请问如何配置UART1的RX接收中断 [打印本页]

作者: john    时间: 2022-1-5 17:01
标题: 请问如何配置UART1的RX接收中断
修改如下的方法,通过串口终端发送的消息始终到不了接收中断,请问下是需要什么额外的配置,感谢!

#define REC_LENGTH  1
extern uint8_t rDataBuffer[REC_LENGTH];
static void MX_USART1_UART_Init(void)
{
    huart1.Instance = USART1;
    huart1.Init.BaudRate = 115200;
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
    huart1.Init.StopBits = UART_STOPBITS_1;
    huart1.Init.Parity = UART_PARITY_NONE;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart1) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
               
    /* enable usart1 irq interrupt init*/
    //HAL_NVIC_SetPriority(USART1_IRQn, 4, 0);
    //HAL_NVIC_EnableIRQ(USART1_IRQn);
                #if 1
                //enable uart interrupt
                if (HAL_OK ==  HAL_UART_Receive_IT(&huart1,(uint8_t *)rDataBuffer,REC_LENGTH)){
               
                        printf("uart interrupt receive success.\n");
                }
                #endif//
                printf("/******************************************************************/\n");
                printf("/********More Information Please Visit Our Website*************/\n");
                printf("/************************www.51uwb.cn**************************/\n");
                printf("/*********************FM Version F4-V1.0************************/\n");
                printf("/******************************************************************/\n");

}

然后在HAL_UART_RxCpltCallback的中断方法里面使用rDataBuffer ,但是这个中断就没有进来,很奇怪,请教下是什么原因,非常感谢!

uint8_t rDataBuffer[1];  //  RX Data buffer
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
        uint8_t rDataCount = 0;  //  count Data bytes
        uint8_t rDataFlag = 0;  //  waitting complete RX date having been send
  /* Prevent unused argument(s) compilation warning */
  UNUSED(huart);
  /* NOTE: This function should not be modified, when the callback is needed,
           the HAL_UART_RxCpltCallback could be implemented in the user file
   */
  //while(HAL_UART_Receive_IT(huart, rDataBuffer, 1) != HAL_OK);         // Wait completly receive 1 byte data, and put data in rDataBuffer
       
        printf("%c", rDataBuffer[0]);
       
        HAL_UART_Receive_IT(huart, rDataBuffer, 1);
}


作者: john    时间: 2022-1-5 17:10
补充下,使用的是TWR-400的模块
作者: john    时间: 2022-1-11 15:54
初始化:
                //enable uart interrupt
                if (HAL_OK ==  HAL_UART_Receive_IT(&huart1,(uint8_t *)rDataBuffer,REC_LENGTH)){
               
                        printf("uart interrupt receive success.\n");
                }
    /* enable usart1 irq interrupt init*/
    HAL_NVIC_EnableIRQ(USART1_IRQn);
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);


中断处理函数
uint8_t rDataBuffer[1];  //  RX Data buffer  
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
        if(huart->Instance==USART1)//如果是串口1
        {  
                printf("%c", rDataBuffer[0]);
        }
}
#if 1
void USART1_IRQHandler(void)                       
{
        HAL_UART_IRQHandler(&huart1);        //调用HAL库中断处理公用函数
       
  while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY);//等待就绪

    //一次处理完成之后,重新开启中断并设置RxXferCount为1
        while(HAL_UART_Receive_IT(&huart1, (uint8 *)rDataBuffer, 1) != HAL_OK);
}

作者: 赵汝杰    时间: 2022-3-14 00:09
john 发表于 2022-1-11 15:54
初始化:
                //enable uart interrupt
                if (HAL_OK ==  HAL_UART_Receive_IT(&huart1,(uint8_t *)rDataBu ...

解决了吗哥
作者: 兔子    时间: 2022-4-18 00:18
同问啊同问啊




欢迎光临 51uwb.cn (http://51uwb.cn/) Powered by Discuz! X3.3