DAC in STM32 || Sine wave || HAL || CubeIDE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to controllers tech today in this video I will show you guys some basic DAC working first I will show what digital to analog converters are used for and then we will create a sine wave using DAC in stm32 digital to analog converters are basically opposite to ADC they convert the digital signal to the analog signal and as the sine wave is the best representation of an analog signal that's what we will create today let's start with the cube ID first I am using stm32f4 for six ref1 zero 3 c 8 don't of DAC so I have to use this one let's set up the cube mix first things first I am using the external crystal for the clock next select the DAC configuration as you can see here the pin pa4 is selected as DAC out pin by default output buffer is enabled and there is no trigger we will keep it like this for the first part of this video next setup the clock and once done click Save to generate the code you you [Music] here is the generated main.c file before starting let's see the document provided by st on DAC as you can see the formula here to calculate the DAC output the ref is 3.3 volts DOR is the value that we don't know max digital values are given below depends on what resolution you use let's start programming now [Music] first of all I am creating a variable 0.2 is going to be the output voltage that is DAC output value we need to create another variable to store the respective digital value inside the main function first start the DAC in this while loop I am going to do the conversion from voltage to the digital value to do so we need to make do R as the subject of this formula once done we need to set this value to the DAC and then increment the value this process will continue with some delay remember that the value can't be more than reference voltage and that is 3.3 volts looks like we got some errors let's compile it again so the code compiled successfully time to flash it to the board just create a debug configuration you you observe the reading on the voltmeter and on the oscilloscope the voltage output from the pin is changing every 750 milliseconds and it doesn't go higher than three volts you you let's increase the time delay so that you can observe the change properly here we go the voltage is increasing every two seconds now so this was the basic idea about how to use the DAC in stm32 now we will create a sine wave using the same this process is mentioned in the same note but before this we need to make some changes to our setup first we need to select a trigger timer I am using timer 2 also we will use DMA for this entire process this will keep the CPU free [Music] [Music] next go to the timer setting that you chose this setup will decide the frequency of the wave and therefore is critical I am using timer 2 which is connected to the a PB 1 o'clock and this is running at 90 megahertz I am selecting the prescaler as 90 this will reduce the timer clock to 1 megahertz using a RR as 100 will further divide the clock to 10 kilo Hertz I will explain this particular setup in a while select update events here this is it for the setup let's generate the code now [Music] include math.h so that we can use the sine function I am creating an array to store the digital values of 100 samples of sine function we need pi value this function here will do the conversion let's take a look at the PDF again this is the formula that we are going to use number of samples is going to be 100 in our case f FF is for the 12 bit resolution [Music] now in the main function we first need to start the timer and then start the DAC with DMA [Music] [Music] now let's talk about the frequency of this wave [Music] as I mentioned the APB clock is at 90 megahertz and using a pre scalar of 90 will divide that clock by 90 making it one megahertz using the AR AR value 100 will further divide the clock by 100 making it 10,000 Hertz according to this application note the frequency of the sine wave is equal to time of frequency divided by number of samples in our case we have to further divide this value by 100 because we are doing 100 samples which gives us a frequency of 100 Hertz this is the frequency that I am expecting for the sine wave to have oh sorry I forgot to include the function to convert the values to digital form let's build and run the code [Music] you can see the sine-wave being produced with the frequency of 100 Hertz let's increase the frequency now [Music] I am dividing it by ten that will increase the frequency by multiple of ten now the frequency should be 1,000 Hertz [Music] you can see the frequency is 1 kilo Hertz this is it guys I hope you understood the video I will do more tutorials on DAC in future we will generate the triangular waves and noise waves too you can download the code from the link in the description keep watching
Info
Channel: ControllersTech
Views: 59,047
Rating: undefined out of 5
Keywords: stm32, dac, digital, analogue, converter, sine, wave, example, how to, generate, hal, cubeide, cube, f103, f446, f4, stm32cubeide, Tutorial
Id: 6Z1L6ox63j0
Channel Id: undefined
Length: 16min 7sec (967 seconds)
Published: Wed Nov 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.