51uwb.cn

 找回密码
 立即注册
查看: 3704|回复: 4
打印 上一主题 下一主题

请问如何配置UART1的RX接收中断

[复制链接]

1

主题

4

帖子

13

积分

新手上路

Rank: 1

积分
13
跳转到指定楼层
楼主
发表于 2022-1-5 17:01:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
修改如下的方法,通过串口终端发送的消息始终到不了接收中断,请问下是需要什么额外的配置,感谢!

#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);
}

回复

使用道具 举报

1

主题

4

帖子

13

积分

新手上路

Rank: 1

积分
13
沙发
 楼主| 发表于 2022-1-5 17:10:20 | 只看该作者
补充下,使用的是TWR-400的模块
回复

使用道具 举报

1

主题

4

帖子

13

积分

新手上路

Rank: 1

积分
13
板凳
 楼主| 发表于 2022-1-11 15:54:26 | 只看该作者
初始化:
                //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);
}
回复

使用道具 举报

1

主题

3

帖子

13

积分

新手上路

Rank: 1

积分
13
地板
发表于 2022-3-14 00:09:04 | 只看该作者
john 发表于 2022-1-11 15:54
初始化:
                //enable uart interrupt
                if (HAL_OK ==  HAL_UART_Receive_IT(&huart1,(uint8_t *)rDataBu ...

解决了吗哥
回复

使用道具 举报

35

主题

76

帖子

197

积分

官方会员

Rank: 8Rank: 8

积分
197
5#
发表于 2022-4-18 00:18:21 | 只看该作者
同问啊同问啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

bphero Inc.  

GMT+8, 2024-4-24 16:13 , Processed in 0.013311 second(s), 4 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表