cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Timer Period Discrepancy

mtw
Associate

I am setting up Timer 1 channel 3 on the STM32F4. I am looking to set up a period of 3.9ms or 256kHz. I am using ABP1 - 168kHz, the ARR is set to 65535, and a prescaler of 10. 

65535 / ( 168,000,000 / 10) = 3.9ms

My timer initialization is below.

 

   _timData.timer_ptr       = timx_Ptr;
   _timData.timerChannel    = Channel; //3
   _timData.duty_pct        = 0U;
   _timData.timPeriod_ticks = TimerPeriod_ticks; // 65535

   TIM_TimeBaseInitTypeDef timeBaseStructure =
   {
      .TIM_Prescaler = Prescaler, //10
      .TIM_CounterMode = TIM_CounterMode_Up,
      .TIM_Period = _timData.timPeriod_ticks,
      .TIM_ClockDivision = TIM_CKD_DIV1,
      .TIM_RepetitionCounter = RepetitionCounter,  // 0
   };
   TIM_TimeBaseInit( _timData.timer_ptr, &timeBaseStructure );

   // Set up the PWM output init structure.
   _timData.ocConfig.TIM_OCMode      = TIM_OCMode_PWM1;
   _timData.ocConfig.TIM_OutputState = TIM_OutputState_Enable;
   _timData.ocConfig.TIM_Pulse       = 0U;
   _timData.ocConfig.TIM_OCPolarity  = TIM_OCPolarity_High;

   // initialize channel 3
   TIM_OC3Init( _timData.timer_ptr, &_timData.ocConfig );
   // Enable the channel's preload register
   TIM_OC3PreloadConfig( _timData.timer_ptr, TIM_OCPreload_Enable ); // Channel 3
   TIM_InternalClockConfig( _timData.timer_ptr );

   TIM_CtrlPWMOutputs( _timData.timer_ptr, ENABLE );
   TIM_Cmd( _timData.timer_ptr, ENABLE );
 
I have confirmed that the configuration registers do not change after initialization. I would expect to see a 3.9ms period, I consistently measure a period of 4.3ms with my scope. The only way I can get a 3.9ms period is altering the prescaler to 9 instead of 10. I do not understand why this is happening and what configuration within the timer controls this. This issue is independent of the CCR register. 
 
mtw_0-1718125824208.png

This scope captures the signal at a 50% duty cycle and the configuration shown above. I am quite lost on why this is happening.

1 ACCEPTED SOLUTION

Accepted Solutions
Bob S
Principal

The correct equation is:

(ARR+1) / ( 168,000,000 / (prescaler+1) ) = timer period

So with ARR=65535 and prescaler=10 you should get 4.29ms.   

View solution in original post

1 REPLY 1
Bob S
Principal

The correct equation is:

(ARR+1) / ( 168,000,000 / (prescaler+1) ) = timer period

So with ARR=65535 and prescaler=10 you should get 4.29ms.