0:09
everyone welcome to controllers tech
0:13
as the title says today in this video
0:16
we will see how to write the basic
0:18
library for the nif24l01
0:21
transceiver this is a very very long
0:25
and that's why i have split it into
0:28
the first part will cover the basic
0:30
setup and configuration
0:33
this configuration will remain common
0:36
and receive section in the second part
0:40
we will cover the transmitting data and
0:43
finally in the third part
0:44
we will see how to receive data my aim
0:48
through this video is not to provide you
0:51
but make you understand how to write one
0:54
according to your requirement
0:56
anyway the link for the code will be in
0:59
but i would suggest that you write your
1:03
let's start with the data sheet then
1:06
i have here the data sheet for nrf24l01
1:11
it's a transceiver which works around
1:17
i am saying around because we can set
1:20
and hence the frequency let's go through
1:24
and we will see some important things
1:26
that we will be using today
1:29
we have the pin description here we have
1:33
which is a digital input ce
1:36
stands for chip enable and it will be
1:40
or disable the device this pin is input
1:45
not the microcontroller it will be
1:49
and input to the nrf then we have chip
1:54
pin we have clock mossy and miso
2:00
and finally the interrupt pin which we
2:03
will not be using today
2:14
keep the supply voltage less than 3.6
2:36
here are the operational modes
2:40
we will be only focusing on rx and tx
2:47
we will use these configurations later
2:50
when we will set up the registers
3:02
it supports the air data rate of 1
3:13
then we have the rf channel frequency
3:16
it have the base frequency of 2400
3:20
and later we can add the channel number
3:22
to change this frequency
3:25
each channel will increment the
3:26
frequency by 1 megahertz
3:28
if you are using the data rate of 1
3:31
or by 2 megahertz in case of 2 mbps data
3:45
we can also set the output power
3:47
according to our requirement
4:01
we will not be using enhanced shock
4:03
burst for this tutorial
4:20
we have to set the payload length during
4:23
and it can vary between 0 to 32 bits
4:53
it supports auto acknowledgement it is
4:55
basically when the receiver send the
4:57
acknowledgement to the transmitter
4:59
but we will not be using this today
5:23
here we have the addressing system for
5:31
pipe zero have unique five bytes address
5:33
and between pipe one to pipe five
5:36
the four most significant bytes are
5:38
shared for the address
5:45
only the least significant bike changes
5:49
and keep in mind that the address
5:51
register for pipe 2 to pipe 5
5:53
is only 1 byte wide we will see this
5:57
in the receiving part of this video
6:04
then we have here is the s p i related
6:09
we can use the maximum data rate of 8
6:20
we will write the msb first using the
6:31
now we have reached the main part of the
6:34
so here we have our certain commands
6:37
which are like some special commands
6:40
these commands have some specific
6:42
functions for example
6:44
if we want to send the payload we must
6:46
use this command first
6:48
this will inform the nrf that the next
6:52
payload similarly before receiving the
6:56
we need to send this command
7:01
and this one here is to flush the rx
7:07
you will understand them once we will
7:10
start writing these registers and
7:15
now finally we have arrived to the
7:31
before i continue any further let's
7:34
create a new project in the cube ide
7:37
this is basically the setup part for
7:40
and if you are using any other
7:42
microcontroller you can set up the spi
7:45
and that's all i am using stm32f407z
7:52
this is the controller i am using the
7:55
reason for the use of this is
7:57
because it have the connector for the
8:01
you can see this connector and here i
8:03
have attached the transceiver
8:10
let's give the name to this project and
8:18
this here is the cube mx let's do our
8:23
i am selecting external crystal for the
8:27
i have eight megahertz crystal and i am
8:29
running the cpu at maximum
8:38
now we need to set up the spi
8:53
if you take a look at the schematics you
8:56
can see the nrf is connected to spi-1
9:12
we will use the full duplex mode
9:18
note here that three pins got selected
9:20
for the spi-1 and they are
9:26
we need to keep the spi clock below 8
9:35
let's see the schematics again
9:41
spi-1 pins are actually the pins pb3
9:52
so i will remap the spi to those pins
10:11
so let's select them as the output pins
10:24
i also want to select the leds for the
10:32
they are pins pf9 and pf10
10:38
that's all for the setup click save to
10:40
generate the project
10:48
let's create a new source file where we
10:50
can write the nrf related code
10:57
in order to communicate with nlrf we
11:00
need to write and read the data from the
11:04
to do that we will create a function to
11:07
simplify this operation
11:09
this function will write data to the
11:12
and its parameters will be the register
11:15
where you want to write the data into
11:17
and the data itself before that
11:21
let's define the chip select pin and the
11:36
now create a buffer which can hold two
11:40
the zeroth byte is the register and we
11:43
have to write a one in the fifth
11:57
this is because it's given in the data
11:59
sheet that whenever we have to send the
12:03
the fifth bit must be a one
12:06
these five a's are the five bits of
12:15
now before we start writing to the slave
12:17
device we must pull the cs pin low
12:21
i forgot to change this to cs
12:29
i think it's better if we create a
12:31
separate function for these pins
12:34
this is for selecting the slave device
12:39
this is for unselecting the slave device
12:48
ce enable will enable the device and ce
12:51
disable will disable the device
13:14
now we will simply call the chip select
13:19
next we will use whole spi transmit to
13:22
transmit the data to the spi
13:26
we need to define the spi handler first
13:32
let's call it nrf24spi
13:40
here we will send the buff the size will
13:44
the first byte will be the data byte and
13:47
let's give some time out
13:49
now we will release the device by
13:51
pulling the cs pin high
13:56
sometimes we need to write multiple
13:59
and that's why we will create a separate
14:14
it will basically be the same code
14:17
except first we will send the register
14:20
and then we will send all the data bytes
14:27
these functions are used in similar
14:29
manner in other devices also
14:32
take any spi device you will need these
14:35
functions in order to communicate with
14:46
now let's write a function to read the
14:48
data from a particular register
14:51
this function will return the data
14:56
again we will use the same code
15:03
but if you see the data sheet we don't
15:05
need any special instruction for the
15:08
we just have to send the 5-bit register
15:17
so here we will simply send the register
15:20
where we want to read the data from
15:23
let's define a variable
15:25
where we can store the data and now call
15:29
read to read one byte of data from the
15:45
and in the end we will return this data
15:50
this is the function to read multiple
15:54
starting from this register pointer to
15:58
where you want to store the data and the
16:00
number of bytes you want to read
16:17
let's write a separate function for
16:18
sending those special commands
16:23
these are the ones i am talking about
16:26
these commands are standalone commands
16:29
and they don't need to be written to
16:32
so we are creating a new function
16:36
the procedure will be same select the
16:39
send the command and release the device
16:43
this completes our general spi functions
16:46
any device like i mentioned these
16:51
and required to be used in any spi
16:54
device let's write a function to
16:56
initialize the nrf24
17:07
here we will start configuring these
17:10
starting with the first one config
17:15
before we configure the registers we
17:17
need to disable the device
17:19
and after configuring we will enable it
17:22
let me change these to enable and
17:44
we will disable the device actually we
17:48
don't need to call this chip unselect
17:50
i am just calling it let's see the
17:56
to put the device in receive or transmit
17:59
we will write a separate function for
18:01
that also we will power up the device
18:04
after putting it in the rx or tx mode
18:08
so these two bits will be configured
18:11
we are not using any crc today
18:15
then we have some interrupt related bits
18:18
which we won't be looking at today
18:20
so the config register will be zero
18:33
we will use nrf write register to write
18:35
the data to this register
18:39
but before that let's create a new
18:43
and let's call it nif24l01.h
18:50
here i am defining addresses for all the
18:54
and also the commands this will make it
18:57
easy for us to write the values to the
19:07
let's include that file here
19:22
i think i need to build this once
19:32
okay now it shows up so the config
19:35
register will be zero
19:45
next register is to enable the auto
19:49
this will enable the auto
19:51
acknowledgement by the receiver
19:52
after successful reception but we are
19:56
so we will write zero to this register
20:04
so let's write zero to disable auto
20:18
the next register is to enable the data
20:22
to enable any data pipe for receiving
20:25
you need to write a one in that position
20:28
again we will do this setup
20:31
when we will be setting the receiver
20:35
we are trying to do the general setup
20:37
which can be used in both the
20:39
and the receiver mode for now we will
20:43
disable all the pipes
20:58
next register is to set up the address
21:01
here we have to set how many bytes the
21:04
and rx address are going to be only the
21:07
first two bits of this register can be
21:11
we will use the 5 bytes for the address
21:14
and to do that we will write 0 cross 0
21:37
next is the setup for automatic
21:41
when the transmitter does not receives
21:43
the auto acknowledgement from the
21:45
the transmitter can re-transmit the data
21:49
here we can control how many
21:50
re-transmissions can be done
21:52
and at what interval they will be but
21:55
since we are not using auto
21:58
we will disable this entire register
22:01
so write zero to this register
22:13
next is the channel selection for the
22:17
we just have to put the channel number
22:27
for now we will write 0 here and later
22:31
when we set up the tx or rx mode
22:34
then we will set up the channel also
22:41
next register is the rf setup
22:45
here we can set the data rate and the
22:49
i am setting everything to the maximum
22:52
mbps and power of 0 decibels
22:56
take care here the 0th bit is not being
23:00
we are modifying bits 1 to 3. we need to
23:05
0 that is 0 cross zero e
23:10
so write zero cross zero e to the rf
23:27
status register can be used to check the
23:29
status of data received
23:31
or transmitted observe tx is also
23:39
here we can write the address for the
23:43
we will set them up during the mode
23:47
here you can set the length of data in
23:52
again we will set up later
24:00
fifo status can be used to check the
24:11
and we are not using dynamic payloads so
24:14
no need to check other registers
24:17
this is it then this completes the
24:24
this configuration will remain common
24:26
for both the tx mode
24:27
and the rx mode that's why we are using
24:31
a separate function here
24:33
this is it for the first part of the
24:36
next part will cover the transmit mode
24:39
and the data transmission
24:41
the link is in the description that's it
24:46
keep watching and have a nice day ahead