cancel
Showing results for 
Search instead for 
Did you mean: 

APB1/TIM2 clock

hospodar
Associate III
Posted on April 03, 2015 at 20:02

How to find out APB1 clock frequency? I want to know TIM2 clock, which is connected to APB1. I use STM32F103 and standard peripheral library.

Thank you.

12 REPLIES 12
francescatodiego
Associate II
Posted on April 03, 2015 at 22:20

TIM2 clock = APB1 clock if APB1 prescaler == 1

TIM2 clock = 2*APB1 clock if APB1 prescaler != 1

RM0008 page 93

APB1 clock soruce is AHB prescaler but AHB clock input can be

HSI

HSE

PLL

you should

give

more information

on which is the

clock source

The AHB prescaler clock output (SYSCLK) can be also direct to MCO pin and you can measure it with scope

I dont use the STM32F1xx family but I think the system_stm32f1xx.c contain all the clock settings info.

hospodar
Associate III
Posted on April 04, 2015 at 00:34

Yes, in system_stm32f1xx.c is a lot about clock, but I can not identify where is this informations.

Is there some particular functions/definitions used for seting this frequencies and prescallers?

EDIT:

I added this statements to my code:

RCC_ClocksTypeDef ClksFreq;

RCC_GetClocksFreq(&ClksFreq);

I obtain informations about frequencies in structure:

SYSCLK  72Mhz

HCLK  72 MHz

PCLK1  36 MHz

PCLK2  72 MHz

ADCCLK  36 MHz

So, timer TIM2 clock is 36 MHz, right?

Posted on April 04, 2015 at 01:48

SYSCLK (CPU), HCLK (AHB), PCLK1 (APB1), PCLK2 (APB2)

/* HCLK = SYSCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 04, 2015 at 01:51

So, timer TIM2 clock is 36 MHz, right?

No, per the Clock Tree in RM0008, a TIM on APB1, where APB1 is DIV2, the TIMCLK is APB1*2, ie 72 MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on April 04, 2015 at 19:07

Thank you clive1.

I have this configuration of timer:

timerInitStructure.TIM_Prescaler = 10;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 65535;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 200;
TIM_TimeBaseInit(TIM2, &timerInitStructure);

In documentAN4013, which is about timers is following relation: Update_event = TIM_CLK/((PSC + 1)*(ARR + 1)*(RCR + 1)) From that, I expected update event frequency about 0,5 Hz or time period of 2 second, respectively. However, this is not true. Time period is far more short than 2 second (I turn LED ON and when update interrupt ocurr, turn LED OFF). The LED just blink for very short time. What I forgot?
Posted on April 04, 2015 at 20:38

Pretty sure the repetition count is not available on all timer, TIM1 and perhaps TIM8 on your part.

timerInitStructure.TIM_Prescaler = 3600 - 1;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 40000 - 1;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure);

Or perhaps 50000-1, 2880-1 or 60000-1, 2400-1
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sirpak
Associate II
Posted on April 19, 2015 at 17:34

Hi Clive,

I have an issue with clock. In my application there is a 12 MHz external oscillator. This is multiplied by 6 in the PLL in order to obtain 72MHz as sysclk. By mistake I setup the APB1 without dividing the clock, as you can see in the code below, so the frequency is double of the limit 36 Mhz. I supply this clock to TIM2, TIM3, TIM4 and USART2. Everything is working fine since many years. Would you please advise which are the possible malfunctioning I can expect?

Thanks in advance.

RCC_PLLConfig (RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);  

/* HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1); 

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1); 

/* PCLK1 = HCLK */

RCC_PCLK1Config(RCC_HCLK_Div1);

/* ADCCLK = PCLK2/4 */

RCC_ADCCLKConfig(RCC_PCLK2_Div2); 

Posted on April 19, 2015 at 22:42

Would you please advise which are the possible malfunctioning I can expect?

Well it's not going to catch fire. You're likely to see odd behaviour if any of the critical paths get tickled over voltage and temperature.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
huydungshpy
Associate II
Posted on April 20, 2015 at 02:17

hi Clive1!

I think he want 

time period of 2 second so  

TIM_Period = 20000 - 1.