Difference between revisions of "STM32 Timer PWM Input Capture"

From Stm32World Wiki
Jump to navigation Jump to search
Line 41: Line 41:
  
 
[[File:PWM Input Capture example - Timer 2 confriguration.png|400px]]
 
[[File:PWM Input Capture example - Timer 2 confriguration.png|400px]]
 +
 +
A few things are important here.  First of all, no prescaler is set, so the counter resolution will be the full 96 MHz internal clock.  Second, the Combined PWM Input on Channel 1.  Notice that both Channel 1 and Channel 2 are "greyed out".  This is because Channel 1 is used a the primary channel, triggering on the rising edge, while Channel 2 counter is triggered on the falling edge (making PWM duty cycle calculation possible).
  
 
== Code ==
 
== Code ==

Revision as of 04:28, 13 March 2022

The timers in STM32 MCUs are incredible powerful. We have used them before to generate PWM Signals and to decode signals from rotary encoders. In this example we will use them to determine frequency an duty cycle of an external PWM signal.

For the example we will be using a Black Pill development board and we will be using the same MCU to both generate and to decode the PWM signal.

The source of the complete example is here.

Wiring

Since we are using the same board to generate and to decode the PWM signal, we need a jumper wire to connect the PWM Output to the PWM Input. The pinout resulting from the setup in the next section looks like this:

PWM Input Capture example - pinout.png

So, we will need to run a jumper wire from PB6 (the PWM output pin) to PA5 (the PWM input capture pin) - like this:

PWM Input Capture example - wiring.jpg

In this photo, a ST-Link device with serial port, and an oscilloscope probe has also been connected.

STM32CubeMX Setup

We can now begin configuring the STM32 in Stm32CubeMX. As usual the clock is critical.

PWM Input Capture example - clock settings.png

The 25 MHz crystal oscillator on the Black Pill _can_ generate a 100 MHz clock, but even though USB is not needed in this example, the 48 MHz frequency needed for USB is not possible when the clock is set to 100 MHz. I therefore often use 96 MHz, as this can be used for USB as well.

The most important frequency is the APB1 timer clock. In this example that will be running at the full 96 MHz.

We will be using Timer 4 to generate the PWM signal. The setup of that timer looks like this:

PWM Input Capture example - Timer 4 configuration.png

By default we set the prescaler to 47 and the counter period to 1000. This will result in a frequency of 96 MHz / 48 / 1000 - 2 kHz. The pulse has been configured to 900 resulting in a duty cycle of 90 %.

According to the datasheet of the STM32F411 MCU on the Black Pill board, the following timers are available:

STM32F411 Timers.png

Since Timer 2 has a 32 bit counter, we will be using that for the input capture (lower frequencies can be measured):

PWM Input Capture example - Timer 2 confriguration.png

A few things are important here. First of all, no prescaler is set, so the counter resolution will be the full 96 MHz internal clock. Second, the Combined PWM Input on Channel 1. Notice that both Channel 1 and Channel 2 are "greyed out". This is because Channel 1 is used a the primary channel, triggering on the rising edge, while Channel 2 counter is triggered on the falling edge (making PWM duty cycle calculation possible).

Code

Miscellaneous Links