cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100 Timer-Clock

shadowrunner92
Associate II
Posted on November 12, 2011 at 15:41

Hello forum!

I'm trying to get familiar with the timers in the STM32 but now I got stuck at a problem I can't solve, first here's the concerned function:

void TIM4_Configuration(void)

{

  /* Compute the prescaler value  -> @24MHz Prescaler = 0 */

  int 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(TIM4, &TIM_TimeBaseStructure);

  /* TIM4 PWM2 Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 15535;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

  /* OPM Bit -> Only one pulse */

  TIM4->CR1 |= (1 << 3);

 

  /* TIM3 enable counter */

  TIM_Cmd(TIM4, ENABLE);

}

The purpose of this code is generating a pulse with a certain width ( TIMx_ARR - TIMx_CCR1 says the reference-manual, that means TIM_Period - TIM_Pulse using the library-structures , at least I think so :) ).

My problem now is that the pulse width is exact three times longer than the calculation says. A example how I calculated the pulse-width, so you can check if it's right :

SystemCoreClock = 24Mhz

TIM_Period = 65535

TIM_Pulse = 15535

(1 / SystemCoreClock) * (TIM_Period - TIM_Pulse) equals 2ms.

What am I missing?

Thank you in advance!

Chris

#timer-one-pulse
6 REPLIES 6
Posted on November 12, 2011 at 19:29

  TIM_OCInitStructure.TIM_Pulse = 15535;

At 24 MHz, presuming that's what the system is running at and an APB of DIV1 or DIV2, we'd expect the pulse high width to be 647 us

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shadowrunner92
Associate II
Posted on November 13, 2011 at 13:34

Posted on November 13, 2011 at 14:01

I'm more used to using PWM in repetitive modes, if you do that what mark/space times do you see.

If you are seeing 6 ms now, in your single-shot mode, it's probably because the board is not running at 24 MHz, but at 8 MHz. You should perhaps check using the MCO pin, or get the system to report it.

  RCC_ClocksTypeDef RCC_Clocks;

  RCC_GetClocksFreq(&RCC_Clocks);

 

RCC_Clocks.HCLK_Frequency = ??

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shadowrunner92
Associate II
Posted on November 13, 2011 at 14:39

Posted on November 13, 2011 at 18:02

The PLL does not come up by default, the internal 8MHz HSI drives the part at start up. The code in the library to bring up the PLL is dependent on external defines, and depending on how you created your project may not be included.

The function I mentioned decodes the actual register settings in the part, along with assuming an 8MHz source. It will detect if the PLL is being used, and what the multipliers are achieving.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shadowrunner92
Associate II
Posted on November 13, 2011 at 20:19