cancel
Showing results for 
Search instead for 
Did you mean: 

General questions on timers.

xpp07
Senior

Hello,

I'm still learning how to handle timers in CubeMx. I've got some simple questions.

I'm using TIM1 and TIM2 of the L432KC. In the clock tree, I see APB1/APB2 peripheral and timer clocks bus frequencies. I thought that when I set a prescaler, the APBx timer clock frequency is the one divided by the prescaler, but actually is the APBx peripheral clock frequency. What's the real difference?

Second question:

I've set timer 2 in PWM input to measure the period of an input signal. So,

.....

period = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2);

....

Does this say the period in milliseconds, right?

Third question:

I read in an app note:

The TIMxCLK frequency is set to 72 MHz, the prescaler is 0x0 so the TIM2 counter clock frequency is 72 MHz. So the minimum frequency value to measure is 1100 Hz.

How do I calculate that minimun frequency?

Last question:

When setting the PWM input mode or input capture mode to measure an external signal, how do I choose the prescaler and counter period effectively in CubeMx according to my frequencies range?

1 ACCEPTED SOLUTION

Accepted Solutions
Khouloud GARSI
Lead II

Hi @XP.1acheco​ ,

  • Correct, if you want to have a 10kHz PWM frequency, prescaler should be one using your configuration.
  • Right, regarding the timer clocks, "APBx timer clock" is only dedicated for the timer peripherals.

Khouloud.

View solution in original post

12 REPLIES 12
T J
Lead

Q2:

period would come back as timer "input clock" counter

you may need to enable the timeroverflow interrupt to extend your period counter, that would give you much higher period numbers and thereby goiving you much lower frequency detection.

which frequency range are you really interested in ?

Khouloud GARSI
Lead II

Hi @XP.1acheco​ ,

Question1:

For the STM32L432 devices, the timer clock frequencies are automatically defined by hardware. There are only two cases:

  1. If the APB prescaler is equal to 1, the timer clock frequencies are set to the same frequency as that of the APB domain.
  2. Otherwise, they are set to twice (×2) the frequency of the APB domain.

Question2:

The value returned by "HAL_TIM_ReadCapturedValue" is the number of clock cycles. 

Supposing that timer frequency is FCLK_TIM, the input signal's period (in seconds) is equal to HAL_TIM_ReadCapturedValue/FCLK_TIM.

Question3:

The minimum frequency that can be measured without a timer counter overflow is:

72MHz/ARR = 1100 Hz just in case ARR = 0xFFFF

However, TIM2's ARR is a 32bits register -> ARR is up to 0xFFFFFFFF.

-> minimum frequency = 72MHz/0xFFFFFFFF = 0.017 Hz.

Could you please precise from which application note, you have taken this information.

Question4:

You should choose the correct value in order to ensure that the maximum input signal value doesn't exceed the timer's overflow period.

You may refer to timer examples provided under cube packages.

Khouloud.

xpp07
Senior

Hi @Khouloud GARSI​ .

About question 1, I was confused because in CubeMx Clock Tree I see APBx Peripheral Clocks and APBx Timer Clocks, so I thought that Timers were clocked at APBx Timer Clocks, but actually they are clocked at APBx Peripheral Clocks.

About Questionn 3, I took the information from AN2581 App Note, but I realized that Timer 2 there is 16 bits, so it's okay.

About Question 4. I want to measure input signals up to 5 kHz, so how do I set the proper prescaler and counter period?

Khouloud GARSI
Lead II

Hi @XP.1acheco​ ,

Question 1: Timer peripherals are always clocked through APBx timer clocks not APBx peripheral clocks.

Question 4: The values should be chosen in a way to respect the following equation:

[Timer Fresuency ] / [(Prescaler+1)*(Period+1)] <= 5KHz

Khouloud.

xpp07
Senior

Hi @Khouloud GARSI​ 

About question 1:

You say that Timer peripherals are always clocked through APBx timer clocks. But when I was setting a PWM frequency in CubeMx, I chose the prescaler taking APBx timer clocks frequency as reference, and I got the wrong PWM frequency. Then, I used APBx peripheral clocks frequency as reference for the prescaler, and got it right.

Khouloud GARSI
Lead II

H @XP.1acheco​ ,

Could you please share your system clock and timer configuration.

Khouloud.

xpp07
Senior

@Khouloud GARSI​ 

Attached are my clock tree and timer setting for PWM 10 kHz on Timer 1 Channel 1. As you can see my prescaler is 1, so it's taking the 1 MHz instead of 2 MHz from APBx Timer Clocks.

0690X000006C5uLQAS.png

0690X000006C5uQQAS.png

Khouloud GARSI
Lead II

Hi @XP.1acheco​ ,

In your case the APB2 timer clocks is equal to 2MHz.

Below is the equation that we use to calculate the frequency:

PWM Frequency = [Timer Fresuency ] / [(Prescaler+1)*(Period+1)] ; Since your Prescaler is one, the frequency will be divided by two (prescaler+1).

In order to have your expected frequency you should set the timer prescaler to 0.

Khouloud.

xpp07
Senior

Hi @Khouloud GARSI​ ,

Thanks for your patience.

If I want 10 kHz, then applying the formula with prescaler one and counter period 99 I have:

PWM Frequency = [2 MHz] / [(1+1)*(99+1)] = 10 kHz

Why do you say my prescaler should be 0?

Let me see if I understood the concept: APBx timer clocks are specifically for timers, and APBx peripheral clocks are for the other peripherals, right?