Home ▸ Uncategorized ▸ Software Trigger vs Timer Trigger

STM32 DAC Software and Timer Trigger with HAL

This is the third tutorial in the STM32 DAC series. In the previous tutorial, we calculated the output voltage of the DAC from a 12-bit code, and we wrote the setDACVoltage() function that generates any voltage we want on the output pin.

In this tutorial, we will explore the trigger options of the STM32 DAC peripheral. A trigger is the event that tells the DAC when to take the value we have written and update the output voltage. We will see how the software trigger and the timer trigger work, and how they differ from each other. After that, we will configure Timer 2 to generate the trigger. We will also generate the step signal again, but this time the timer will control the frequency of the waveform.

I am using the same STM32H755 Nucleo project from the previous tutorials, and the code runs on the Cortex-M7 core. The same method works on other STM32 boards that have a DAC, but the timer clock and the list of trigger sources can be different, so it is worth checking both for your device.

This is the 3rd tutorial in the STM32 DAC Deep Dive series. If you have not set up the DAC in STM32CubeMX yet, the first tutorial explains the configuration that we use here.

How the STM32 DAC Trigger Works

So far, every voltage that we generated appeared on the pin right after we called setDACVoltage(). This happened because the trigger option of the DAC channel was set to None, and in this mode the DAC moves a new value to its output stage on its own after a very short delay. The trigger option changes this behavior, and it lets us decide the exact moment at which the output updates.

To understand the trigger, we need to know that the DAC keeps the digital value in two places. The function HAL_DAC_SetValue() writes the 12 bit DAC code into the data holding register (DHR). The converter itself works from a second register, the data output register (DOR). When a trigger is enabled, the value moves from DHR to DOR only when the trigger event arrives, and the voltage on the pin changes only after this move. The diagram below shows the complete path.

STM32 DAC data path showing HAL_DAC_SetValue writing DHR, a trigger moving the value to DOR, and the output on pin PA4

In STM32CubeMX, the trigger option of the DAC channel lists None, several hardware trigger events, and the software trigger at the end. Most of the hardware events are trigger outputs of the timers. Only one of these options can be active for a channel at a time, so we have to pick the source that fits our application.

STM32CubeMX DAC trigger drop-down list showing None, timer trigger out events and the software trigger

Each trigger event loads exactly one value into the converter, which means that the DAC does not convert the same value again and again. It converts the value once for every trigger, and the pin holds that voltage until the next trigger loads a new value.

In this tutorial, we will start with the software trigger because it is the simplest one, and then we will move to the Timer 2 trigger output.


STM32 DAC Software Trigger

The software trigger is used when we want to update the DAC output by writing a small piece of code in the IDE. I have selected the software trigger in the DAC configuration and generated the project again. Let’s see what happens when we keep the same code from the previous tutorial, where we only start the DAC with HAL_DAC_Start() and then call setDACVoltage() to set a voltage.

  HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
  setDACVoltage(2.46);

Why the Output Stays at Zero

After building the project and flashing it to the board, the voltage on the pin stays close to zero, and the voltage that we set in the code is not generated. The reason is the software trigger. The function setDACVoltage() calls HAL_DAC_SetValue(), which only writes the code into the data holding register. The DAC does not convert this value yet, because it waits for a trigger event, and starting the DAC alone does not create one.

The function HAL_DAC_Start() does contain a check for this case. Inside it, the HAL tests whether the software trigger is enabled, and if it is, the function sets the software trigger bit in the register SWTRIGR. This happens only once, at the moment we call HAL_DAC_Start(), and that is before we set our voltage. The trigger therefore fires while the data holding register still contains zero, and this is why the output stays near zero.

HAL_DAC_Start function in STM32CubeIDE showing the software trigger check that sets the SWTRIGR register bit

Adding the Software Trigger

The fix is to trigger the DAC again after we have set the voltage. We can copy the same statement from inside HAL_DAC_Start() and paste it after the call to setDACVoltage().

/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

setDACVoltage(2.46);
SET_BIT(hdac1.Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
/* USER CODE END 2 */

Make sure to provide the correct DAC instance (hdac1 in this case). Also note that the handle is hdac1, which is a normal variable and not a pointer, so we use a dot after hdac1. The member Instance is still a pointer, so the arrow after it stays the same.


Testing the Software Trigger

After flashing the project, the multimeter shows 2.46 volts on the pin PA4. This means that the DAC generates the required voltage, and the trigger statement is the reason for it.

Multimeter showing 2.46 V on the STM32 DAC output after the software trigger

STM32 DAC Timer Trigger

The software trigger works well for a single update or a quick test, but it has two drawbacks. We have to write the SET_BIT statement for every update, and the timing of the update depends on when the CPU reaches that statement. A timer trigger removes both problems. A hardware timer sends the trigger to the DAC by itself at a fixed rate, so we only start the timer, and the timer then triggers the DAC at every update event.

Select Timer 2 as the Trigger

In the DAC configuration, select the Timer 2 Trigger Out event as the trigger. This means that the DAC will be triggered by the event generated by the Timer 2.

STM32CubeMX DAC1 trigger set to the Timer 2 Trigger Out event

Now we need to configure Timer 2, so it can generate the event at a predefined rate. Let’s enable Timer 2 for the Cortex-M7 core and set its Clock Source to the Internal Clock.

STM32CubeMX TIM2 enabled with the Internal Clock as the clock source

Find the Timer Clock

The output frequency of the timer depends on the prescaler and the counter period (ARR). Before we set them, we need to know the clock that the timer receives. All the timers are connected to either the APB1 bus or the APB2 bus, and the bus decides the clock of the timer. The block diagram in the datasheet of the microcontroller shows this connection. In the diagram of the STM32H755, TIM2 sits in the group of peripherals that is connected to the APB1 bus.

STM32H755 datasheet block diagram showing TIM2 connected to the APB1 bus

This bus can run at a maximum of 120 MHz, but this does not mean that Timer 2 runs at the same clock. The timer clock is derived from the bus clock, and it can be different from it, so we need to check the exact value in the Clock Configuration tab of STM32CubeMX.

STM32CubeMX clock configuration showing APB1 peripheral clock at 100 MHz and APB1 timer clock at 200 MHz

In my project, the APB1 peripheral clock is 100 MHz, while the APB1 timer clock is 200 MHz. This means that the Timer 2 clock is 200 MHz.


Set the Prescaler and Counter Period

Now we have to scale this clock down to the frequency that we want, using the prescaler and the auto-reload register. I am going to use a prescaler of 200, which brings the clock down to 1 MHz. Then I will use a counter period of 1000, which divides the 1 MHz clock down to 1 kHz.

SettingValue in CubeMXActual dividerResult
Timer clock––200 MHz
Prescaler1992001 MHz counter clock
Counter Period99910001 kHz update rate

Set the Trigger Output

The last setting is in the Trigger Output (TRGO) Parameters of Timer 2. Here we set the Trigger Event Selection to Update Event. With this option, the timer sends a trigger signal to the DAC every time its counter overflows, which means every time it counts up to the period value and returns to zero. Our timer runs at 1 kHz, so this happens once every 1 millisecond. The diagram below shows how the clock of the timer becomes the trigger of the DAC.

Clock path from the 200 MHz TIM2 clock through the prescaler and counter period to the 1 kHz TRGO trigger of the DAC
STM32CubeMX TIM2 settings with prescaler, counter period and the TRGO update event selection

That is all the configuration we need, so let’s click Generate Code to update the project.


Timer Trigger Code

In the code, comment out the lines that we used for the software trigger, because we do not need them anymore. We start the DAC with HAL_DAC_Start(), set the voltage that we want with setDACVoltage(), and then start the timer with HAL_TIM_Base_Start(). This time, I am setting the voltage to 1.37 volts.

/* USER CODE BEGIN 2 */

HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

setDACVoltage(1.37);
HAL_TIM_Base_Start(&htim2);

/* USER CODE END 2 */

Once the timer starts, the trigger out event is generated after 1 millisecond, because the timer runs at 1 kHz. At this event, the DAC is triggered, and the multimeter shows 1.37 volts, which is the same voltage that we set with setDACVoltage(). After the first update, the timer keeps generating the same event once every millisecond, and the DAC keeps taking the latest value from the register. We have not changed the value, so the output stays at the same level.

Multimeter showing 1.37 V on the STM32 DAC output generated with the Timer 2 trigger

Software Trigger vs Timer Trigger

The timer trigger is similar to the software trigger in one respect, because in both cases the DAC waits for a trigger before it updates the output. The big difference is who creates the trigger. With the software trigger, we had to write the SET_BIT statement every time we wanted an update. With the timer trigger, we write no trigger code at all, and the timer triggers the DAC automatically at every update event. On each update, the DAC takes the latest value that we have written and updates the voltage on the pin.

Software triggerTimer trigger
Trigger sourceOur code sets the SWTRIG1 bitTimer 2 update event (TRGO)
Code needed per updateOne SET_BIT statementNone
Update timingDepends on when the CPU runs the statementFixed by the prescaler and the counter period
Best used forSingle updates and quick testsWaveforms with a fixed sample rate

STM32 DAC Staircase Signal

Since the timer runs at 1 kHz, we can use it to generate a waveform. To demonstrate this, I will generate a staircase signal, and the timer will decide how fast the signal moves from one level to the next. At every update event of the timer, we will give a new value to the DAC, and this way we get a waveform at the frequency that we want.

Define the Test Voltages

I have defined an array called test_voltages with eight values. The voltage rises in steps of 0.5 volts from 0 V up to 3.0 V, and the last level is 3.3 V, which is the maximum output of the DAC. The function setDACVoltage() converts each voltage into a DAC code, and the table shows the code that it calculates for every level.

float test_voltages[] = {
    0.0f,
    0.5f,
    1.0f,
    1.5f,
    2.0f,
    2.5f,
    3.0f,
    3.3f
};
LevelVoltageDAC code
10.0 V0
20.5 V620
31.0 V1241
41.5 V1861
52.0 V2482
62.5 V3103
73.0 V3723
83.3 V4095

The last level is a good example of the limit check inside setDACVoltage(). A request for 3.3 volts gives a code that is at or just above 4095, and the function keeps it at 4095, which is the highest code the DAC accepts.


Write the Timer Callback

Now we need a way to give a new value at every update. When the counter of the timer reaches the end of its period, the timer generates an update event, and if the interrupt is enabled, the HAL library calls the function HAL_TIM_PeriodElapsedCallback(). Since our timer runs at 1 kHz, this callback is called once every millisecond. Inside it, we pass the next voltage level to the DAC. The HAL declares this function as a weak function, so we can define our own version of it in main.c.

I will define a variable called sample_index first, which keeps track of the current element of the test_voltages array. Inside the callback, we call setDACVoltage() with the next element of the array, and then we increase the sample_index. When the sample_index becomes equal to or higher than 8, we reset it to 0, because the array has only eight elements.

volatile uint8_t sample_index = 0;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Instance == TIM2)
	{
		setDACVoltage(test_voltages[sample_index++]);
		if (sample_index >= 8) sample_index = 0;
	}
}

Enable the Timer Interrupt

We need to enable the Timer Interrupt in CubeMX, so the interrupt callback can be triggered. Open the NVIC Settings of Timer 2 in STM32CubeMX and enable the TIM2 global interrupt.

STM32CubeMX NVIC settings of TIM2 with the TIM2 global interrupt enabled

After generating the project again, call the function HAL_TIM_Base_Start_IT() instead of HAL_TIM_Base_Start(), which starts the timer in the interrupt mode.

/* USER CODE BEGIN 2 */

HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
HAL_TIM_Base_Start_IT(&htim2);

/* USER CODE END 2 */

The main function now has only two calls. The first one is HAL_DAC_Start(), which starts the DAC, and the second one is HAL_TIM_Base_Start_IT(), which starts the timer in the interrupt mode. Every time the period of the timer is finished, the callback is called, and there we pass the new voltage level.


When the New Value Appears

There is one detail about the timing that is useful to know. The timer generates the trigger for the DAC and the interrupt at the same update event. The DAC reacts to the trigger within a few clock cycles, and this is much faster than the CPU, which first has to enter the interrupt and run the callback code. When the callback writes a new value into the data holding register, the DAC has therefore already used the old value for this event. The new value is loaded at the next update event, so each value appears on the pin one sample period after it was written.

Timing diagram showing a value written by the callback at one TIM2 update event and output by the DAC at the next event

This delay of one sample does not change the shape or the frequency of the waveform, because every level is delayed by the same amount. It only matters when the first output has to be exact, and in that case the first value should be written into the register before the timer is started, as we did in the 1.37 V example.


Testing the Staircase Signal

Let’s flash the project to the board and connect the oscilloscope to the pin PA4. We can see a staircase signal with a total of eight levels, and the frequency of this signal is 125 Hz.

Oscilloscope showing the STM32 DAC staircase signal with eight levels at 125 Hz

This frequency of 125 Hz belongs to the waveform and not to the timer. The timer is still running at 1 kHz, and the signal needs eight steps before it returns to its first level. The next section explains how the two frequencies are related.


STM32 DAC Waveform Frequency

The waveform frequency depends on the rate at which the DAC is updated and on the number of samples in one cycle. The update rate is called the sample frequency, and it is equal to the frequency of the timer update event. The number of samples is the number of elements in the array. With these two values, we can calculate the frequency of the waveform.

Waveform frequency = Sample frequency / Number of samples

In our example, the sample frequency is 1 kHz and the array has eight samples, so the waveform frequency is 1 kHz / 8, which is 125 Hz. This matches the value that we saw on the oscilloscope. The diagram below shows one period of the staircase and how it is built from the eight samples.

Eight-level STM32 DAC staircase signal from 0 V to 3.3 V, where one waveform period equals eight samples

Changing the Frequency

To see how the timer controls the waveform, I will change the counter period of Timer 2 and check the result each time. First, I reduce the counter period to 100, which increases the timer frequency to 10 kHz. With eight samples, the waveform frequency should now be 1.25 kHz. Similarly, if I set the counter period to 250, the timer frequency will reduce to 4 kHz, and the waveform frequency becomes 500 Hz.

Counter periodSample frequencySamplesWaveform frequency
10001 kHz8125 Hz
10010 kHz81.25 kHz
2504 kHz8500 Hz
1258 kHz81 kHz
Oscilloscope captures of the STM32 DAC staircase signal at 1.25 kHz, and 1 kHz for different counter periods

Calculating the Counter Period

In many projects, we know the waveform frequency that we want, and we need to find the counter period for it. We can rearrange the formulas to get the counter period directly. The prescaler in this formula is the actual divider, which is 200 in our project.

Counter period = Timer clock / (Prescaler × Waveform frequency × Number of samples)

For example, to get a wave with exactly 1 kHz on the oscilloscope, the sample frequency has to be 8 kHz, because we have eight samples. The counter period is then 200 MHz divided by 200 and by 8 kHz, which is 125. We type 124 in STM32CubeMX, because of the extra one that the hardware adds. This is why the generated code in main.c shows htim2.Init.Period = 125-1. The timer frequency becomes 8 kHz, and the waveform frequency is exactly 1 kHz.

htim2.Init.Prescaler = 200-1;
htim2.Init.Period = 125-1;

The timer trigger therefore gives us predictable DAC update timing. The timer frequency decides the sample rate, the size of the array decides the number of samples, and together they decide the frequency of the waveform. The callback method that we used here has one limit, which is that the CPU is involved at every update. Each sample needs an interrupt and a floating point calculation, and this load grows with the sample rate.


Common STM32 DAC Trigger Problems

Output Stays at Zero

The most common reason for a zero output is a trigger that never arrives. With the software trigger, this means that the SET_BIT statement is missing, or that it runs before setDACVoltage() writes the value. With the timer trigger, it usually means that the timer was never started, or that the Trigger Event Selection of Timer 2 is not set to Update Event. It is also worth checking that the DAC trigger is set to the Timer 2 trigger out event, because a trigger from a different timer is ignored without any warning.


Callback Never Runs

If the staircase does not appear, the callback may not be called at all. This happens when the timer is started with HAL_TIM_Base_Start() instead of HAL_TIM_Base_Start_IT(), or when the TIM2 global interrupt is not enabled in the NVIC settings. In both cases, the timer still triggers the DAC, but the value in the data holding register never changes, so the output stays at a single level.


Wrong Frequency

A wrong frequency almost always comes from a wrong assumption about the timer clock. On this board, the timer clock is 200 MHz, which is twice the 100 MHz of the APB1 bus. A calculation based on 100 MHz gives a frequency that is off by a factor of two. The other common mistake is to forget the extra one that the hardware adds to the prescaler and the counter period, which is small for large values but visible for small ones. Finally, the waveform frequency also depends on the number of samples, so the formula has to use the real size of the array.


Conclusion

In this tutorial, we learned how the trigger of the STM32 DAC controls the moment at which the output updates. The function HAL_DAC_SetValue() only writes the code into the data holding register, and the trigger event moves it to the output register. With the software trigger, we set the trigger bit in the register SWTRIGR ourselves, and we saw that the output stays near zero until this bit is set. With the timer trigger, Timer 2 sends the update event to the DAC through the trigger output, and the DAC updates automatically at a fixed rate without any trigger code.

We then used the timer trigger to generate a staircase signal. The callback HAL_TIM_PeriodElapsedCallback() passes a new value from the test_voltages array at every update event, and the waveform frequency equals the sample frequency divided by the number of samples. By changing the counter period, we generated waveforms at 125 Hz, 500 Hz, 1.25 kHz and 1 kHz, and we confirmed each of them on the oscilloscope.

The CPU is still involved at every update, because we pass the output voltages inside the callback function. In the next tutorial, we will generate a sine wave using a sine lookup table. After that, we will involve the DMA, so that the waveform is generated automatically, without us providing a value at each update.


STM32 DAC Video Tutorial


Download 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.

Browse More STM32 DAC Tutorials

Coming Soon!


About the Author
Arun Rawat
Arun Rawat
Embedded Systems Engineer · Founder, ControllersTech

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
×

Don’t Miss Future STM32 Tutorials

Join thousands of developers getting free guides, code examples, and updates.