1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Which microcontroller? \$\endgroup\$ – Jeroen3 Sep 24 '18 at 6:59
  • \$\begingroup\$ L432KC Nucleo . \$\endgroup\$ – Xavier Pacheco Paulino Sep 24 '18 at 9:04
1
\$\begingroup\$

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.

stm32l432 clock tree

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Xavier Pacheco Paulino Sep 24 '18 at 14:08
  • \$\begingroup\$ @XavierPachecoPaulino see edit \$\endgroup\$ – Jeroen3 Sep 24 '18 at 14:16
  • \$\begingroup\$ 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? \$\endgroup\$ – Xavier Pacheco Paulino Sep 24 '18 at 15:31
  • \$\begingroup\$ @XavierPachecoPaulino You multiply it with the period of the timer clock. \$\endgroup\$ – Jeroen3 Sep 24 '18 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.