HAL_UART_Receive_IT()

文章详细解释了如何使用HAL_UART_Receive_IT函数以中断模式接收数据,特别关注了在接收字符串时size参数的作用,并提供了中断处理函数中的示例代码,包括如何判断接收是否完成以便进行后续操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
  * @brief  Receives an amount of data in non blocking mode.
  * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  *         the received data is handled as a set of u16. In this case, Size must indicate the number
  *         of u16 available through pData.
  * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  *               the configuration information for the specified UART module.
  * @param  pData Pointer to data buffer (u8 or u16 data elements).
  * @param  Size  Amount of data elements (u8 or u16) to be received.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)

首先思考一个问题:这个函数是会以中断方式来接受字符串的,那么每次接受一个字符就一定会发生一次中断,那么这个函数里面的size是给来干什么的?用来判断是否接受完成的。但是这个逻辑需要我们自己去设计例如:

在中断处理函数里面:

void USART1_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart1);
  HAL_UART_Receive_IT(&huart1,(uint8_t*)&goodBuffer[order],1);
}

可以加入一下判断是否接受完成的代码:

int order=0;

int size=5;

void USART1_IRQHandler(void)

{

        HAL_UART_IRQHandler( &huart1);

        order++;

        if(order==size-1)//这个时候就接受完成了

        {

                //其他代码

        }

        HAL_UART_Transmit(&huart1,(uint8_t*)goodBuffer,sizeof(goodBuffer),0xffff);

        HAL_UART_Receive_IT(&huart1,(uint8_t*)&goodBuffer[order],1);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geniusNMRobot引流专家

你最少得给我一个亿

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值