0:14
hello everyone welcome to controllers
0:18
tech today in this video I will show you
0:22
guys some basic DAC working first I will
0:27
show what digital to analog converters
0:29
are used for and then we will create a
0:33
sine wave using DAC in stm32 digital to
0:38
analog converters are basically opposite
0:40
to ADC they convert the digital signal
0:44
to the analog signal and as the sine
0:47
wave is the best representation of an
0:50
analog signal that's what we will create
0:52
today let's start with the cube ID first
0:56
I am using stm32f4 for six ref1 zero 3 c
1:15
8 don't of DAC so I have to use this one
1:35
let's set up the cube mix first things
1:42
first I am using the external crystal
1:46
for the clock next select the DAC
1:49
configuration as you can see here the
2:01
pin pa4 is selected as DAC out pin by
2:05
default output buffer is enabled and
2:08
there is no trigger we will keep it like
2:11
this for the first part of this video
2:14
next setup the clock and once done click
2:18
Save to generate the code
2:50
here is the generated main.c file before
2:55
starting let's see the document provided
2:58
by st on DAC as you can see the formula
3:06
here to calculate the DAC output the ref
3:11
is 3.3 volts DOR is the value that we
3:16
don't know max digital values are given
3:21
depends on what resolution you use let's
3:25
start programming now
3:34
first of all I am creating a variable
3:38
0.2 is going to be the output voltage
3:41
that is DAC output value we need to
3:46
create another variable to store the
3:48
respective digital value inside the main
3:51
function first start the DAC in this
4:11
while loop I am going to do the
4:13
conversion from voltage to the digital
4:15
value to do so we need to make do R as
4:19
the subject of this formula
4:46
once done we need to set this value to
4:58
and then increment the value this
5:01
process will continue with some delay
5:05
remember that the value can't be more
5:07
than reference voltage and that is 3.3
5:24
looks like we got some errors
5:44
let's compile it again
5:46
so the code compiled successfully time
5:51
to flash it to the board just create a
6:29
observe the reading on the voltmeter and
6:31
on the oscilloscope the voltage output
6:35
from the pin is changing every 750
6:38
milliseconds and it doesn't go higher
7:08
let's increase the time delay so that
7:11
you can observe the change properly
7:31
here we go the voltage is increasing
7:35
every two seconds now so this was the
7:44
basic idea about how to use the DAC in
7:47
stm32 now we will create a sine wave
7:51
using the same this process is mentioned
7:55
in the same note but before this we need
8:03
to make some changes to our setup
8:21
first we need to select a trigger timer
8:24
I am using timer 2 also we will use DMA
8:35
for this entire process this will keep
8:57
next go to the timer setting that you
9:01
chose this setup will decide the
9:04
frequency of the wave and therefore is
9:07
critical I am using timer 2 which is
9:11
connected to the a PB 1 o'clock and this
9:15
is running at 90 megahertz I am
9:18
selecting the prescaler as 90 this will
9:22
reduce the timer clock to 1 megahertz
9:25
using a RR as 100 will further divide
9:29
the clock to 10 kilo Hertz I will
9:40
explain this particular setup in a while
9:43
select update events here this is it for
9:47
the setup let's generate the code now
10:44
include math.h so that we can use the
10:59
I am creating an array to store the
11:06
digital values of 100 samples of sine
11:10
function we need pi value this function
11:21
here will do the conversion let's take a
11:26
look at the PDF again this is the
11:33
formula that we are going to use number
11:37
of samples is going to be 100 in our
11:39
case f FF is for the 12 bit resolution
12:02
now in the main function we first need
12:05
to start the timer and then start the
12:31
now let's talk about the frequency of
12:46
as I mentioned the APB clock is at 90
12:49
megahertz and using a pre scalar of 90
12:53
will divide that clock by 90 making it
12:56
one megahertz using the AR AR value 100
13:01
will further divide the clock by 100
13:04
making it 10,000 Hertz according to this
13:08
application note the frequency of the
13:10
sine wave is equal to time of frequency
13:13
divided by number of samples in our case
13:16
we have to further divide this value by
13:19
100 because we are doing 100 samples
13:22
which gives us a frequency of 100 Hertz
13:26
this is the frequency that I am
13:29
expecting for the sine wave to have
13:59
oh sorry I forgot to include the
14:03
function to convert the values to
14:16
let's build and run the code
14:36
you can see the sine-wave being produced
14:39
with the frequency of 100 Hertz
14:50
let's increase the frequency now
15:00
I am dividing it by ten that will
15:04
increase the frequency by multiple of
15:06
ten now the frequency should be 1,000
15:34
you can see the frequency is 1 kilo
15:37
Hertz this is it guys I hope you
15:49
understood the video I will do more
15:52
tutorials on DAC in future we will
15:56
generate the triangular waves and noise
15:58
waves too you can download the code from
16:02
the link in the description keep