STM32 Output Compare and One-Pulse Mode Explained

In the previous article, STM32 Input Capture Mode Explained, the timer copied its counter value into the capture/compare register when an external edge arrived. Output compare works in the opposite direction. The program now writes a value into the capture/compare register, and the timer reacts when the counter reaches this value. This reaction can be a flag, an interrupt, or a change of the output pin, and it happens in hardware at an exact tick of the timer clock.

One-pulse mode builds on the same comparison logic, and it is usually described together with output compare for that reason. Instead of repeating a waveform, the timer waits for a trigger, produces one pulse with a defined delay and width, and then stops by itself. In this article, we will first look at how output compare works and which output actions are available. After that, we will configure a channel in CubeMX, start it with the HAL library, and then do the same for one-pulse mode.

The examples in this article use the NUCLEO-F446RE board with the same clock setup as in the previous articles, so the APB1 timer clock is 90 MHz. TIM2 runs as an output compare channel on pin PA5, which is connected to the LED LD2, and TIM3 produces the one-pulse output on pin PA6. This article is part of the STM32 timer tutorial series.

What Is Output Compare

A timer counts clock ticks in the counter register CNT, and every channel of the timer has a capture/compare register called CCR. In output compare, the CCR holds a value that the program has chosen, and a hardware comparator checks the counter against this value on every tick. When the two values are equal, the timer sets the compare flag of the channel (for example, CC1IF for channel 1), and it can also raise an interrupt or a DMA request. At the same moment, the channel can change the level of its output pin, depending on the output mode that was selected.

The important point is that the comparison is made by the hardware, so the event occurs at an exact counter value without any software delay. The program only decides when the next event should happen, and it does this by writing a new value into the CCR. This is the same idea as in the Prescaler, Counter and ARR article, where the counter and the auto-reload register define the time base, and the compare register adds a second point inside this time base that can trigger an action.

The diagram below shows the path from the two registers to the output. The comparator reports a match, and the match goes to two places, which are the output mode logic that drives the pin and the flag logic that can start the interrupt or the DMA transfer.

Block diagram of an STM32 output compare channel with CNT and CCR feeding a comparator that drives the output pin and the CCxIF flag

Output compare and input capture share the same capture/compare register, and the channel configuration decides which role it has. In input capture, the hardware fills the CCR with the counter value at the moment of an edge. Whereas, In output compare, the program fills the CCR, and the hardware compares it with the counter. This is also why the two modes are often combined, for example when a timer measures an incoming signal on one channel and generates a delayed response on another.


Output Compare Modes

The output mode of a channel decides what the timer does when the counter matches the CCR. All modes use the same comparison, and they differ only in the action that follows. The names in the HAL library start with TIM_OCMODE, and the exact set of modes that a channel supports depends on the timer, so do check the reference manual of the device.

Timing Event Without a Pin Transition

The first mode is called frozen, or timing mode, and its HAL name is TIM_OCMODE_TIMING. In this mode, the comparison result has no effect on the output pin, but the compare flag is still set, and the interrupt or DMA request still occurs. The channel therefore works as a pure timer event, and the pin can be used for something else. CubeMX offers the same idea as Output Compare No Output, which also leaves the pin unassigned.

This mode is useful when several timed actions have to be derived from one free-running counter. For example, a single timer with four channels can schedule four independent software events, and each channel only needs its own compare value and callback. If we compare this to a basic timer, this saves peripherals, because the counter is shared and only the compare values differ.


Toggle, Active, and Inactive Output Actions

The other modes change the output at the moment of the match. In toggle mode (TIM_OCMODE_TOGGLE), the output changes its level on every match. In active mode (TIM_OCMODE_ACTIVE), the output is forced to its active level on a match, and in inactive mode (TIM_OCMODE_INACTIVE), it is forced to its inactive level. The polarity setting of the channel decides whether active means a high or a low level on the pin.

A related pair of modes, TIM_OCMODE_FORCED_ACTIVE and TIM_OCMODE_FORCED_INACTIVE, sets the output directly without waiting for a match, which is useful for putting the pin into a known state at startup.

The diagram below shows two counter cycles with a compare value at about 60 percent of the auto-reload value. The frozen row shows only the flag, and the three other rows show how the same match changes the pin in each mode. The output changes only at the match, and it keeps its level at the end of the counter cycle, which is the main visual difference from PWM.

Timing diagram of STM32 output compare modes showing frozen, toggle, active and inactive outputs at a CNT and CCR match

Not every timer and channel supports every mode. Basic timers such as TIM6 and TIM7 have no compare channels at all, and some general-purpose timers have only one or two channels. Advanced timers add complementary outputs and dead-time features, which are described in STM32 Timer Types Explained. Before choosing a timer for a project, make sure to check the channel table in the datasheet and the timer chapter of the reference manual.


Output Compare Versus PWM

Output compare and PWM both use the same CNT and CCR comparison, and for this reason they are easy to confuse. In PWM mode, the output level depends on whether CNT is below or above the CCR, so the hardware produces a repeating waveform with a fixed period and a duty cycle without any further help from the program. In output compare, the output acts only at the moment when the two values are equal, so a single compare value produces one event per counter cycle, and the program is responsible for deciding what happens next.

A toggle channel with a constant CCR produces a square wave with a period of twice the counter period, whereas a program that moves the CCR forward after every match can produce nearly any pattern of intervals.


Configure Output Compare

In this section, we will configure TIM2 so that it toggles pin PA5 every 500 ms, which makes the LED LD2 blink once per second. The configuration needs four decisions: the timer channel, the compare value, the output mode, and whether an interrupt is required. If you are not yet familiar with the tool, STM32CubeMX Explained describes it in detail.

Select the Channel and Output Mode

Open Pinout & Configuration, and select TIM2. Set Clock Source to Internal Clock, and then set Channel1 to Output Compare CH1. On the STM32F446, TIM2_CH1 can be assigned to PA0, PA5, or PA15, and we use PA5 because the LED is connected to it. When the project was created from the board preset, PA5 is first labeled as a GPIO output for LD2, and this label has to be replaced by the timer function. The pin works as alternate function AF1 for TIM2, as explained in GPIO Alternate Functions Pins.

STM32CubeMX pinout with TIM2 Channel1 set to Output Compare CH1 on pin PA5

Set the Counter and Compare Value

The counter should run at 1 MHz, so each tick lasts 1 ยตs and the compare values are easy to calculate. With an APB1 timer clock of 90 MHz, a prescaler value of 89 divides the clock by 90, hence setting the counter clock to 1 MHz. TIM2 is a 32-bit timer on this device, so the counter period can be set to the full range. The first compare value of 500000 ticks corresponds to 500 ms.

ParameterValueReason
Prescaler (PSC)89Divides 90 MHz by 90 to give a 1 MHz counter clock
Counter Period (ARR)4294967295The full 32-bit range, so the counter runs freely
ModeToggle on matchThe output changes its level at every match
Pulse500000The first match occurs after 500 ms
CH PolarityHighAn active output drives the pin high
STM32CubeMX TIM2 parameter settings for output compare with prescaler 89, toggle on match and pulse 500000

Open the NVIC Settings tab and enable the TIM2 global interrupt. The interrupt is optional for a fixed square wave, but it is required here, because the program has to move the compare value forward after each match.

STM32CubeMX NVIC settings with the TIM2 global interrupt enabled for output compare

After the code is generated, the function MX_TIM2_Init() contains the output compare part below.


Start Output Compare with HAL

As with the other timer modes that we covered, the generated code prepares the peripheral but does not start it. The function HAL_TIM_OC_Start_IT() starts the counter, enables the output of the channel, and enables the compare interrupt. It takes the timer handle and the channel as arguments.

There are two more functions that we can use for other cases. HAL_TIM_OC_Start() starts the channel without an interrupt, which is enough when only the pin transition matters. Also the function HAL_TIM_OC_Start_DMA() lets the DMA controller write a sequence of compare values into the CCR. The interrupt version reports every match through the function HAL_TIM_OC_DelayElapsedCallback(), which we can override in the user code section of main.c.

The callback adds 500000 ticks to the current compare value, so the next match occurs exactly 500 ms after the previous one. The addition uses unsigned 32-bit arithmetic, which wraps around in the same way as the counter, so no special handling is needed at the end of the counter range. Because the HAL library leaves the compare preload disabled for output compare, a new CCR value takes effect immediately, and for this reason the new value must always lie in the future of the counter. The timer toggles the pin itself, and the program only schedules the next match, which keeps the blink period exact even when the CPU is busy with other work.


What Is One-Pulse Mode

One-pulse mode is a special operating mode of a timer in which the counter runs only once and then stops on its own. The timer waits in an idle state until a trigger arrives, and the trigger starts the counter. After a programmed delay, the output channel changes to its active level, and it stays active until the counter reaches the auto-reload value. At this update event, the counter stops, the output returns to its idle level, and the timer waits for the next trigger.

In One-Pulse mode, the result is described by the three values. The trigger is the event that starts the counter, and it is usually an edge on a timer input pin, an edge from another timer, or a software event. The delay is the number of ticks from the trigger until the output becomes active, and it is set by the compare value CCR. The pulse width is the time from this point until the counter reaches ARR, which is (ARR โˆ’ CCR + 1) ticks. In HAL, the output channel uses PWM mode 2, where the output is inactive while CNT is below the CCR and active from the match until the end of the counter cycle.

Timing diagram of STM32 one-pulse mode showing the trigger edge, the delay set by CCR, the pulse until ARR and the counter stopping

The automatic stop is the main difference from the other timer modes. In normal operation, the counter restarts after every update event, and the timer keeps producing events until the program stops it. In one-pulse mode, the hardware clears the counter enable bit at the update event, so the timer stays quiet and needs no interrupt or software action to end the pulse. A trigger edge that arrives while the pulse sequence is still running has no effect, because the counter is already running. The mode can also be set to repetitive, in which case the timer behaves as a normal timer, but a single pulse is the usual reason to use it.


Configure One-Pulse Mode

In this section, we will configure TIM3 so that a button press on pin PA7 produces one pulse on pin PA6. The signal will have a delay of 1 ms and a width of 5 ms. The pulse is generated only when the button is pressed, and nothing repeats afterward.

Set the Channels and the Trigger

One-pulse mode needs an output channel and a trigger source. We use channel 1 as the output on PA6, and the trigger is an edge on the channel 2 pin PA7. Both pins use alternate function AF2 for TIM3, as described in GPIO Alternate Functions Pins.

In the Mode panel of TIM3, set Slave Mode to Trigger Mode and Trigger Source to TI2FP2, which means that the filtered edge of the channel 2 input starts the counter. CubeMX greys out the Channel2 mode after this selection, because the channel 2 input is now used by the trigger path, and PA7 should appear in the pinout as TIM3_CH2. If it does not, click the pin and select TIM3_CH2 manually. Then set Channel1 to PWM Generation CH1 and tick One Pulse Mode. The One Pulse Mode setting is the part that stops the counter after one cycle. Without it, the trigger would only start the counter, and the timer would then repeat the same pattern continuously.

STM32CubeMX TIM3 mode panel with Slave Mode set to Trigger Mode, Trigger Source TI2FP2, Channel1 PWM Generation and One Pulse Mode enabled

TI2FP2 is only one of the possible trigger sources. The timer can also be triggered by an edge on its channel 1 pin (TI1FP1), by the external trigger pin ETR, or by an internal trigger from another timer (ITR0 to ITR3), and the reference manual of the device shows which of them each timer supports. The slave mode stays Trigger Mode in all cases, and only the source changes. Some timers do not support one-pulse mode at all, so the feature table should be checked before the timer is chosen.


Set the Timing and Pulse Width

We will configure the prescaler with the value of 89 again, so the counter runs at 1 MHz and one tick lasts 1 ยตs. A delay of 1 ms requires a compare value of 1000. For a pulse width of 5 ms, the auto-reload value must be 1000 + 5000 โˆ’ 1 = 5999, which follows from the formula in the previous section. The table below lists the values that we enter in the Parameter Settings of TIM3.

ParameterValueReason
Prescaler (PSC)89A 1 MHz counter clock
Counter Period (ARR)5999The counter stops after 6000 ticks, which ends the pulse
Pulse (CCR)1000A delay of 1 ms between the trigger and the rising edge
ModePWM mode 2The output is active from the match until the end of the cycle
CH PolarityHighAn active output drives the pin high
STM32CubeMX TIM3 parameter settings for one-pulse mode with prescaler 89, period 5999, PWM mode 2 and pulse 1000

The exact CubeMX menus can look a little different between versions, so check the generated code to confirm your settings. Below is the code that CubeMX typically generates in MX_TIM3_Init().

There are Three important things in this code. The call to HAL_TIM_OnePulse_Init() must use TIM_OPMODE_SINGLE, because TIM_OPMODE_REPETITIVE would let the counter keep running. The slave mode must be TIM_SLAVEMODE_TRIGGER, and the trigger polarity must be rising, so that a button press starts the sequence and the release does not. If the polarity or the filter value is not what you want, change it in a USER CODE block at the end of MX_TIM3_Init().


Connect the Button

The button connects directly to the trigger pin. One side of the button goes to the 3.3 V pin of the board, and the other side goes to PA7. PA7 must read low while the button is released, so enable the internal pull-down resistor in the GPIO Settings tab of the pin, or add an external pull-down resistor of about 10 kฮฉ. Without a pull-down, the pin floats and can start the sequence on its own. A logic analyzer or an oscilloscope probe connects to PA6 to observe the output.

Wiring of a push button between 3.3 V and PA7 with a pull-down resistor on a NUCLEO-F446RE for the one-pulse trigger

A mechanical button bounces for a short time, and the bounces appear as several rising edges. Edges that arrive during the 6 ms sequence are ignored, because the counter is already running, but a bounce that arrives after the sequence has ended starts a second pulse. A small capacitor across the button, a larger filter value for the trigger input, or a debounced button signal reduces this effect.


Start One-Pulse Mode

The start call enables the output of the channel, but it does not start the counter, because the trigger edge does that later.

If the generated code uses HAL_TIM_OnePulse_ConfigChannel() instead of the PWM channel configuration, the matching start function is HAL_TIM_OnePulse_Start(&htim3, TIM_CHANNEL_1). The main loop stays empty in both cases, which shows that the whole sequence runs in hardware. After the call, PA6 stays low and the counter stays stopped until the first press. Each press then produces one pulse that starts 1 ms after the edge and lasts 5 ms. A press that arrives while the pulse sequence is still running has no effect, and after the sequence ends, the counter stops and waits for the next press.

Logic analyzer capture of STM32 one-pulse mode showing a 5 ms pulse on PA6 starting 1 ms after the PA7 button edge

Trigger from Software

The pulse can also be started from code instead of a pin. The slave mode must still be set to Trigger Mode, but no signal has to reach the timer input. The function HAL_TIM_GenerateEvent() with the event source TIM_EVENTSOURCE_TRIGGER then starts the same sequence, and it can be called from any place in the program, for example from the callback of a button interrupt as described in GPIO Interrupts and EXTI.


When to Use Each Mode

Use the output compare mode when something has to happen at an exact counter value. One common case is running several software timers from one free-running counter, where each channel has its own compare value. Another case is a signal with edges at calculated times, or two outputs that stay a fixed time apart. A frozen channel is enough when you only need the event, and a toggle, active, or inactive channel is the right choice when the event should also change a pin. If you only need a regular waveform with a fixed period and duty cycle, PWM mode is simpler, because the timer repeats the waveform on its own.

Use one-pulse mode when you need exactly one pulse after an event, such as a button press. A typical example is the 10 ยตs trigger pulse that an ultrasonic distance sensor expects. Other examples are a camera flash trigger, a firing pulse that must come a fixed time after a zero-crossing detector, or a short strobe after a sensor signal. A delay loop or a timer interrupt can also make a pulse, but its timing depends on how fast the CPU responds. In one-pulse mode the hardware does the timing, so the delay and the pulse width stay the same every time.

The two modes can also work together. One timer can create a schedule of events with output compare, and a second timer in one-pulse mode can turn each event into a pulse with its own delay and width. If you want to measure a signal instead of creating one, input capture is the right tool, because it records when an edge arrived.


Conclusion

Output compare lets a timer act when its counter reaches a value that you choose. The match can set a flag, or it can also toggle the pin, set it to its active level, or set it to its inactive level. Unlike PWM, the output changes only at the match, so your program decides what happens next by writing a new compare value. This gives you control over the intervals and the offsets between outputs.

One-pulse mode uses the same comparison, but it adds a trigger and an automatic stop. The compare value sets the delay, and the pulse lasts from the match until the counter reaches the auto-reload value. After that, the counter stops and waits for the next trigger. For both modes, make sure that the timer and channel support the function you want, connect the correct pin and trigger, and keep the compare value inside the counter range. In the next articles of the timer series, we will look at more timer features that build on the same counter and channel logic.


Download the Project Here

Download the CubeMX project files and HAL source code โ€” all tested on real hardware.

If you find this useful, you can support us with a Donation as well.

What to Read Next

Article by Yashmiti

Yashmiti is a technical writer at STM32Guide, where she creates tutorials and guides that help embedded-systems learners and developers work confidently with STM32 microcontrollers. Her writing covers tools like STM32CubeIDE and STM32CubeMX, along with peripherals and communication protocols โ€” always with an emphasis on clear explanations and hands-on examples that take readers from concept to working project.

She's also part of the effort to build STM32Guide into an organized, easy-to-navigate resource for the STM32 community.

Leave a Comment