DAC in STM32
DAC stands for Digital to Analogue converter, and as the name suggests, it converts the Digital signal to Analogue form. Today in this tutorial, I will walk you through some basic DAC working in STM32. I will be using STM32F446RE controller with CubeIDE
As shown in Figure below, the DAC accepts data in three integer formats: 8-bit (the LS Byte of the data hold register), 12-bit right aligned (the twelve LS bits of the data hold register) and 12-bit left aligned (the twelve MS bits of the data hold register). In this tutorial I will use the 12 bit Right Aligned data format.
As shown in the figure above, the Out1 config is selected, and that selects the pin PA4 as the DAC_OUT pin. Also note that output buffer is enabled, and the Trigger is disabled for now.
This is all the setup we require at the moment. Setup the clock, and generate the project.
Some Insight into the CODE
In the main.c file, first I am going to define a float type variable called val. I am assigning a value of 1.2 to it. This is going to be the output voltage that we want on the pin.
Typically in normal digital form, the voltage on the pin can either be 0, or 3.3v. But with the DAC, we will vary the output on the pin in analogue form.
Variable var stores the digital value after we convert it from the analogue form. The analogue variable, val, is being multiplied to 4096, because I am using 12 bit resolution. Also note that 3.3 is the reference voltage (Vref).
Now It’s time to start the DAC, and write this value to the pin.
I am increasing the voltage on the pin every 500 ms. You can observe this in the video at the end of the post.
Sine wave Generation
As of now, we have produced an analogue signal of varying voltage on the pin PA4. Next, we are going to produce a sine wave at the pin. First, we have to go through some setup in the cubemx again. Below is my setup
This time, I have enabled the trigger from Timer 2, and also turned on the DMA. So that the processor can be used for other purposes. As the Timer 2 is enabled for the generation of the wave, i need to set up the Timer 2.
Timer 2 clock in my case is 90 MHz, and that’s why I am using a prescalar of 90. The clock will be divided by this value giving a total of 1 MHz frequency. The ARR value is 1000. This will further divide the clock, and now I have a frequency of 1000 Hz. This is the frequency of the TIMER 2 right now. The sine wave frequency also depends on the number of samples, that we are going to take. I will explain that in a while.
Let’s take a look at the code now.
I have included math.h, so that we can use the sine function.
We need to generate the digital values of sine function now. calcsin function will do that. I am taking 100 samples for better accuracy. The formula I have used here, is defined in the DAC document provided by ST. The picture is shown below.
Now, talking about the frequency of the sine wave. According to this document, the frequency is given by the formula below.
Remember the Timer frequency in our case was 1000 Hz, and we are taking 100 samples here. So, the frequency of the sine wave will be 1000/100 = 10 Hz.
Inside the main function, first we need to start the Timer, and than call for the function calcsin, so that the conversion can take place. Now, we will start the DAC with DMA. And that’s it, you can see the generated sine wave in the picture below.
You can control the frequency of this wave by either adjusting the TIMER parameters, or by changing the number of samples. For more details, check out the video at the end.
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020