STM32 library systemInit crystal oscillator to change the default method 8M to 12M

This article is reproduced from kN Wei generals "How to configure an external crystal clock stm32 change"
Because stm32 default library is implemented in the case 8M external crystal, it is time to configure the serial port baud rate by 8M, including the frequency.
If external crystal 12M, arranged to clock 72MHZ  .

1) PLL frequency of such a change:
8n:
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);//8*9=72
12M:
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);//12*6=72
Library function: void RCC_PLLConfig (uint32_t RCC_PLLSource, uint32_t RCC_PLLMul)
例:RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);

2) stm32f10x.h modified:
8n:
#define HSE_Value ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ 
12M:
#define HSE_Value ((uint32_t)12000000) /*!< Value of the External oscillator in Hz */


Then talk about the internal clock, the internal clock is generated by the RC oscillator accuracy is not high. Power produced by the internal clock smaller, also saves PCB space, the program is configured as follows:
void RCC_Configuration(void)
{
        /* RCC system reset(for debug purpose) */
        RCC_DeInit ();

        RCC_HSICmd (ENABLE); // open the internal high-speed clock
        // Wait HSI ready
        while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

        FLASH_PrefetchBufferCmd (FLASH_PrefetchBuffer_Enable); // open FLASH prefetch function
        // FLASH timing control
        // recommended: SYSCLK = 0 ~ 24MHz Latency = 0
        //       SYSCLK = 24~48MHz  Latency=1
        //       SYSCLK = 48~72MHz  Latency=2
        FLASH_SetLatency(FLASH_Latency_2);
        RCC_HCLKConfig (RCC_SYSCLK_Div1); // set HCLK (AHB clock) = SYSCLK
        RCC_PCLK2Config(RCC_HCLK_Div1);                //PCLK2(APB2) = HCLK
        RCC_PCLK1Config(RCC_HCLK_Div1);                //PCLK1(APB1) = HCLK

        //PLL设置 SYSCLK/2 * 12 = 4*12 = 48MHz
        RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);
        //启动PLL
        RCC_PLLCmd(ENABLE);//如果PLL被用于系统时钟,不能被DISABLE
        //等待PLL稳定
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){;}

        //设置系统时钟SYSCLK = PLL输出
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        //等待PLL成功用作于系统时钟的时钟源,并等待稳定
        // 0x00:HSI作为系统时钟
        // 0x04:HSE作为系统时钟
        // 0x08:PLL作为系统时钟
        while(RCC_GetSYSCLKSource() != 0x08);
}
还有如果修给了 HSE_Value ,但是还有问题,则库文件使用的是LIB文件,而不是C文件。现在看来是stm32f10x_rcc.c转LIB时的问题。它里面把HSE_VALUE编译成死的了,不跟头文件走。
所以,如果库文件使用的是LIB文件,那么改晶振频率后就需要把stm32f10x_rcc.c加到工程里一起编译。
发布了18 篇原创文章 · 获赞 86 · 访问量 16万+

Guess you like

Origin blog.csdn.net/u013178472/article/details/76307107