[STM32N6] 4.运行STM32N6 的UART并测试

[复制链接]
722|1
 楼主| 一路向北lm 发表于 2025-3-12 13:43 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2025-3-12 13:45 编辑

串口1初始化,波特率115200,7位数据位,1停止位 偶校验
  1. static void MX_USART1_UART_Init(void)
  2. {

  3.   /* USER CODE BEGIN USART1_Init 0 */

  4.   /* USER CODE END USART1_Init 0 */

  5.   /* USER CODE BEGIN USART1_Init 1 */

  6.   /* USER CODE END USART1_Init 1 */
  7.   huart1.Instance = USART1;
  8.   huart1.Init.BaudRate = 115200;
  9.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  10.   huart1.Init.StopBits = UART_STOPBITS_1;
  11.   huart1.Init.Parity = UART_PARITY_ODD;
  12.   huart1.Init.Mode = UART_MODE_TX_RX;
  13.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  14.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  15.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  16.   huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  17.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  18.   if (HAL_UART_Init(&huart1) != HAL_OK)
  19.   {
  20.     Error_Handler();
  21.   }
  22.   if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  23.   {
  24.     Error_Handler();
  25.   }
  26.   if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  27.   {
  28.     Error_Handler();
  29.   }
  30.   if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  31.   {
  32.     Error_Handler();
  33.   }
  34.   /* USER CODE BEGIN USART1_Init 2 */

  35.   /* USER CODE END USART1_Init 2 */

  36. }
串口1传输测试,发送aTxStartMessage数组,接受并使用led指示
  1. if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxStartMessage, TXSTARTMESSAGESIZE)!= HAL_OK)
  2.   {
  3.     /* Transfer error in transmission process */
  4.     Error_Handler();
  5.   }

  6.   /*##-2- Put UART peripheral in reception process ###########################*/
  7.   /* Any data received will be stored in "aRxBuffer" buffer : the number max of
  8.      data received is 10 */
  9.   if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  10.   {
  11.     /* Transfer error in reception process */
  12.     Error_Handler();
  13.   }

  14.   /*##-3- Wait for the end of the transfer ###################################*/
  15.   /*  Before starting a new communication transfer, you need to check the current
  16.       state of the peripheral; if it's busy you need to wait for the end of current
  17.       transfer before starting a new one.
  18.       For simplicity reasons, this example is just waiting till the end of the
  19.       transfer, but application may perform other tasks while transfer operation
  20.       is ongoing. */
  21.   while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  22.   {
  23.     /* While waiting for character from PC side, LED1 is toggling every 100ms */
  24.     BSP_LED_Toggle(LED1);
  25.     HAL_Delay(100);
  26.   }

  27.   /*##-4- Send the received Buffer ###########################################*/
  28.   if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK)
  29.   {
  30.     /* Transfer error in transmission process */
  31.     Error_Handler();
  32.   }

  33.   /*##-5- Wait for the end of the transfer ###################################*/
  34.   /*  Before starting a new communication transfer, you need to check the current
  35.       state of the peripheral; if it's busy you need to wait for the end of current
  36.       transfer before starting a new one.
  37.       For simplicity reasons, this example is just waiting till the end of the
  38.       transfer, but application may perform other tasks while transfer operation
  39.       is ongoing. */
  40.   while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  41.   {
  42.   }

  43.   /*##-6- Send the End Message ###############################################*/
  44.   if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxEndMessage, TXENDMESSAGESIZE)!= HAL_OK)
  45.   {
  46.     /* Transfer error in transmission process */
  47.     Error_Handler();
  48.   }

  49.   /*##-7- Wait for the end of the transfer ###################################*/
  50.   while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  51.   {
  52.   }

  53.   /* Turn on LED1 if test passes then enter infinite loop */
  54.   BSP_LED_On(LED1);
串口助手打印测试信息如下:
7420767d11f8672e07.png

yangjiaxu 发表于 2025-4-9 14:53 | 显示全部楼层
串口直接用cubemx搭建一下就行了,非常简单
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部