Skip to main content
JJohn.3
Associate III
December 6, 2020
Question

STM32f103 Clock configuration problem

  • December 6, 2020
  • 4 replies
  • 1134 views

Hello,

Here is my code:

#include <stdio.h>
#include <string.h>
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
 
void GPIOSYS(void){
		GPIO_InitTypeDef GPIOStructure;
	
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
		//GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
	
	
		GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	
	
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	
			RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		/* Output clock on MCO pin ---------------------------------------------*/
		GPIOStructure.GPIO_Pin = GPIO_Pin_8;
		GPIOStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIOStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIOStructure);
 
		GPIOStructure.GPIO_Mode=GPIO_Mode_Out_PP;
		GPIOStructure.GPIO_Pin=GPIO_Pin_7 | GPIO_Pin_0 | GPIO_Pin_1 ;
		GPIOStructure.GPIO_Speed=GPIO_Speed_50MHz;
		GPIO_Init(GPIOB,&GPIOStructure);		
		
		GPIOStructure.GPIO_Mode=GPIO_Mode_Out_PP;
		GPIOStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_10;
		GPIOStructure.GPIO_Speed=GPIO_Speed_50MHz;
		GPIO_Init(GPIOA,&GPIOStructure);
 
	
}
 
void CLOCK(void){
	
	//RCC_DeInit(); 
	
	RCC_HSEConfig(RCC_HSE_ON);
	
	if(RCC_WaitForHSEStartUp()!=ERROR){
	
	RCC_HCLKConfig(RCC_SYSCLK_Div2);
		
	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);	
	FLASH_SetLatency(FLASH_Latency_2);
		
		
	RCC_PLLCmd(DISABLE);	
	RCC_PCLK1Config(RCC_HCLK_Div1); 						// APB1 Phrepheral 36Mhz, 
	RCC_PCLK2Config(RCC_HCLK_Div1); 						//PCLK is 36Mhz		
	RCC_PLLConfig(RCC_PLLSource_HSE_Div1,9); //36Mhz
	RCC_PLLCmd(ENABLE);
	
	while(RCC_GetFlagStatus( RCC_FLAG_PLLRDY)!=SET);
	
	FLASH_SetLatency(FLASH_Latency_2);
	
	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
	
//	RCC_ClockSecuritySystemCmd(ENABLE);
	
	while (RCC_GetSYSCLKSource() != 0x08);
	SystemCoreClockUpdate();
	
}
	//else
//	{
//		GPIO_ResetBits(GPIOB,GPIO_Pin_0);
//	}
 
}
 
void Delay(){
	int i;
	for(i=0;i<5;i++);
}
 
int main(void){
	int i;
		GPIOSYS();
	CLOCK();
	
	GPIO_SetBits(GPIOA,GPIO_Pin_10); //PC
	GPIO_SetBits(GPIOB,GPIO_Pin_0); //UDP
	GPIO_SetBits(GPIOB,GPIO_Pin_1); //UDP
//	SysTick_Config(SystemCoreClock / (14000));
 
	RCC_MCOConfig(RCC_MCO_SYSCLK);
	
	while(1){
		//delay();
			GPIO_SetBits(GPIOA,GPIO_Pin_10); //PC
			GPIO_SetBits(GPIOB,GPIO_Pin_0); //UDP
			GPIO_SetBits(GPIOB,GPIO_Pin_1); //UDP
		//delay();
			GPIO_ResetBits(GPIOA,GPIO_Pin_10); //PC
			GPIO_ResetBits(GPIOB,GPIO_Pin_0); //PC
			GPIO_ResetBits(GPIOB,GPIO_Pin_1); //PC
		
 	}
}
 
 
 
 
 
 

I have tried to set my MCU frequency to 36Mhz. However, on PA8 I see 72Mhz which is more than I wanted. I need to know what is my problem that the MCU frequency is not 36Mhz. In addition,when I use "RCC_DeInit(); " at the beginning of my clock configuration, I don't see anything at PA8.

it is necessary to change clock and make it safe (connected to HSI )when the external clock fails to 36Mhz (both mode should have the same frequency)

This topic has been closed for replies.

4 replies

Vangelis Fortounas
Associate II
December 6, 2020

Hello

check the definition of "HSE_VALUE" if is different than your crystal (system_stm32f1xx.c)

JJohn.3
JJohn.3Author
Associate III
December 7, 2020

Thank you for your post, I have checked the system_stm32f10x.c file and HSE_VALUE variable definition was not found in the system_stm32f10x.c file.

Vangelis Fortounas
Associate II
December 7, 2020

hello, use RCC_PLLSource_HSE_Div2 as parameter to RCC_PLLConfig()

JJohn.3
JJohn.3Author
Associate III
December 9, 2020

Thank you for your cooperation,

I changed it ,I just see a (8Mhz)semi-sinusoidal wave on PA8 instead of 36Mhz. but it doesn't work:

void CLOCK(void){
	
	RCC_DeInit(); 
	
	RCC_HSEConfig(RCC_HSE_ON);
	
	if(RCC_WaitForHSEStartUp()!=ERROR){
	
 RCC_HCLKConfig(RCC_SYSCLK_Div1);
		
	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);	
	FLASH_SetLatency(FLASH_Latency_2);
		
		
//	RCC_PLLCmd(DISABLE);	
	RCC_PCLK1Config(RCC_HCLK_Div1); 						// APB1 Phrepheral 36Mhz, 
	RCC_PCLK2Config(RCC_HCLK_Div1); 						//PCLK is 36Mhz		
	RCC_PLLConfig(RCC_PLLSource_HSE_Div2,9); //36Mhz
	RCC_PLLCmd(ENABLE);
	
	while(RCC_GetFlagStatus( RCC_FLAG_PLLRDY)!=SET);
	
	FLASH_SetLatency(FLASH_Latency_2);
	
	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
	
//	RCC_ClockSecuritySystemCmd(ENABLE);
	
	while (RCC_GetSYSCLKSource() != 0x08);
	//SystemCoreClockUpdate();
	
}
	//else
//	{
//		GPIO_ResetBits(GPIOB,GPIO_Pin_0);
//	}
 
}

JJohn.3
JJohn.3Author
Associate III
December 9, 2020

In addition, I need to know what the "   RCC_MCOConfig(RCC_MCO_PLLCLK_Div2);" is. it means,will it produce the frequency after PLL multiplier? Because it returns 4Mhz in the output.

As I know, when the HSE and RCC_PLLSource_HSE_Div2 are selected the PLL frequency is 4Mhz (8Mhz crystal quartz) ( RCC_MCO_PLLCLK_Div2 should be 2Mhz or after PLL multiplier it shoud be (8/2=4Mhz as PLL input, 4*9=36Mhz , 36Mhz/2=18Mhz)

Vangelis Fortounas
Associate II
December 9, 2020

Hello

>RCC_MCOConfig(RCC_MCO_PLLCLK_Div2); is a seting on MCO multiplexer that selects the divided by 2, PLLCLCK freq as output to the MCO pin

>As I know, when the HSE and RCC_PLLSource_HSE_Div2 are selected the PLL frequency is 4Mhz (8Mhz crystal quartz) .. Yes it is true. This is the PLL input if selected as PLL source from PLLSRC multiplexer.

this link contains SPL help about clock configuration fuctions . You can also find examples .

0693W000006F9EpQAK.jpg