0:11
welcome to controllers tech this is the
0:14
continuation of nrf24l01 video
0:18
and this is the third and final part
0:21
in the first two parts we saw how to
0:24
configure the common section of the nrf
0:27
and in the second video we configured
0:31
this video will cover the receiving part
0:34
let's continue from where we left off
0:37
let's go back to the nrf.c
0:39
file and write a function to configure
0:43
this function will take two parameters
0:46
the address of the receive data pipe and
0:51
we have a very similar code that we had
0:53
for the transmit mode
0:55
but we still need to modify few things
0:59
so let's go back to the data sheet and
1:12
now we have to enable the rx data pipe
1:14
and i am choosing the pipe 1 to receive
1:22
later i will choose pipe 2 also but for
1:26
let's go with pipe 1. to enable this
1:30
we will write a 1 to the first position
1:37
write 0 cross 0 2 to the register
1:46
but this way we might disable other
1:48
pipes which are already enabled
1:51
so we will first read this register and
1:54
then only modify the bit 1.
2:02
this way other bits remain unaffected
2:16
next in line we have the rf channel
2:21
so write the channel number to the rf
2:25
this is already done so let's move on
2:33
now we have to set the address for the
2:36
we chose the data pipe 1 so we will set
2:39
the address for the same
2:41
data pipe 1 takes 5 bytes for the
2:45
let's write the 5 byte address for the
3:05
now we have to set the data size for the
3:08
we will set it to maximum 32 bytes
3:12
since we are not using dynamic payload
3:15
the receiver will always receive 32
3:19
some of it might be the garbage values
3:21
but we will take care of them later
3:24
write 32 to this register
3:34
then there is nothing left after this so
3:36
let's go back to config register
3:38
to power up the device we need to set
3:43
to enable the receive mode and also the
3:47
to power up the device
3:54
this is for the rx mode before we
3:58
we need to check if the data is
4:00
available in the fifo
4:02
to do that we will write a function is
4:06
the parameter of this function will be
4:10
so that we can check a particular pipe
4:11
for the data availability
4:14
this can be done in the status register
4:23
the zeroth bit of this register
4:25
indicates if the tx fifo is empty
4:27
or not but we are interested in bits 1
4:32
whenever there is data available in any
4:35
the respective bit will set here as you
4:39
the values here can be between zero to
4:43
so all we have to do here is check the
4:45
respective pipe number
4:47
if the bit is set that means the data is
4:55
other than these we also have to look
4:59
and that is rxdr this is a data ready rx
5:04
and it sets when a new data arrives in
5:11
so here we will read the status register
5:18
and then check for the bit number six
5:30
note that the rx pipe number bits start
5:33
so we will check the pipe number by
5:36
shifting it to the first position
5:43
if these conditions are satisfied we
5:45
will first clear the rxdr bit
5:56
and then return a 1 to indicate the
6:00
or else return 0 to indicate that the
6:03
data hasn't been received
6:05
and the last function will be the nrf
6:09
this function will be used to copy data
6:14
into our buffer the parameter is the
6:16
pointer to the buffer
6:18
where you want to save the data
6:22
we have to follow the same steps what we
6:25
did in the transmit function
6:27
but this command here will change
6:36
instead of payload send command we will
6:39
use the payload receive command
6:42
this will inform the nrf that we want to
6:44
receive the data from the data pipe
6:48
here we will send the command and after
6:52
we will receive data using whole spi
7:08
here we will receive 32 bytes of data
7:14
once the data has been received we will
7:23
these are all the functions we need for
7:27
let's copy these functions in the header
7:40
now i am going to create a new project
7:43
where i can receive the data
7:54
here i am using stm32f446r
7:58
controller let's give some name and
8:01
finish i will do a quick setup
8:04
like i did for the other microcontroller
8:09
here also i am using spi-1 and i will
8:18
other than the regular spi pins we still
8:22
two pins for cs and ce
8:25
i am selecting pb6 for the cs and
8:28
pc7 for the chip enable
8:34
let's enable the uart so that we can see
8:36
the data on the console
8:40
click save to generate the code
8:47
here is the nrf connector i am using to
8:50
simplify the connection
8:52
as you can see it have six pins for the
8:58
and this is how it is connected to the
9:04
with clock mossy miso chip select
9:07
and chip enable pins i have also
9:11
connected the interrupt pin
9:12
but as i mentioned we are not using it
9:27
here i will just copy those libraries
9:29
that we have created so far
9:38
now change the cs pin and ce pin
9:46
let's include the nrf.h in our main
9:50
file next we have to define the rx
9:53
address for the data pipe
9:56
this address must be same as the address
9:58
to which the transmitter is sending the
10:02
and create a buffer where we can store
10:07
now let's initialize the nrf
10:12
and set the nrf in the rx mode the
10:15
address will data the rx address
10:17
and the channel number 10. now in the
10:21
we will check if the data is available
10:24
if it is then we will receive the data
10:27
into the rx data buffer
10:32
and finally transfer this data to the
10:45
this should be rx data not rx address
10:50
let's debug this now
11:01
let's put a break point inside this
11:05
we got the hit so the receiver is able
11:10
let's open the serial console to see the
11:27
you can see we are getting hello world
11:30
but along with that we are getting some
11:34
this is because we are transmitting 32
11:37
but our data is actually less than that
11:39
and the same goes for the receiver also
11:43
let's check it in the hercules here also
11:46
we have the extra zed
11:52
anyway let's add a break point and check
11:54
the data that we are actually getting
12:11
you can see we are receiving hello world
12:15
there is terminating characters this is
12:19
we will check up to this terminating
12:23
to do this i am using string length
12:26
to get the length of the string
12:54
let's change this to character pointer
13:02
you can see the controllers on the top
13:05
now we have the proper string also note
13:08
that the led in the transmitter is
13:11
so the data is being transmitted and we
13:14
already know that the receiver is
13:17
because we are able to see the data
13:29
i am holding the reset button on the
13:31
transmitter and the data transmission
13:34
now it started again
13:37
so everything is working well so far
13:50
let's see what happens if i look for the
14:07
as expected we didn't get any data
14:10
this is because the transmitter is only
14:12
transmitting to the pipe one address
14:15
now we will set up our receiver so that
14:18
it can receive with pipe 2.
14:20
to do that we have to enable the data
14:31
this can be done here we have to write a
14:34
in the second position
14:50
after enabling data pipe 2 we have to
14:52
set the address for data pipe 2.
14:55
this part is little confusing so pay
14:59
as we have already discussed that the
15:01
foremost significant bytes for the data
15:03
pipe 2 to data pipe 5
15:05
will be same as data pipe 1. so we have
15:08
to set up the address for the data pipe
15:15
here the most significant address byte
15:23
and 0 cross e e is the least significant
15:30
so i am going to modify this least
15:32
significant byte here
15:34
and set it to anything other than zero
15:46
and we will write this address to the
15:58
for the pipe 2 address we can only write
16:02
and here we will write 0 cross e e
16:08
so pipe 2 address will be made of 4
16:10
bytes from pipe 1 address
16:12
and 1 byte from pipe 2 address
16:18
also we will set the payload size for
16:31
i don't need to give this pointer here
16:33
as that function will take
16:39
so after i tested it wasn't working and
16:42
i had to reconnect the power to the
16:45
i found out that it has something to do
16:48
values so i wrote a function to reset
16:53
you can use this function to reset the
16:57
or the fifo status register or all the
17:01
there is nothing special going on here
17:03
we are just writing the reset values to
17:06
the respective registers
17:08
these reset values are given in the
17:10
datasheet in front of every bit of every
17:14
now when we initialize the nrf we will
17:17
reset the registers first
17:21
i am passing 0 here because i want to
17:23
reset the entire nrf
17:28
you can pass any value other than 0
17:37
in the transmit function after checking
17:59
let's test the second data pipe now
18:05
we are receiving the data and it's
18:18
if we check the status register this
18:21
time you can see the second bit is set
18:23
and that means the data has been
18:25
received in the pipe 2.
18:30
so everything worked out well we were
18:33
able to do a simple transmit receive
18:39
by the way i have nif 24 nif24l01
18:42
and 0 1 plus and the data transfer works
18:46
well between the two
18:48
if you face any problems you should test
18:50
the transmit code separately
18:52
like i did in the second video
18:58
once you are sure that the transmit is
19:00
working then test the receiver
19:03
also you can create a function to read
19:06
and print them on the console just to
19:08
make sure that the values are set as per
19:12
i will include that in the final code
19:19
this is it for this video later
19:22
i will buy some more modules and i will
19:25
make another video for multi-receiver
19:27
and also for shock burst mode and for
19:32
this will have a separate playlist so
19:34
it's easier to access
19:36
these videos you can download the code
19:39
from the link in the description
19:42
keep watching and have a nice day ahead