# General questions on Timers [STM32 & CubeMx]

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

First question:

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 mode to measure the period of an input signal. So, in the interrupt callback I can say:

period = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2);


Does this say the period in milliseconds? Or is this the right way to ask for the period?

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?

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?

• Which microcontroller? – Jeroen3 Sep 24 '18 at 6:59
• L432KC Nucleo . – Blue_Electronx Sep 24 '18 at 9:04

Regarding the AHB/APB question, this is best explained with a look at the Clock Tree.
AHB and APB are not the same bus. The AHB has memory and core, the APB has peripherals. Energy wise these might not have the same speed.

A special case in ST chips is that the peripherals, including the timers registers, are clocked at PCLK1 and the Timer clock source is on a multiple of PCLK1. This allows slower APB1, but still allows faster timers.

Does this say the period in milliseconds?

It gives the result in timer ticks.

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?

Clock / Timer maximum = 72 MHz / (2^16) = 1.09863281 kilohertz.

How do I choose the prescaler and counter period effectively in CubeMx according to my frequencies range?

You probably can't use one range if you need a large capture range. You could switch the prescalers automatically, like your multimeter does in Auto-ranging.

• It's not about AHB. For example, in the clock tree you attached, let's say APB1 Presc goes to PCLK (APB1 Peripherals) and X1,X2 (to TIMx). So, when setting the prescaler in CubeMx, I should use APB1 peripheral clock as reference, not TIMx clock. Why is that? And, how do I get the period signal with the timer ticks? – Blue_Electronx Sep 24 '18 at 14:08
• @XavierPachecoPaulino see edit – Jeroen3 Sep 24 '18 at 14:16
• Ok, thank you. So far my only doubt is about question 2. You told me it gives the result in timer ticks. So how I do deduce the period of the signal in ms? – Blue_Electronx Sep 24 '18 at 15:31
• @XavierPachecoPaulino You multiply it with the period of the timer clock. – Jeroen3 Sep 24 '18 at 20:45