常见泽1 发表于 2023-7-27 19:20

【ST NUCLEO-WBA52CG】第二篇 串口printf

<p align="center"><strong><span style="font-family:宋体;"><span style="font-size:16px;"><a href="https://bbs.eeworld.com.cn/thread-1250591-1-1.html" target="_blank">【ST NUCLEO-WBA52CG】</a>第二篇 串口printf</span></span></strong></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">一 串口介绍</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">USART是一种通用的串行、异步通信总线,该总线有两条数据线,可以实现全双工的发送和接收。串口的通信速度用波特率来表示,其单位为bps(bit per second)即每秒钟传送bit的数量。</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">二 工程创建</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">(1)工程底板还是使用上一个LED的版本打开</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">(2)选择串口 USART1</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">和其他的官方开发板一样,使用的是VCP</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">通过USB线将STM32开发板与计算机相连,并确认连接方式设置为&ldquo;虚拟串口模式&rdquo;。</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">可以在设备管理器中看到一个新的串口,即&ldquo;STM32 Virtual ComPort&rdquo;</span></span></p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">开发板串口使用的是USART1</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">这个猛一看容易看错,CN14是预留的串口的接口,在板子的中间地带</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p align="left">&nbsp;</p>

<p align="left"><span style="font-family:宋体;"><span style="font-size:16px;">串口的配置如下</span></span></p>

<p align="left"><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">(3)生成工程</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">三 修改代码</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">代码修改主要有三处</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">第一处:加入头文件</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">#include &ldquo;stdio.h&rdquo;</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">第二处:</span></span></p>

<pre>
<code class="language-cpp">int fputc(int ch, FILE *f)

{

HAL_UART_Transmit(&amp;huart1, (uint8_t *)&amp;ch, 1, 2);//huart根据你的配置修改

return ch;

}</code></pre>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">第三处:加入printf</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">printf(&quot;welcom stm32wba52&quot;);</span></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">工程里需要修改个小东西</span></span></p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">这里勾选上,不然程序无法实现正常</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">配置完成点,点击编译下载</span></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;">四 测试</span></span></p>

<p>&nbsp;</p>

<p><span style="font-family:宋体;"><span style="font-size:16px;"> &nbsp;</span></span></p>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【ST NUCLEO-WBA52CG】第二篇 串口printf