cancel
Showing results for 
Search instead for 
Did you mean: 

Change STM32F103 Frequency at run time

pkumar1883
Associate II
Posted on February 17, 2017 at 06:28

Hi,

I am using STM32F103. I have configured my clock at 72 MHz as shown in the below code--

RCC_DeInit(); msDelay(10); RCC_HSEConfig(RCC_HSE_OFF); msDelay(10); RCC_HSEConfig(RCC_HSE_ON); while(1){ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) break; } if(HSEStartUpStatus == SUCCESS) { RCC_PLLCmd(DISABLE); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); FLASH_SetLatency(FLASH_Latency_2); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); if(HSE_VALUE==16000000){ RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9); } else{ RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); } RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){} RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08){} Flag_SYS_SPEED_72=SET; Flag_HSE_Active=SET; } else { RCC_HSIConfiguration(); Flag_HSI_Active=SET; }

Now I want to reconfigure my system clock at run time. I want to run my system at 8MHz. Please suggest how to do this.

1 REPLY 1
pkumar1883
Associate II
Posted on February 17, 2017 at 06:34

Please see my code is as below-

     RCC_DeInit();

        msDelay(10);

        RCC_HSEConfig(RCC_HSE_OFF);

        msDelay(10);

        RCC_HSEConfig(RCC_HSE_ON);

              while(1){

                  HSEStartUpStatus = RCC_WaitForHSEStartUp();

                  if(HSEStartUpStatus == SUCCESS)

                      break;

              }

            if(HSEStartUpStatus == SUCCESS)

            {

                RCC_PLLCmd(DISABLE);

                RCC_HCLKConfig(RCC_SYSCLK_Div1);

                RCC_PCLK2Config(RCC_HCLK_Div1);

                RCC_PCLK1Config(RCC_HCLK_Div2);

                FLASH_SetLatency(FLASH_Latency_2);

                FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

                if(HSE_VALUE==16000000){

                    RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9);

                }

                else{

                    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

                }

                RCC_PLLCmd(ENABLE);

                while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}

                RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

                while(RCC_GetSYSCLKSource() != 0x08){}

                Flag_SYS_SPEED_72=SET;

                Flag_HSE_Active=SET;

            }

            else {

                RCC_HSIConfiguration();

                Flag_HSI_Active=SET;

            }