Skip to main content
A3_it
Associate III
November 6, 2019
Solved

Timer Configuration

  • November 6, 2019
  • 10 replies
  • 2061 views

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?

This topic has been closed for replies.
Best answer by waclawek.jan

> 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

10 replies

waclawek.jan
Super User
November 6, 2019

Yes.

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

JW

A3_it
A3_itAuthor
Associate III
November 7, 2019

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_it
A3_itAuthor
Associate III
November 7, 2019

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

S.Ma
Principal
November 6, 2019

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...

waclawek.jan
Super User
November 6, 2019

Good idea, @S.Ma​ 

JW

waclawek.jan
waclawek.janBest answer
Super User
November 7, 2019

> 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_it
A3_itAuthor
Associate III
November 7, 2019

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?

waclawek.jan
Super User
November 7, 2019

That sounds good.

JW

A3_it
A3_itAuthor
Associate III
November 13, 2019

Thanks. It worked.

dbgarasiya
Senior
December 28, 2019

I am using same controller

If i want to configure timer of 5 sec how can i do it

what are configuration to do ?