野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16236|回复: 3

(stm32f4 HAL)各位大神啊,请告诉我 串口接收中这段代码的意思

[复制链接]
发表于 2017-8-21 16:28:44 | 显示全部楼层 |阅读模式
10火花
本帖最后由 胡图图 于 2017-8-21 16:54 编辑

/**
  * @brief  Receives an amount of data in non blocking mode
  * @param  huart: pointer to a UART_HandleTypeDef structure that contains
  *                the configuration information for the specified UART module.
  * @retval HAL status
  */
static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
{
  uint16_t* tmp;

  /* Check that a Rx process is ongoing */
  if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  {
    if(huart->Init.WordLength == UART_WORDLENGTH_9B)
    {
      tmp = (uint16_t*) huart->pRxBuffPtr;
      if(huart->Init.Parity == UART_PARITY_NONE)
      {
        *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FFU);
        huart->pRxBuffPtr += 2U;
      }
      else
      {
        *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FFU);
        huart->pRxBuffPtr += 1U;
      }
    }
    else
    {
      if(huart->Init.Parity == UART_PARITY_NONE)
      {
        *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FFU);
      }
      else
      {
        *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007FU);
      }
    }

    if(--huart->RxXferCount == 0U)
    {
      /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
      CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));

      /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
      CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);

      /* Rx process is completed, restore huart->RxState to Ready */
      huart->RxState = HAL_UART_STATE_READY;

      HAL_UART_RxCpltCallback(huart);

      return HAL_OK;
    }
    return HAL_OK;
  }
  else
  {
    return HAL_BUSY;
  }
}


那个解释下呗。。

最佳答案

查看完整内容

U表示无符号整形,+2的意思是如果串口字长为9bit则占两个字节,所以指针加2,对应取值是0x01FF;同理如果是8bit以下则占一个字节,指针加1,对应取值是0x00FF
回复

使用道具 举报

发表于 2017-8-21 16:28:45 | 显示全部楼层
U表示无符号整形,+2的意思是如果串口字长为9bit则占两个字节,所以指针加2,对应取值是0x01FF;同理如果是8bit以下则占一个字节,指针加1,对应取值是0x00FF
回复

使用道具 举报

 楼主| 发表于 2017-8-21 17:17:15 | 显示全部楼层
cancore 发表于 2017-8-21 16:28
U表示无符号整形,+2的意思是如果串口字长为9bit则占两个字节,所以指针加2,对应取值是0x01FF;同理如果是8 ...

谢谢大佬,大概明白了
回复

使用道具 举报

发表于 2017-11-1 16:04:28 | 显示全部楼层
我司是专业生产HMI串口液晶触摸屏的,想了解的的朋友可以打我电话18123741645微信同号,或加QQ397050970张S,谢谢
回复

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-4 13:12 , Processed in 0.081179 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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