cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Configuration

A3
Associate III

Hi, I am using STM32F779NI for development. My requirements are:

  1. On PD15, i.e Timer 4 channel 4, I want to generate a PWM output.
  2. On PB7, i.e Timer 4 channel 2, I want to use it as an Input capture functionality.

Is it possible to configure the Timer for both this functionalities on different channels of same Timer?

1 ACCEPTED SOLUTION

Accepted Solutions

> Now, with the above timing value unchanged, I will just configure the IC functionality. If I want to measure the frequency of a signal at PB7,

> what will be the limitations?

Without any additional "tricks", like what @S.Ma​ mentioned above, or counting separately the overflows in an interrupt or counting the overflows using another timer in master-slave arrangement (and both ways is rather tedious expecially when it comes to resolving events at the vicinity of the overflow), the capture is basically restricted by:

> htim4.Init.Prescaler = 5;

> htim4.Init.Period = 1000;

which means, for capture, maximum resolution (granularity) i.e. the minimum time difference resolvable is (5 + 1)*1/ftim, and the maximum period you can capture is (1000 + 1)*(5 + 1)*1/ftim (i.e. the minimum frequency of the captured input signal is 1/that), where ftim is the frequency of the timer input clock.

JW

View solution in original post

10 REPLIES 10

Yes.

The counter's range is given by the PWM frequency, though.

JW

S.Ma
Principal

Actually if the channel 4 has DMA on channel compare, you can generate a burst of PWM pulses to extend the timer timeout by multiple of PWM periods...

Good idea, @S.Ma​ 

JW

Thanks for the reply.

Do you mean the frequency of the signal to be measured on PB7 will be limited to some upper lower range since the timing parameters are with respect to to the PWM configuration. ?

Following are the values set for PWM output. PWM signal of freq around 33khz is generated.

  htim4.Instance = TIM4;

 htim4.Init.Prescaler = 5;

 htim4.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim4.Init.Period = 1000;

 htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_Base_Init(&htim4) != HAL_OK)

 {

  return HAL_ERROR;

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

 if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)

 {

  return HAL_ERROR;

 }

 if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)

 {

  return HAL_ERROR;

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)

 {

  return HAL_ERROR;

 }

 sConfigOC.OCMode = TIM_OCMODE_PWM1;

 sConfigOC.Pulse = 500;

 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

 if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)

 {

  return HAL_ERROR;

 }

Now, with the above timing value unchanged, I will just configure the IC functionality. If I want to measure the frequency of a signal at PB7,

what will be the limitations?

A3
Associate III

Corrections: PWM signal of frequency around 16.5 KHZ is generated with the above timing parameter configurations.

> Now, with the above timing value unchanged, I will just configure the IC functionality. If I want to measure the frequency of a signal at PB7,

> what will be the limitations?

Without any additional "tricks", like what @S.Ma​ mentioned above, or counting separately the overflows in an interrupt or counting the overflows using another timer in master-slave arrangement (and both ways is rather tedious expecially when it comes to resolving events at the vicinity of the overflow), the capture is basically restricted by:

> htim4.Init.Prescaler = 5;

> htim4.Init.Period = 1000;

which means, for capture, maximum resolution (granularity) i.e. the minimum time difference resolvable is (5 + 1)*1/ftim, and the maximum period you can capture is (1000 + 1)*(5 + 1)*1/ftim (i.e. the minimum frequency of the captured input signal is 1/that), where ftim is the frequency of the timer input clock.

JW

A3
Associate III

Thanks @Community member​ 

Since the two requirement are mutually exclusive i.e generating PWM at one time and input capture at another time .So I am planning to use below settings.

  1. For PWM output, I will use

  htim4.Init.Prescaler = 5;

htim4.Init.Period = 1000;

2. For Input capture, as per the intended freq range of the signal, I will change htim4.Init.Prescaler and htim4.Init.Period . I will switch between the these two Timing values as per the current functionality required i.e PWM output or Input Capture thing.

Please let me know if there are any restrictions or limitations with this?

That sounds good.

JW

A3
Associate III

Thanks. It worked.