Coming soon |
This article explains what DAC is and how to use it through examples
1. What is a Digital to Analog Converter (DAC)?
A digital to analog converter is a system that converts a digital input signal or a value into an analog signal. It takes in a digital number or value as an input and converts it into an analog voltage. The voltage level corresponds to the binary number in the DAC output register.
To better understand DACs, we will take a look at the STM32L4 DAC in this article.
1.1. Simplified Block Diagram
1.2. DAC main features
- 8 or 12 bit mode: The DAC can support different input formats:
- 8-bit right aligned data format.
For dual mode (to provide input data for 2 DACs): 8-bit + 8-bit - 12-bit mode: either a right or left aligned mode
- 8-bit right aligned data format.
- Buffered output: the DAC output can have a low impedance buffer to drive external loads
- Sample and Hold mode to reduce power consumption
- Synchronized update capability
- DMA capability to offload CPU
- Several trigger inputs: The DAC output data can be updated by a timer, an external trigger or a software trigger
- Signal generation: DAC integrates small logic to generate noise waves as well as triangle waves
2. Configure DAC to generate a wave
2.1. Objectives
- Learn how to setup DAC as wave generator in STM32CubeMX
- Generate Code in STM32CubeMX and use HAL functions
- Create simple application to test DAC
2.2. Create the project in STM32CubeIDE
- File > New > STM32 Project in main panel.
.
In this example the NUCLEO-L476RG board is used.
- Select NUCLEO-L476RG board using Board Selector as shown in the below figure.
In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically. It may take some time.
- Save the project.
2.3. Configure DAC
- Search for DAC1 in the left search field
- Search for PA5 then click Reset_State (if already set)
- We will leave the clock configuration as default
- The output buffer is enabled and no trigger is needed.
- We can measure the output signal on this pin.
2.4. Generate project and edit main.c
- Click "Ctrl+S" to generate the project.
- Edit main.c
/* USER CODE BEGIN PV */
uint16_t value_dac =0 ;
/* USER CODE END PV */
/* USER CODE BEGIN 2 */
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
/* USER CODE END 2 */
/* USER CODE BEGIN 3 */
HAL_DAC_setvalue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value_dac);
If (value_dac<4095)
{
value_dac++;
}
else
{
value_dac=0;
}
HAL_Delay(1);
}
/* USER CODE END 3 */
- Explanation
Inside the If conditional statement, we will check if the DAC value is lower than 4095 (0xFFF) which is the maximum output value for a 12-bit resolution DAC:
- If the value is lower than the maximum value, we will increment it
- If not, we will reset it, back to zero. Then we will add some delay of one millisecond to have a stable output value.
This will be running in the infinite loop. So, the code will sweep through all the DAC output possible values, in roughly four seconds and then reset it to zero restarting the process all over and again.
2.5. Compile and flash
2.6. Build simple signal generator
Connect your multimeter or oscilloscope to the PA5 pin, the DAC output gradually increases up to a maximum output value which is 3V then it will go back to zero drawing a sawtooth wave.
3. References
Please check these references for further details
- AN3126: Audio and waveform generation using the DAC in STM32 products. [1]
- AN4566: Extending the DAC performance of STM32 microcontrollers. [2]
All these documents are available on www.st.com [1].
[[category:Getting_started_with_STM32_system_peripherals | 65]]