cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32F100B

Alex T
Associate II
Posted on April 05, 2017 at 16:19

I want to know how TIM2 of STM32F100 works. Can someone help me?

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

uint16_t PrescalerValue = 0;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (SystemCoreClock / 1000) - 1;

TIM_TimeBaseStructure.TIM_Period = 999;

I don't understand why TIM_Period is 999.

10 REPLIES 10
Posted on April 05, 2017 at 16:34

The counter goes through 0 to N-1, this has N states. The logic for comparing if the current count is N-1, and resetting/updating at the next clock is simpler to implement.

OutClock = TIMCLK / (P * Q); where Prescaler = Q-1, and Period = P-1

Your example looks to generate a 1Hz update. It prescales the TIMCLK to 1 KHz, and then counts off 1000 ticks.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Khouloud GARSI
Lead II
Posted on April 05, 2017 at 16:34

Hi

toader.alexandra

‌,

I'd highly recommend you to refer to TIM example under the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef1.html

firmware package.

The example is found under the path below:

STM32Cube_FW_F1_V1.4.0\Projects\STM32VL-Discovery\Examples\TIM\TIM_TimeBase

The example titled 'TIM_TimeBase' shows how to configure the TIM peripheral to generate a time base of one second with the corresponding Interrupt request.

You may take this example as a reference to correctly configure your peripheral.

Khouloud

Posted on April 05, 2017 at 16:46

Thanks, 

so who is P and Q?

Posted on April 05, 2017 at 16:46

OP is using the SPL, and doesn't understand the timer/counter at a conceptual level. ie the mechanics of the timer, and why the values are chosen

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 05, 2017 at 17:36

They would be factors.

If you know what frequency you want to generate, you have to determine what two integer divisors are required to get there from the input clock, assuming in this case both must fit in 16-bit, so can't exceed 65536

The TIM is clocked via the APB bus, if that's 24 MHz, dividing it by 24000 gets you to 1 KHz, ie a prescaler of 24000-1 or 23999

To get an update period of 1 Hz you must divide the 1 KHz by 1000,ie a period of 1000-1 or 999

So if TIMCLK = 24000000, and Q=24000 and P=1000

OutClock = 24,000,000 / (24000 * 1000)

OutClock = 1 Hz

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 05, 2017 at 18:19

sorry but i don't understand why you divide the  24 MHz by 24000? Why 24000? From where you get 24000?

Posted on April 05, 2017 at 19:54

It comes from (SystemCoreClock / 1000)

Where SystemCoreClock is 24,000,000 (what you're running the processor and buses at)

Divided by 1000, gets us 24000 (a valid integer between 1 and 65536)

24,000 ticks of a 24 MHz clock source takes 1ms, and 1ms is 1KHz

After the prescaler the TIM count increments every 1ms, a thousand of those and you've got yourself a second.

The equation for the TIM Update, ie when it rolls back to zero (0..999)

OutClock = (24,000,000 / 24,000) / 1,000

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 05, 2017 at 22:50

thank you so much

Max
ST Employee