2018-09-23 03:47 PM
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?
Solved! Go to Solution.
2018-10-10 05:59 AM
Hi @XP.1acheco ,
Khouloud.
2018-09-23 06:21 PM
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 ?
2018-09-24 07:33 AM
Hi @XP.1acheco ,
Question1:
For the STM32L432 devices, the timer clock frequencies are automatically defined by hardware. There are only two cases:
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.
2018-09-24 05:35 PM
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?
2018-09-26 08:25 AM
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.
2018-09-26 05:28 PM
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.
2018-09-27 02:28 AM
H @XP.1acheco ,
Could you please share your system clock and timer configuration.
Khouloud.
2018-09-27 10:14 AM
@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.
2018-09-28 02:27 AM
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.
2018-09-28 06:49 AM
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?