Library 33- PWM for STM32F4

I had enough of each time configuring PWM for some reason, so I decided to make a library for it. This library allows you to use PWM signal on all possible timers on all possible pins for timer. I made it for controling servos, but it can be used for anything else. PWM works in FAST PWM mode.

You just set your PWM frequency, timer you will use, channel on timer and you are ready to go.

If you need any specific and exotic settings, you should look at my PWM tutorial.

Library

Features

  • Drive PWM signal on all possible PWM timers
  • Only frequency is required for timer initialization
  • Supports all possible output pins for each timer
  • Fast PWM mode
  • Version 1.1 – September 15, 2014
    • PWM library splitted into a 2 libraries. One for PWM and one for timer properties. This allows me to use one library with timer’s settings on more other libraries.
  • Version 2 – January 03, 2015
    • Library has been rewritten, new examples below

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx TIM
  • TM
    • TM TIMER PROPERTIES
    • TM GPIO
    • defines.h

Table below shows all possible pins for each timer and channel. You can select any of max 3 pins for each output channel.

Timer Channel 1 Channel 2 Channel 3 Channel 4
PP1 PP2 PP3 PP1 PP2 PP3 PP1 PP2 PP3 PP1 PP2 PP3
TIM 1 PA8 PE9 PA9 PE10 PA10 PE13 PA11 PE14
TIM 2 PA0 PA5 PA15 PA1 PB3 PA2 PB10 PA3 PB11
TIM 3 PA6 PB4 PC6 PA7 PB5 PC7 PB0 PC8 PB1 PC9
TIM 4 PB6 PD12 PB7 PD13 PB8 PD14 PB9 PD15
TIM 5 PA0 PH10 PA1 PH11 PA2 PH12 PA3 PI0
TIM 8 PC6 PI5 PC7 PI6 PC8 PI7 PC9 PI2
TIM 9 PA2 PE5 PA3 PE6
TIM 10 PB8 PF6
TIM 11 PB9 PF7
TIM 12 PB14 PH6 PB15 PH9
TIM 13 PA6 PF8
TIM 14 PA7 PF9
  • PPx: Pins Pack 1 to 3, for 3 possible channel outputs on timer.

Notes on table above

  • Not all timers are available on all STM32F4xx devices
  • Not all timers works with same tick frequency
  • All timers have 16bit prescaler
  • TIM6 and TIM7 don’t have PWM feature, they are only basic timers
  • TIM2 and TIM5 are 32bit timers
  • TIM9 and TIM12 have two PWM channels
  • TIM10, TIM11, TIM13 and TIM14 have only one PWM channel
  • All channels at one timer have the same PWM frequency!

This library do everything by it’s self. Everything you need to set is a PWM frequency you want to use (let’s say 50Hz for servo motor) for specific timer you will use. Library automatically sets period and prescaler for timer. This value is set, that allows you maximum possible resolution for PWM duty cycle at given frequency.

Everything is explained in example at the bottom. To work with PWM, you will need some typedefs and functions. They are below:

Example

For example, leds on STM32F4-Discovery and Nucleo F401-RE boards are used. STM32F429-Discovery doesn’t have leds connected to PWM pins.

I tested some other pins, if they are actually working with oscilloscope, but there is a lot of pins and timers, so I didn’t check for all possibility. They should work.

Simple Servo controller example

Both projects are available at my Github account, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...