HAL DAC How to Use

group DAC_How_To_Use

DAC peripheral main features

DAC channels

Two 12-bit Digital Analog Converters are integrated.

Up to 2 channels converters (i.e. channel1 and channel2) can be used independently (separate mode) or simultaneously (dual mode):

  • DAC channel output can be connected to on chip peripherals or to GPIO for voltage generation on device pin.

DAC triggers

Digital to analog conversion can be non-triggered using HAL_DAC_TRIGGER_NONE and DAC_OUT1 / DAC_OUT2 is available once writing to DHRx register.

Digital to analog conversion can be triggered by:

  • External event: EXTI Line,

  • Timers,

  • Low Power Timers,

  • Software using HAL_DAC_TRIGGER_SOFTWARE

DAC output buffer mode feature

Each DAC channel integrates an output buffer that can be used to reduce the output impedance, and to drive external loads directly without having to add an external amplifier. To enable the output buffer use HAL_DAC_SetConfigChannel() to set HAL_DAC_OUTPUT_BUFFER_ENABLED.

Channel in separate mode, input and output operation

  • Use HAL_DAC_StartChannel()

    to enable one channel and start conversion for this channel.

    Careful: when software trigger is selected, the call of

    HAL_DAC_StartChannel() starts the first conversion with the data previously present in DAC_DHRx register. So, a first call to HAL_DAC_SetChannelData is required in order to fix the initial value of DAC_DHRx. Then next conversions will use data set with HAL_DAC_SetChannelData() .

  • Use HAL_DAC_StopChannel() to disable one channel and stop conversion for this channel.

  • Use HAL_DAC_StartChannel_DMA()

    to enable one channel and start conversion for this channel using DMA to feed DAC converters.

    Careful: first issued trigger starts the conversion with the data previously present in DAC_DHRx register. So, a first call to HAL_DAC_SetChannelData is required in order to fix the initial value of DAC_DHRx.

  • Use HAL_DAC_StopChannel_DMA() to disable the channel and stop conversion for his channel using DMA.

  • Use HAL_DAC_SetChannelData() to set digital data to be converted.

  • Use HAL_DAC_GetChannelData() to get digital data to be converted.

DualChannel mode, input and output operation

Dual mode allows to use two DAC channels for simultaneous operation.

  • Use HAL_DAC_StartDualChannel()

    to enable both channel and start conversion for dual mode operation.

    Careful: when software trigger is selected, the call of

    HAL_DAC_StartDualChannel() starts the first conversion with the data previously present in DAC_DHRx register. So, a first call to HAL_DAC_SetDualChannelData is required in order to fix the initial value of DAC_DHRx. Then next conversions will use data set with HAL_DAC_SetDualChannelData() .

  • Use HAL_DAC_StopDualChannel() to disable both channel and stop conversion for dual mode operation.

  • Use HAL_DAC_StartDualChannel_DMA()

    to enable both channel and start conversion for dual mode operation using DMA to feed DAC converters.

    Careful: first issued trigger starts the conversion with the data previously present in DAC_DHRx register. So, a first call to HAL_DAC_SetDualChannelData is required in order to fix the initial value of DAC_DHRx.

    The same callbacks that are used in single mode are called in dual mode to notify transfer completion (half complete or complete), errors or underrun.

  • Use HAL_DAC_StopDualChannel_DMA() to disable both channel and stop conversion for dual mode operation using DMA to feed DAC converters.

  • When DualChannel mode is enabled (i.e. DAC channel 1 and channel 2 are used simultaneously): use HAL_DAC_GetDualChannelData() to get digital data to be converted and use HAL_DAC_SetDualChannelData() to set digital data to be converted simultaneously in channel 1 and channel 2.

Autonomous mode operation

Autonomous mode allows DAC operation in device low power mode.

DAC sample and hold feature

For each converter, 2 modes are supported: “normal mode” and “sample and hold” mode (i.e. low power mode). In the “sample and hold” mode, the DAC core converts data, then holds the converted voltage on a capacitor. In case of DAC output connected to on chip peripheral, this capacitor is internal. In case of DAC output connected to pin, an external capacitor must be connected to the output pin.

When not converting, the DAC cores and buffer are completely turned off between samples and the DAC output is tri-stated, therefore reducing the overall power consumption. A new stabilization period is needed before each new conversion.

The sample and hold mode allows setting internal or external voltage with a low power consumption cost (output value can be changed at any given rate either by CPU or DMA).

The sample and hold block and registers uses either LSI and run in several power modes: run mode, sleep mode, low power run, low power sleep mode and stop1 mode.

Low power stop1 mode allows only static conversion.

To enable sample and hold mode, enable LSI using HAL_RCC_LSI_Enable() .

Use HAL_DAC_SetConfigChannelSampleAndHold() to set sample_time_cycle, hold_time_cycle and refresh_time_cycle. Use HAL_DAC_EnableChannelSampleAndHold() or HAL_DAC_DisableChannelSampleAndHold() to enable or disable sample and hold mode.

DAC calibration feature

  • The 2 converters (channel1 and channel2) provide calibration capabilities.

  • Calibration aims at correcting some offset of output buffer.

  • The DAC uses either factory calibration settings or user defined calibration settings (i.e. trimming mode).

  • The user defined settings can be figured out using self calibration handled by HAL_DAC_CalibrateChannelBuffer() .

  • Use HAL_DAC_CalibrateChannelBuffer() to:

    • Calibrate one DAC channel and also to set automatically the trimming value.

    • Runs automatically the calibration.

    • Enable the user trimming mode.

    • Updates a structure with trimming values with fresh calibration results.

  • Use HAL_DAC_GetChannelBufferCalibrationValue() to retrieve the trimming value (the trimming factory setting after reset, or the user setting if HAL_DAC_SetChannelBufferCalibrationValue()

    have been used at least one time after reset).

    The user might store the calibration results for latter usage (for instance: monitoring the trimming as a function of temperature).

  • Use HAL_DAC_SetChannelBufferCalibrationValue() to set the trimming value.

DAC wave generation feature

Both DAC channels can be used to add noise wave or to add triangle wave.

DAC data format

The DAC data alignment format can be:

  • 8 bits right alignment using HAL_DAC_DATA_ALIGN_8_BITS_RIGHT

  • 12 bits left alignment using HAL_DAC_DATA_ALIGN_12_BITS_LEFT

  • 12 bits right alignment using HAL_DAC_DATA_ALIGN_12_BITS_RIGHT

DAC data value to voltage correspondence

The analog output voltage on each DAC channel pin is determined by the following equation: DAC_OUTx = (Vref+) x (DOR / 4095)

where DOR is the Data Output Register where Vref+ is the input voltage reference (refer to the device datasheet)

e.g. :
  • Assuming that Vref+ is equal to 3.3V, to set DAC_OUT1 to 0.7 V set data value 868 in DOR register:

  • DAC_OUT1 = (3.3) x (868 / 4095) = 0.7 V

DMA requests

A DMA request can be generated when an external trigger (but not a software trigger) occurs if DMA requests are enabled using HAL_DAC_StartChannel_DMA() .

High frequency interface mode

The high frequency interface informs DAC instance about the bus frequency in use. This is a mandatory information for DAC (as internal timing of DAC is bus frequency dependent). This is provided with the parameter high_frequency_mode handled in HAL_DAC_SetConfig() function.

The optimum frequency interface mode for the DAC peripheral can be determined by calling HAL_DAC_GetOptimumFrequencyMode() . Then the optimum high frequency interface mode (HFSEL bits) could be set with HAL_DAC_SetConfig() .

The high frequency mode is the same for all converters of a same DAC instance.

How to use the HAL DAC module driver

The DAC HAL driver can be used as follows (in separate channel configuration):

Initialize the DAC low level resources :

Polling mode, input and output operation

DMA mode, input and output operation

  • Start the DAC peripheral on a given channel using DMA with HAL_DAC_StartChannel_DMA()

    , the user specifies a data buffer and the data size to be converted through DMA.

    Careful: when software trigger is selected, the call of

    HAL_DAC_StartDualChannel() starts the first conversion with the data previously present in DAC_DHRx register. So, a first call to HAL_DAC_SetChannelData is required in order to fix the initial value of DAC_DHRx.

  • At the middle of data transfer HAL_DAC_ConvHalfCpltCallback() function is executed (user might have added his own code by overriding this weak function).

  • At the end of data transfer HAL_DAC_ConvCpltCallback() function is executed (user might have added his own code by overriding this weak function).

  • In case of transfer error, HAL_DAC_ErrorCallback() function is executed (user might have added his own code by overriding this weak function).

  • In case of DMA underrun, DAC interruption triggers and executes internal function HAL_DAC_IRQHandler. HAL_DAC_ErrorCallback() function is executed (user might have added his own code by overriding this weak function).

  • Stop the DAC peripheral using HAL_DAC_StopChannel_DMA() .

Callback registration

The compilation define, “USE_HAL_DAC_REGISTER_CALLBACKS”, when set to 1 allows the user to configure dynamically the driver callbacks.

These functions take as parameters the HAL peripheral handle and a pointer to the user callback function.

By default, after the first HAL_DAC_Init, all callbacks are reset to the corresponding legacy weak (overridden) functions. Callbacks can only be registered in state HAL_DAC_STATE_SEPARATE_CHANNEL_CONFIGURED or HAL_DAC_STATE_DUAL_CHANNEL_CONFIGURED. When the compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or not defined, the callback registering feature is not available and weak (overridden) callbacks are used.

Note

The trigger selection depends on the PWR mode, in STOP modes, only the triggers relative to peripherals that are functional in STOP modes are possible (ex: EXTI and LPTIM).

Note

Refer to the device datasheet for more details about output impedance value with and without output buffer.

Note

A helper macro is available to calculate the DAC conversion data (unit: digital value) corresponding to a voltage (unit: mVolt) refer to LL DAC driver: LL_DAC_CALC_VOLTAGE_TO_DATA