【 ST NUCLEO-H743ZI测评】+ 4、串口输出
<div class='showpostmsg'> 本帖最后由 TL-LED 于 2020-5-15 14:52 编辑<p><span style="font-size:16px;"> 这篇来学习下串口的使用,使用串口输出一串字符到PC机。HAL 库之前没怎么使用过,和之前的标准库还是有些区别。</span></p>
<p> </p>
<p><span style="font-size:16px;"> <strong>一、硬件电路</strong></span></p>
<p><span style="font-size:16px;"> 使用ST-LINK虚拟串口来与PC机通信</span></p>
<p><span style="font-size:16px;"> 1.1、仿真器接口</span></p>
<p> </p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;"> 1.2、MCU接口</span></p>
<p><span style="font-size:16px;"> MCU使用的端口是PD8和PD9端口。</span></p>
<p> </p>
<p><strong><span style="font-size:16px;"> 二、程序</span></strong></p>
<p> </p>
<p><span style="font-size:16px;"> 2.1、main.c</span></p>
<pre>
<code>int main(void)
{
uint8_t txdat="www.eeworld.com.cn-stm32h743 usart test\r\n";
CPU_CACHE_Enable();
HAL_Init();
SystemClock_Config();
LED_Init();
KEY_Init();
UART_Init();
while (1)
{
led1_tog();
HAL_Delay(100);
led2_tog();
HAL_Delay(100);
led3_tog();
HAL_Delay(100);
HAL_UART_Transmit(&UartHandle, txdat, sizeof(txdat), 100);
}
}</code></pre>
<p> </p>
<p><span style="font-size:16px;"> 2.2、usart.c</span></p>
<pre>
<code>#include "config.h"
UART_HandleTypeDef UartHandle;
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDefGPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
/* Enable USARTx clock */
USARTx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration*/
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration*/
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
}
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
/*##-1- Reset peripherals ##################################################*/
USARTx_FORCE_RESET();
USARTx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks #################################*/
/* Configure USART3 Tx as alternate function*/
HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
/* Configure USART3 Rx as alternate function*/
HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
}
void UART_Init(void)
{
UartHandle.Instance = USART3;
UartHandle.Init.BaudRate = 115200;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
{
//Error_Handler();
}
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
//Error_Handler();
}
}</code></pre>
<p><span style="font-size:16px;"> 2.3、usart.h</span></p>
<p><span style="font-size:16px;"> </span></p>
<pre>
<code>#ifndef __USART_H
#define __USART_H
#define USARTx USART3
#define USARTx_CLK_ENABLE() __HAL_RCC_USART3_CLK_ENABLE()
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
#define USARTx_FORCE_RESET() __HAL_RCC_USART3_FORCE_RESET()
#define USARTx_RELEASE_RESET() __HAL_RCC_USART3_RELEASE_RESET()
/* Definition for USARTx Pins */
#define USARTx_TX_PIN GPIO_PIN_8
#define USARTx_TX_GPIO_PORT GPIOD
#define USARTx_TX_AF GPIO_AF7_USART3
#define USARTx_RX_PIN GPIO_PIN_9
#define USARTx_RX_GPIO_PORT GPIOD
#define USARTx_RX_AF GPIO_AF7_USART3
extern UART_HandleTypeDef UartHandle;
void UART_Init(void);
#endif
</code></pre>
<p><strong><span style="font-size:16px;"> 三、运行结果</span></strong></p>
<p> </p>
<p><span style="font-size:16px;"> 串口间隔输出数据</span><br />
</p>
<p><span style="font-size:16px;"> </span></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>好资料,谢谢楼主。</p>
<p>这种软硬都有的资料,最有参考价值了</p>
再来点硬核的 <p>如果可以写的更加详细就更好了,再增加点扩展功能举一反三之类的</p>
littleshrimp 发表于 2020-5-15 20:53
再来点硬核的
<p>好的,<img height="28" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/smiling-face-with-open-mouth-and-smiling-eyes_1f604.png" width="28" /></p>
led2015 发表于 2020-5-27 23:16
如果可以写的更加详细就更好了,再增加点扩展功能举一反三之类的
<p>可以的, 还在学习!</p>
页:
[1]