cancel
Showing results for 
Search instead for 
Did you mean: 

Timers timing thorough explanation, please...

jamsoft
Associate II
Posted on April 22, 2013 at 17:19

Hi all,

I know it was disscused many times over the forum but still I can't get the correct results for the timer setting in PWM mode...

Can someone explain to me step by step how to understand timers timing, please?

I've used the clock configuration tool for STM32F4xx from ST.

The only thing I've changed was the HSE value from 25 to 8MHz as the STM32f4 Discovery board is fitted by the 8MHz quartz.

Now, the clock configuration is (based on the values indicated by the config tool):

HSE: 8MHz

SYSCLK: 64MHz

AHBx Prescaler: 4

HCLK frequency: 16MHz

APB1Prescaler: 1

TIM 2, 3, 4, 5, 6, 7, 12, 13, 14: 16MHz

TIM 1, 8, 9, 10, 11: 16MHz

PCLK1: 42MHz max, 16MHz

PCLK2: 84MHz max, 16MHz

Enable I2S Clock: checked as I need to communicate with the LIS302DL MEMS on the board

How I have to set the timing TIM1, TIM2 and TIM3 clock if I want to get:

a) 20ms period PWM output with duty cycle from 1 - 2 ms (for servo control)

b) input capture in period of 500 ms

c) input capture in period of 100 ms?

Teach me, please, step by step, what to do to get expected values from timers.

What value for timer prescaler and why?

What value for timer period and why?

What value for clock division and why?

Is the calculating same for all timers providing the above mentioned clock settings (by the clock configuration tool)?

My servo control was working well before I exchanged the file generated by the clock configuration tool (the settings was from some example found web). Now, it doesn't work anymore and anyway I would like to understand how to set the timers timing correctly for the case I'll change the Discovery board for something different (with the same STM32F407 microcontroler, of course) in future.

Thank you all for your answers in advance... I thing that it'll be useful for many folks here 🙂

Jirka

5 REPLIES 5
Posted on April 22, 2013 at 17:59

I'm not offering step-by-step tutorials.

For the servo PWM

The frequency (50 Hz) comes from the Prescale and Period multiplied together, and dividing down the input clock. They are Factors, they must fit in 16-bit values (65536).

The value written to the structure are N-1 values.

Skills required : multiplication, division, factoring

Say the TIMCLK is 184 MHz, the Prescaler require to clock the Time Base at 1 MHz is

184, or  TIM_TimeBaseStructure.TIM_Prescaler = 184 - 1;

For a 16 MHz TIMCLK, a Prescaler of 16 would be required to divide down to 1 MHz. TIM_TimeBaseStructure.TIM_Prescaler = 16 - 1;

1 MHz was chosen to simplify math, is 1000 ns or 1 us

50 Hz has a period of 20000 us, or 20 ms

A period of 20000 ticks of 1 us will result in a PWM frequency of 50 Hz.

TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 1 MHz / 20000 = 50 Hz (20ms)

A pulse of 1000 ticks of 1 us will be a high duty of 1 ms, 2000 would be 2 ms.

  TIM_OCInitStructure.TIM_Pulse = 1000;

  TIM_OCInitStructure.TIM_Pulse = 2000;

This is all pretty pedestrian math. The relationships of the clocks, and diagrams for the timers can be found in the Reference Manual, it's pretty dry reading, but an essential read.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jamsoft
Associate II
Posted on April 22, 2013 at 19:42

Thank you for your answer,

this is exactly what I have configured except I've forgotten one ''0'' for the period value 🙂

Now it is working well...

Iˇve still another questions:

For the TIM_GetCaptureX(TIM1) function - does the ''X'' stands for the channel X of the TIM1 or it is the Xth captured value, so e.g.

TIM_GetCapture2(TIM1) - TIM_GetCapture1(TIM1)  =  number of timer ticks?

What to do if I want to calculate the pulse length for signal that has not regular frequency?

Can I configure the timer to make an interrupt on the both edges (rising and falling) and make something like

TIM_GetCapture2(TIM1) - TIM_GetCapture1(TIM1)  =  number of timer ticks?

On many examples Iˇve found that they count the frequency by

Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);

Can you explain this calculation, please? Why there is 0xFFFF?

And last one...

If I configure the timer to make an interrupt by both - rising and falling edge so after some number of ticks an overflow happens when timer will start to count from 0 again.

But in this case, if there will be an interrupt by the falling edge at that time, the first value will be larger than the second one and the result of calculation will be incorrect. Is it true or it warks differently?

Thank you for your patience....I'm really a beginner...
Posted on April 22, 2013 at 21:19

They reference different channels of the timer, they latch the current count of the time base. It may not be appropriate to compute a delta unless the channels have been set up to capture opposite clock edges.

The code attempts to handle the condition where the 16-bit time base wrapped between two measurements, it fails to do this properly, and will be additionally confusing as a result. The example is bad, and insidious in this regard.

Provided the timebase is set to a period of 65535 (0xFFFF) the counter will represent 65536 states, and basic 16-bit unsigned math will suffice for delta computation, even across a wrap (0xFFFF->0x0000), will have a usable value provided the time does not exceed the wrapping time of the timer (ie 65536 ticks, or 65.536 ms for a 1 MHz time base).

uint16_t delta, current, prior;

delta = current - prior;
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
jamsoft
Associate II
Posted on April 22, 2013 at 22:39

Thank you for clarification. This I didn't understand from the manual... You are realy professional!

jamsoft
Associate II
Posted on April 23, 2013 at 00:29

Hmmm, but it is physically almost impossible because the signal is on one GPIO pin. So how I can use two channels - one for rising edge and the second one for the falling edge?

I'm sure that some easier solution exists. Measuring pulse width is surely common task.