cancel
Showing results for 
Search instead for 
Did you mean: 

Use of clock dividers in timers (STM32F103)

alexpes59
Associate II
Posted on October 25, 2014 at 16:02

Hi,

I have just begun toying around with a STM32F103 (on a STM32 Nucleo board) and I have some troubles understanding the expected behavior of the clock divider in the timers. So my original goal was to simply implement a delay(uint i_ms) function that would wait a given amount of time . My implementation is the following : My init code is the following :

/**
* @brief Here we will initializes a general purpose timer (TIM2)
* that will be used for the delay functions.
*/
static
void
init_delay_timer(
void
)
{
uint16_t prescaler = 0;
TIM_TimeBaseInitTypeDef TIM2_config;
/*We enable RCC on the timer 2. */
/* Timers are low speed peripherals so we use the APB1 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Now we need a prescaler value in order to achieve the clock rate we need.
* Here we need a 1000Hz frequency*/
prescaler = (uint16_t) ((SystemCoreClock) / 1000) - 1;
/* Time base configuration */
/*65535 is actually the maximum value for the counter before it wraps around */
TIM2_config.TIM_Period = 65535;
TIM2_config.TIM_Prescaler = prescaler;
TIM2_config.TIM_ClockDivision = TIM_CKD_DIV1;
TIM2_config.TIM_CounterMode = TIM_CounterMode_Up;
/* Now we call the init function */
TIM_TimeBaseInit(TIM2, &TIM2_config);
/* We actually start the counter */
TIM_Cmd(TIM2, ENABLE);
}

And the delay function is :


/**

* @brief wait for i_ms

* @param i_ms milliseconds to wait (max : 65535)

*/

static
void
delay_ms(uint16_t i_ms)

{

assert_param(i_ms < 65536);

/* We set the counter value to 0 */

TIM2->CNT = 0;


/* Now we wait until we have counted enough ms*/

while
(TIM2->CNT < i_ms);

}

Which works fine. However, if I try changing the timer's clock divider fromTIM_CKD_DIV1 toTIM_CKD_DIV2 (or 4), it doesn't seem to have any effect. My understanding is that my delay should then be 2, or 4 times as long as with no divider. Am I missing something here? Thanks! #nucleo-f103 #timer
3 REPLIES 3
Posted on October 25, 2014 at 16:26

Am I missing something here?

It's unrelated to the clocking of the timer via the Prescaler and Period factors. It controls the clock used on the pin input filter/synchronizer.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
alexpes59
Associate II
Posted on October 29, 2014 at 13:09

Thank you for the explanation, it makes more sense now.

Posted on June 17, 2018 at 05:59

Clive Two.Zero wrote:

Am I missing something here?

It's unrelated to the clocking of the timer via the Prescaler and Period factors. It controls the clock used on the pin input filter/synchronizer.

Thanks Clive, 

I too had the similar question . Actually i m measuring LSE from TIM2 input capture and i am getting the count equivalent to 31950 something, the TIM count clock is 25Mhz . why i am not getting count equivalent to 32768 ?

is there any way i can synchronize the Input capture signal to the TIM CNT CLK ? will this clk div parameter helps ?