本帖最后由 HuaWng 于 2024-10-11 22:11 编辑
在上一篇:【STM32H7S78-DK测评】-基础任务之开发环境与点灯 https://bbs.21ic.com/icview-3406752-1-1.html 的基础上,本次增加串口打印的功能。
目标: 实现板载调试串口通讯,实现字符串打印输出“Hello 21ic, Hello STM32H7S78-DK!"
STM32H7S78-DK的串口UART4使用的是PD0引脚和PD1引脚,这两个引脚与STLINK-V3EC相连,进而通过CN7连接到PC机。默认的配置参数为:
- 115200 bps
- 8 bit数据
- 无奇偶校验
- 一个bit停止位
- 没有流控
在STM32CubeIDE工程内打开项目的Ioc文件,进而图形化配置界面。在Connectivity里面找到UART4,接着修改使用的引脚。
点击Save,回到代码编辑界面。查看Uart4相关初始化代码 MX_UART4_Init。
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}
继续使用如下API进行串口打印输出:HAL_UART_Transmit(&huart4, "Hello 21ic, Hello STM32H7S78-DK!", sizeof("Hello 21ic, Hello STM32H7S78-DK!"), 0xFFFF);
打开串口监视器:
|