不同晶振配置STM32 波特率

来源:互联网 发布:linux golang 1.8 编辑:程序博客网 时间:2024/05/29 04:44

一般来说,stm32都是用标配的晶振,比如8MHz. 但是,如果用别的晶振,比如13.56M的晶振,那串口接收还正常吗?

根据试验结果,很可能会飞码。比如说用串口助手发送的是0x35,但是在串口接收中断里面就会得到别的值,即上位机发送的数据和板子接收的数据不一致。

通过查资料,解决办法如下:

修改文件stm32f10x.h,

#define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */

修改为:

#define HSE_VALUE    ((uint32_t)13560000) /*!< Value of the External oscillator in Hz */

这样,就正常了。


另外,有的网友说还需要修改一个文件:system_stm32f10x.c,修改PLL参数:


    /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
                                        RCC_CFGR_PLLMULL));
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
修改为:  
    /*  PLL configuration: PLLCLK = HSE * 4 = 54.24MHz */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
                                        RCC_CFGR_PLLMULL));
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL4);

   (因为我的SYSCLK是54.24M)


再摘抄某网友的一段话,帮助理解:

如果你仔细研究USART_Init()函数,你会发现,这个程序在配置<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%B3%A2%E7%89%B9%E7%8E%87&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">波特率</a>时会读取RCC有关的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%AF%84%E5%AD%98%E5%99%A8&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">寄存器</a>来计算当前CPU实际的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%B7%A5%E4%BD%9C%E9%A2%91%E7%8E%87&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">工作频率</a>是多少,然后计算参数来实现你需要的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%B3%A2%E7%89%B9%E7%8E%87&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">波特率</a>——就是说不管你把<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%8D%95%E7%89%87%E6%9C%BA&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">单片机</a>设置为什么样的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%B7%A5%E4%BD%9C%E9%A2%91%E7%8E%87&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">工作频率</a>,这个函数都能让串口以你要求的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%B3%A2%E7%89%B9%E7%8E%87&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">波特率</a>工作。当然如果你把原来8M的<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%99%B6%E6%8C%AF&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">晶振</a>换掉了,那你就需要修改系统中的一个定义:在文件<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=stm32&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">stm32</a>f10x.h中的定义HSE_Value,这参数定义了<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E5%8D%95%E7%89%87%E6%9C%BA&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">单片机</a>使用外部<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%99%B6%E6%8C%AF&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">晶振</a>或<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%97%B6%E9%92%9F%E6%BA%90&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">时钟源</a>的频率,一般就是8000000,即使用8MHz<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E6%99%B6%E6%8C%AF&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">晶振</a>。如果你的STM32用了什么7.3728、11.0592或者25MHz晶振时,你就要修改HSE_Value定义的值,这样保证<a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=%E4%B8%B2%E5%8F%A3%E9%80%9A%E4%BF%A1&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color: rgb(45, 100, 179); text-decoration: none;">串口通信</a>波特率正确。

0 0
原创粉丝点击