STM32 DAC.
The key difference between digital and analog electronics is that digital electronics operates with only two signal levels, logic zero and logic one. This doesn't mean that if we connect a multimeter to a microcontroller pin, it will show 0 or 1 V. Figuratively speaking, zero can be thought of as the absence of a signal and one as the presence of a signal, while the magnitude of the signal itself depends on the supply voltage of the chip. If your MCU is powered from 3 V, logic zero will correspond to 0 V and logic one to 3 V. To get intermediate voltage values on a microcontroller pin, a DAC (digital-to-analog converter) is used.
The STM32 DAC has the following features:
I'd like to point out right away that data is first loaded into the data holding register (DHRx), and only then, on an event (if enabled), transferred to the output register (DORx).
DAC_CR control register:
DMAEN2 (DAC channel2 DMA enable) — a one in this bit enables DMA.
MAMP2[3:0](DAC channel2 mask/amplitude selector) — this bit field sets the amplitude of the generated signal.
WAVE2[1:0](DAC channel2 noise/triangle wave generation enable) — this bit field enables the hardware generator and selects the waveform. 0 — hardware generator disabled, 1 — noise generation, 2 — triangle wave generation.
TSEL2[2:0](DAC channel2 trigger selection) — this bit field selects the event source on which data is transferred from the data holding register (DHRx) to the output register (DORx).
TEN2(DAC channel2 trigger enable) — a one in this bit enables the transfer of data from the data holding register (DHRx) to the output register (DORx) by trigger. With a hardware event this takes 3 clock cycles, with a software event 1.
BOFF2(DAC channel2 output buffer disable) — a one in this bit disables the DAC output buffer; it is enabled by default.
EN2(DAC channel2 enable) — a one in this bit turns on the DAC.
Figuratively, the register can be divided into two parts; they are completely identical but configure different channels.
DMAEN1(DAC channel1 DMA enable) —
MAMP1[3:0](DAC channel1 mask/amplitude selector) —
WAVE1[1:0](DAC channel1 noise/triangle wave generation enable) —
TSEL1[2:0](DAC channel1 trigger selection) —
TEN1(DAC channel1 trigger enable) —
BOFF1(DAC channel1 output buffer disable) —
EN1(DAC channel1 enable) —
DAC_SWTRIGR software trigger register:
SWTRIG2(DAC channel2 software trigger) — writing a one to this bit generates a software event for the second channel.
SWTRIG1(DAC channel1 software trigger) — writing a one to this bit generates a software event for the first channel.
Data can be loaded into the data holding register in several formats: 12-bit right-aligned, 12-bit left-aligned, and 8-bit. Each format has its own register, which greatly simplifies configuration.

DAC_DHR12Rx — for loading 12-bit right-aligned data.
DAC_DHR12Lx — for loading 12-bit left-aligned data.
DAC_DHR8Rx — for loading 8-bit right-aligned data.
If the data of both DACs needs to be updated simultaneously, this can be done using the following registers.

DAC_DHR12RD — for updating both channels simultaneously, 12-bit right-aligned.
DAC_DHR12LD — for updating both channels simultaneously, 12-bit left-aligned.
DAC_DHR8RD — for updating both channels simultaneously, 8-bit right-aligned.
DAC_DOR1 data register — lets you read the current output state of the first DAC.
DAC_DOR2 data register — lets you read the current output state of the second DAC.
I needed the DAC to get a voltage of 1 V on an MCU pin. The code below does that.
The STM32 DAC has the following features:
- two channels, 12-bit, can operate in 8-bit or 12-bit mode
- output voltage from 0 to Vref
- hardware noise and triangle wave generation
- simultaneous data loading into both DACs
- a separate DMA channel for each DAC
- conversion start by trigger
I'd like to point out right away that data is first loaded into the data holding register (DHRx), and only then, on an event (if enabled), transferred to the output register (DORx).
DAC_CR control register:
DMAEN2 (DAC channel2 DMA enable) — a one in this bit enables DMA.
MAMP2[3:0](DAC channel2 mask/amplitude selector) — this bit field sets the amplitude of the generated signal.
WAVE2[1:0](DAC channel2 noise/triangle wave generation enable) — this bit field enables the hardware generator and selects the waveform. 0 — hardware generator disabled, 1 — noise generation, 2 — triangle wave generation.
TSEL2[2:0](DAC channel2 trigger selection) — this bit field selects the event source on which data is transferred from the data holding register (DHRx) to the output register (DORx).
TEN2(DAC channel2 trigger enable) — a one in this bit enables the transfer of data from the data holding register (DHRx) to the output register (DORx) by trigger. With a hardware event this takes 3 clock cycles, with a software event 1.
BOFF2(DAC channel2 output buffer disable) — a one in this bit disables the DAC output buffer; it is enabled by default.
EN2(DAC channel2 enable) — a one in this bit turns on the DAC.
Figuratively, the register can be divided into two parts; they are completely identical but configure different channels.
DMAEN1(DAC channel1 DMA enable) —
MAMP1[3:0](DAC channel1 mask/amplitude selector) —
WAVE1[1:0](DAC channel1 noise/triangle wave generation enable) —
TSEL1[2:0](DAC channel1 trigger selection) —
TEN1(DAC channel1 trigger enable) —
BOFF1(DAC channel1 output buffer disable) —
EN1(DAC channel1 enable) —
DAC_SWTRIGR software trigger register:
SWTRIG2(DAC channel2 software trigger) — writing a one to this bit generates a software event for the second channel.
SWTRIG1(DAC channel1 software trigger) — writing a one to this bit generates a software event for the first channel.
Data can be loaded into the data holding register in several formats: 12-bit right-aligned, 12-bit left-aligned, and 8-bit. Each format has its own register, which greatly simplifies configuration.

DAC_DHR12Rx — for loading 12-bit right-aligned data.
DAC_DHR12Lx — for loading 12-bit left-aligned data.
DAC_DHR8Rx — for loading 8-bit right-aligned data.
If the data of both DACs needs to be updated simultaneously, this can be done using the following registers.

DAC_DHR12RD — for updating both channels simultaneously, 12-bit right-aligned.
DAC_DHR12LD — for updating both channels simultaneously, 12-bit left-aligned.
DAC_DHR8RD — for updating both channels simultaneously, 8-bit right-aligned.
DAC_DOR1 data register — lets you read the current output state of the first DAC.
DAC_DOR2 data register — lets you read the current output state of the second DAC.
I needed the DAC to get a voltage of 1 V on an MCU pin. The code below does that.
#include "stm32f4xx.h"
void DAC_Init(void)
{
//enable port clocking
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
//PA5 push-pull
GPIOA->MODER |= GPIO_MODER_MODER5_1;
GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR5;
GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR5;
//enable DAC clocking
RCC->APB1ENR |= RCC_APB1ENR_DACEN;
//enable DAC2
DAC->CR |= DAC_CR_EN2;
//load 12-bit data into the right-aligned register
DAC->DHR12R2 = 1000;
}
