cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 200 KHz timer interrupt period

tg.developer
Associate II
Posted on July 14, 2015 at 15:06

Dear Sirs,

I have used some general timer sample from library od STM32F103 MCU,but I am having

difficulties to setup time base for 200 Khz interrupt. Not matter what I do, I can't get higher

interrupt period like 0.5ms (2Khz).

/* Compute the prescaler value */

  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 65535;

  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* Output Compare Toggle Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 60; // assume 12Mhz counter clock

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

Please advice how to accomplish that

#interrupt #timer #stm32f103
5 REPLIES 5
Posted on July 14, 2015 at 16:51

The key thing to understand is that you are DIVIDING into the source clock.

/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 200000) - 1; // Count to get 200 KHz
TIM_TimeBaseStructure.TIM_Prescaler = 0; // DIV1
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// Enable the TIM, and the Update Interrupt

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
tg.developer
Associate II
Posted on July 14, 2015 at 18:58

Thanks clive1. Now I see about 11KHz, so it looks like, that there is something

wrong with clock initialization.

Posted on July 14, 2015 at 19:40

Hard to tell from here. You could do an RCC_GetClocks() type call and print out what the system thinks.

Have one of the TIM channel outputs in toggle mode and observe how that's clocking on a scope.

200 KHz is a bit high for interrupting. You'd have to look at what your IRQHandler is doing and how many cycles it's taking. The F1 is a bit unforgiving, the flash is slow, and you'd only have 360 cycles to play with.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
tg.developer
Associate II
Posted on July 14, 2015 at 21:58

I have corrected checked clocks setup and corrected bug I have.

Not I get 100kHz instead of 200kHz. Do you have some idea, what could

be a reason for this?

Thanks in advance 

Posted on July 14, 2015 at 22:46

I'm not very good at telepathy.

You'd have to look, and explain, what your APB clocks were at. Even on an APB1 (DIV2) at 36 MHz the TIMCLK should be 72 MHz.

If you're toggling a GPIO/LED, you're going to implicitly half the interrupt frequency when you view on a scope.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..