STM32 串口通信使用奇偶校验

填坑:STM32串口通信如果使用奇偶校验,需要设置数据位长度为9bit

 

void MX_USART2_UART_Init(void){
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_9B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_EVEN;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)  {
    Error_Handler();
  }
}

 

posted @ 2020-08-12 11:34  vonly  阅读(1857)  评论(0编辑  收藏  举报