0:07
hello everyone welcome to controllers
0:11
tech we have covered ADC DHT temperature
0:16
sensors SD card RTOS in the previous
0:20
videos today in this video I will
0:24
combine them all this video will cover
0:27
how to save data in the SD card which
0:30
will communicate through SPI this data
0:34
will be collected from the potentiometer
0:36
which is connected via ABC and the dht11
0:39
temperature sensor and all this will
0:43
work with free RT OS so let's start by
0:48
creating the project in cube ide first I
0:51
am using stm32f4 for 6r II but you can
0:57
use for other microcontrollers too I
0:59
will also upload the code for SDM 32
1:03
f103 controller let's see the connection
1:07
first SD card miso is connected to mesa
1:12
meseta mossy o'clock 2 o'clock and CS
1:17
pin is PB 6 in my case dht11 data pin is
1:23
connected to PA 0 and potentiometer
1:26
middle pin is connected to PA 1 all of
1:30
them are connected to same 5 volts
1:35
let's first turn on the ADC channel 1/4
1:47
enable the continuous conversion mode 3
1:59
cycle time is okay for us now enable the
2:02
fat FS for SD card leave everything to
2:07
default enable the free RTOS and again
2:15
leave everything as it is here I am
2:18
selecting the external crystal for the
2:27
enable SPI for connecting the SD card
2:33
also select the CS pinners output in my
2:37
case I am using PB 6 as the CS pin
2:45
select time base other than cystic this
2:49
is because of free RT OS timer one will
2:54
be used for the periodic delay I will
2:57
set it up later after setting the clock
3:00
timer seven will be used for micro
3:03
second delay for the dht11 sensor select
3:08
the UART so that we can see the output
3:10
on the serial console let's set the
3:14
clock now I am using external crystal
3:17
which is of eight megahertz and I want
3:21
the controller to run at 60 megahertz
3:24
note that but the apv clocks are also at
3:28
60 megahertz now let's set the timer
3:32
seven first keeping prescaler as sixty
3:36
will divide the APB clock to one
3:39
that means each count in auto reload
3:43
register will take one microseconds set
3:47
the auto reload register at max value
3:50
this is basically the maximum
3:52
microseconds that we can count up to now
3:56
let's go to the time of 1 this time I am
4:00
setting prescaler as 60,000 this will
4:03
divide the APB clock to 1000 Hertz here
4:08
each count in Auto reload register will
4:11
take one millisecond setting Auto reload
4:14
register to 2,000 means that it will
4:17
count for 2,000 milliseconds enable the
4:21
update event and turn only interrupt
4:24
guys I hope you understood the
4:27
difference between the setting of timer
4:28
1 and timer 7 in timer 7 each count of
4:33
auto reload register takes 1 microsecond
4:36
and inside the code we will not count up
4:39
to the fall a our value we will just
4:42
count up to however longer microsecond a
4:47
but in timer one each count takes one
4:50
millisecond and we are allowing it to
4:52
count up to the full auto reload
4:54
register value and that's why it will
4:56
create a periodic delay of two seconds
5:00
let's go to the SPI now divide the clock
5:05
such that the speed remains somewhat
5:08
near two megabits per second also set
5:11
the PA 0 as input for the dht11 sensor
5:15
this completes our setup now just save
5:19
it to generate the code
5:34
here is our main dot C file first of all
5:39
I will remove the CM sis related
5:41
functions so just copy the RTOS
5:45
functions into the main code and comment
5:48
out the CM sis I am going to remove all
5:54
the default task related functions
6:08
let's include the library files into our
6:11
project now you can get these after
6:14
downloading the code from the link in
6:30
include the dht11 and file handling dot
6:49
you need to make modifications here if
6:52
you are using other instances or pins
7:08
also define your CS pin here you can
7:12
change the SPI if you are using any
7:15
other instance of it as instructed here
7:19
we need to copy this and paste it in the
7:36
and this one should go inside the Cystic
7:41
handlers interrupt function I am using
7:50
timer six for the Cystic
8:09
also change the UART if you are using
8:12
any other now we need to make
8:15
modifications in the user disk i/o dot C
8:18
file just watch carefully you can
8:31
replace this entire file with the one
8:55
let's write the code now I am creating
8:59
three task handlers first they are for
9:02
DHT tasks SD card task and the ADC task
9:07
create a handler for the semaphore which
9:10
will be used in the DHT task now the
9:14
task functions where we will write the
9:17
task codes also define the variables to
9:21
hold the ADC value and temperature and
9:24
humidity values now inside the main
9:33
function first of all we will create the
9:36
files in the SD card so mount the SD
9:40
card format it create the ADC file
9:43
create temp file and unmount the card
9:58
now we will create the binary semaphore
10:02
remember that this semaphore must be
10:04
given first and we will not give it here
10:07
now create the tasks I am creating three
10:14
DHT tasks will be of lowest priority we
10:18
will use the semaphore to execute this
10:36
SD card will be of highest priority
10:39
because nothing should preempt it or
10:41
else the volume could fail and at last
10:52
before starting the scheduler we need to
10:55
start the timers also start timer seven
10:59
in normal mode and timer one in the
11:02
interrupt mode now let's write the task
11:13
in a DC task we will simply start the
11:16
ADC poll for the conversion get the
11:20
value and stop the ADC
11:32
this task will run every 500
11:36
now the DHT task here we will acquire
11:41
the semaphore first as the semaphore
11:45
will be given every 2 seconds we will
11:47
wait for two and a half seconds for the
12:01
if it doesn't acquire we will print this
12:03
string or else the dht11 read function
12:08
will read the data and save it in the
12:10
temperature and humidity note that we
12:19
will not release the semaphore here and
12:21
finally we will write the ADC and DHT
12:26
data to the SD card index variable is
12:29
used for indexing before using s printf
12:33
function use the malloc for allocating
12:35
the memory we will mount the SD card
12:39
update the files and unmount the SD card
12:42
this task will run every second now we
12:50
need to release the semaphore
12:51
periodically and you already know that
12:55
when we give semaphore from an ISR we
12:57
need to use the high priority task
12:59
switching also this completes everything
13:03
let's build this code and debug it
13:22
I have put those variables in the live
13:25
expression so that we can watch them I
13:28
will first show you my SD card there are
13:32
two files in it I will delete these
13:36
files and create new files just to show
13:40
you that format SD function also works
13:44
for now the format function only removes
13:47
the files and any directory will still
13:50
be there I am putting the card back in
14:01
let's run this now you can see the
14:05
values here there seems to be some error
14:08
in opening the temp file let's take a
14:11
look at the code again
14:21
it's all right here okay I have created
14:26
directory by mistake let's change this
14:29
to file build and debug again
15:02
now the output is perfect the files are
15:06
updating is expected this whole updating
15:10
data works every one second take a look
15:14
at the ADC value I will reduce it and
15:27
now I will increase it again
15:33
I am touching the temperature sensor now
15:48
you can see the rise in the value
16:00
okay let pause it and it's now the time
16:04
to check our SD card
16:16
you can see the two files here see how
16:20
the ADC values decreased and they
16:23
increased again just as I changed it
16:27
same thing with the temperature also
16:30
it's pretty much consistent but then
16:33
increases a bit so the data is saved
16:37
properly let's see what we get in the
16:48
output now obviously error in everything
16:52
here this is because the card is not
16:55
inserted into the module this is it for
16:59
the video guys I hope you understood
17:02
everything we discussed today you can
17:05
download the code from the link in the
17:07
description be safe and have a nice day