STM32-Peripheral’s Timers: Input PWM

STM32-Peripheral’s Timers: Input PWM

Chronographic Timer by Vivier, 1870-1877 (chronograph - timer)


Figure 2: Timer Table

The advanced-control timers (TIM1&TIM8) consist of a 16-bit auto-reload counter driven by
a programmable prescaler ,

The general-purpose timers (TIM2 to TIM5) consist of a 16-bit or 32-bit auto-reload counter driven by a
programmable prescaler

The TIM9 to TIM14 general-purpose timers consist of a 16-bit auto-reload counter driven by
a programmable prescaler

It may be used for a variety of purposes, including measuring the pulse lengths of input signals (input capture) or generating output waveforms (output compare, PWM,
complementary PWM with dead-time insertion). Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds using the timer prescaler and the RCC clock controller prescalers
The basic timers TIM6 and TIM7 consist of a 16-bit auto-reload counter driven by a
programmable prescaler. They may be used as generic timers for time-base generation but they are also specifically used to drive the digital-to-analog converter (DAC). In fact, the timers are internally
connected to the DAC and are able to drive it through their trigger outputs.
The timers are completely independent, and do not share any resources

TIMER Generic features

The Timer features include:
1. 16-bit up, down, up/down auto-reload counter.
2. 16-bit programmable prescaler allowing dividing (also on the fly) the counter clock frequency either by any
factor between 1 and 65536.
3. Up to 4 independent channels for:
– Input Capture
– Output Compare
– PWM generation (Edge and Center-aligned Mode)
– One-pulse mode output
4. Synchronization circuit to control the timer with external signals and to interconnect several timers together.
5. Supports incremental encoder for positioning purposes

How to use this driver

1. Initialize the TIM low level resources by implementing the following functions depending on the selected
– Time Base : HAL_TIM_Base_MspInit()
– Input Capture : HAL_TIM_IC_MspInit()
– Output Compare : HAL_TIM_OC_MspInit()
– PWM generation : HAL_TIM_PWM_MspInit()
– One-pulse mode output : HAL_TIM_OnePulse_MspInit()
– Encoder mode output : HAL_TIM_Encoder_MspInit()
2. Initialize the TIM low level resources :
a. Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
b. TIM pins configuration
◦ Enable the clock for the TIM GPIOs using the following function:
◦ Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
3. The external Clock can be configured, if needed (the default clock is the internal clock from the APBx), using the following function: HAL_TIM_ConfigClockSource, the clock configuration should be done before any start function.

4. Configure the TIM in the desired functioning mode using one of the Initialization function of this driver:
– HAL_TIM_Base_Init: to use the Timer to generate a simple time base
– HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an Output
Compare signal.
– HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a PWM signal.
– HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an external signal.
– HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer in One Pulse
– HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
5. Activate the TIM peripheral using one of the start functions depending from the feature used:
– Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
– PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
– One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
– Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(),
6. The DMA Burst is managed with the two following functions: HAL_TIM_DMABurst_WriteStart()

For this blog we are going to use timer in input PWM mode

PA9 & PA10 Pin used for debugging purpose as UART Tx & UART Rx respectively. this blog is specially to demonstrate timer in input PWM mode, output will be available in serial terminal. Connect PA5 & PA6 pins for testing.

to learn more about timer in output PWM mode click here , input capture mode click here

Creating a new STM32 executable project

  1. The easiest way to create a new STM32 C/C++ project is to use the STM32 project wizard. It is selected through the menu [File]>[New STM32 Project].
  2. The MCU/MPU selector and Board Selector tabs can be selected at the top of the window. Use the first tab to create project for a specific device and the second if a project for a specific board is needed. today we will select specific device.
Figure 1

3. Select SMT32F429ZI in MCU Selector tab

4. According to the settings in Figure 3, the project is meant to be stored in the default location with the following

options set:

• C project

• Executable binary type

STM32CubeIDE targeted project type

Press [Next] to open the Firmware Library Package Setup page.

Figure 3

5. In this page, it is possible to select the STM32Cube firmware package to use when creating the project. In this case, the default settings are used. Press [Finish] to create the project. (Fig 4)

Figure 4

6. wait for project creation process

Figure 5

7. Initial view of STM32CUBE ide after project generation

Figure 6

8. Go to System core > SYS > Enable Serial wire debug

Figure 8

9. If require enable external clock

Figure 9

10. Clock configuration

System clock: 16 MHz , APB2 timer & peripheral clock 16 MHz, APB1 timer clock 16 MHz & APB1 peripheral clock 8 MHz

Figure 3: Clock Configuration

11. Enable Peripheral Configuration

12. Timer 3 Configuration

Go to System mode > Timers > TIM3 > Clock Source > Internal Clock

Channel 1 > PWM Generation

Figure 4: TIM3 Configuration

13. Enable Peripheral Configuration

Prescaler : 2048 & Counter period : 15625

APB1 clock : 16MHz, Interrupt time interval after calculation : 2 Second

Figure 5: TIM3 Parameters

14. Timer 2 Configuration

Go to System mode > Timers > TIM2 > Clock Source > Internal Clock

Channel 1 > Input PWM mode

Figure 6: TIM2 Configuration

15. TIM2 Parameter settings

Prescaler : 2048 & Counter period : 15625

APB1 clock : 16MHz, Channel 1: Rising edge, Channel 2: Falling edge

Figure 7: TIM2 Parameters

16. Enable TIM2 Interrupt

Figure 8: TIM2 Enable Interrupt

17. Press below icon for code generation

Figure 6 Cube IDE Code Generation

18. Pin configuration

TIM9 : PIN Configuration

19. Sample Code:

u_int32_t IC1 = 0u;
u_int32_t IC2 = 0u;
u_int8_t flag =0u;
uint8_t aShowTime[50] = {0};
uint8_t aShowDate[50] = {0};


void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
	if ( htim->Instance == TIM2)
      IC1 = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);

      if (IC1 != 0u)
        IC2 = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_2);
      if (IC1 != 0u && IC2 != 0u)
        printf("IC1 = %d & IC2 = %d\n",IC1,IC2);
        IC1 = 0u;
        IC2 = 0u;

int main(void)
  // For PWM Without Interrupt use this function HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
    while (1)

      /* USER CODE BEGIN 3 */

20. Output at Serial terminal

TIM10: Output at tera term
Software Tools:
  1. STM32CubeIDE
  2. STM32CubeMx
  3. Teraterm
Hardware Setup:
  1. STM32F429IDISCOVERY board
  2. Mini USB Cable
  3. Jumper wire

Successfully demonstrated timer in input PWM mode.

If you enjoyed this article, share your feedback.

  1. STM32 HAL Library
  2. STM32 UM1718 document
Similar topics:
  2. STM32 Peripherals: GPIO
  3. STM32 Peripherals: DAC
  4. STM32 Peripherals: ADC using Polling Mode
  5. STM32 Peripherals: UART Interrupt Mode
  6. STM32-Peripheral’s-SPI: Polling Mode
  7. STM32 Peripherals : Base timer in interrupt mode

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: