The channel of the DAC module outputs an analog voltage with the following detailed setup steps:
1) Turn on the PA port clock and set the PA4 as analog input.
Stm32f103zet6 DAC Channel 1 is connected to the PA4, so we first want to enable the Porta clock, and then set the PA4 as the analog input (although the input, but the STM32 inside will be connected to the DAC analog output).
2) Enable DAC1 clock.
As with other peripherals, you must first turn on the appropriate clock to use it. The STM32 DAC module clock is provided by APB1, so we first set the clock enable of the DAC module in the APB1ENR register.
3) Set the operating mode of the DAC.
This partial setting is all implemented through the DAC_CR setup, including: DAC Channel 1 Enable, DAC Channel 1 output cache off, no trigger, no waveform generator, and so on.
4) Set the output value of the DAC.
With the setup of the previous 3 steps, the DAC can start working, we use the 12-bit right-aligned data format, so we can get a different voltage value at the DAC output pin (PA4) by setting the DHR12R1.
Stm32 DAC configuration Process