
问题解决最近学习了一下32的串口通信,串口配置还是很简单的,不过后面串口调试助手上面出现的乱码。很让人头疼。
当然第一时间去百度寻求解决办法。
百度搜索总结:
外部晶振设置的问题。但是也要排查下面 ? 两个问题串口助手的波特率是不是和程序设置的是一样的,如果一样的话出现乱码,参考下面的解决方法。插播一条反爬虫信息,读者可以忽略:

stm32的串口使用的是外部高速时钟的时钟源
串口外设是挂在APB总线上的,一般用于低速外设,它是AHB总线时钟分频所得。 这样设计分频是为了降低低速外设的开关损耗。
然而AHB总线的时钟又源自于高速时钟源。高速时钟源可以是高速内部时钟、高速外部时钟、高速内部或外部时钟PLL倍频所得。
所以最终串口的波特率受高速时钟的影响。

首先找到自己板子上外部晶振,并确定它的大小。

▲ 博主板子上晶振是8Mhz
在官方标准库中,外部时钟频率配置默认 25MHz 的晶振。
而我们需要将外部时钟频率配置成适合自己板子的。
找到 stm32f4xx.h ,在 .h 文件120行左右的地方,默认是这样的:
/**
* @brief In the following line adjust the value of External High Speed oscillator (HSE)
used in your application
Tip: To avoid modifying this file each time you need to use different HSE, you
can define the HSE value in your toolchain compiler preprocessor.
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
▲ 默认25Mhz
下面只需定义一下自己开发板的外部时钟源频率即可:(加个宏定义)
/**
* @brief In the following line adjust the value of External High Speed oscillator (HSE)
used in your application
Tip: To avoid modifying this file each time you need to use different HSE, you
can define the HSE value in your toolchain compiler preprocessor.
*/
#define HSE_VALUE ((uint32_t)8000000) // 定义自己开发版上的外部时钟源频率
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */附:如果串口助手没有收到任何东西,请检查连接 TX 、RX 端子的杜邦线是否松动。
stm32f4xx.h 文件中相关配置加以修改。
【参考文献】 [1]:https://blog.csdn.net/qq_34179721/article/details/97427395?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-2