0:09
hello and welcome to controllers Tech
0:12
this is the eth video in the AVR series
0:15
using the explained mini development
0:17
board and today we will again continue
0:19
with the i2c Master Series in the
0:22
previous video we saw how to connect the
0:25
LCD 1602 via the i2c and print some data
0:31
so far we have only been sending the
0:33
data to the i2c slave so today for a
0:36
change we will receive the data from the
0:39
device I am going to use the at24 c256 e
0:44
prom as the slave device in today's
0:46
tutorial we will first store some data
0:49
into the eom memory and then read the
0:52
data from it if everything goes well we
0:56
should get the same data from the
0:58
eom please note that this is not a
1:01
tutorial on interfacing the eom I am
1:04
just using it so that I can test the
1:06
reading data I will cover the full eom
1:09
interfacing in some other video although
1:13
we still need to see the data sheet so
1:15
that we can understand a little bit
1:18
device I am going to continue with the
1:21
i2c master code we have been working on
1:23
in previous videos and I have removed
1:25
the LCD related information from this
1:28
code let's take a look at the data sheet
1:32
we have the clock and data lines as
1:34
usual and there are three address inputs
1:37
which can be used to modify the address
1:40
device this is the same as what we saw
1:46
device the memory is organized into 512
1:50
pages of 64 bytes each and the addresses
1:53
to these locations are 15 bit in size
1:57
the maximum clock frequency is limited
1:59
to 1,000 khz so that is fine as we are
2:04
frequency here you can see the device
2:07
addressing let's focus on the seven bit
2:11
address if all the address input bits
2:13
are zero then the seven bit address of
2:17
hex let's keep that in mind as we would
2:22
programming here is how we can write the
2:24
data to the device to write a single
2:27
bite the master needs to send the start
2:30
condition followed by the device address
2:34
command then it needs to send the two
2:36
address bytes which denotes the memory
2:38
address of the device where the master
2:40
wants to write the data then the master
2:43
sends the data bite and finally the stop
2:47
condition to write the multiple byes the
2:50
same logic is used but instead of a
2:52
single data bite the master can keep
2:55
sending the data we will see the read
2:58
operation later let let's first write
3:00
the data to the slave
3:02
device here I am defining the slave
3:05
address for the eom that is 50 hex now
3:09
let's assume that we want to store the
3:11
string hello world to the eom
3:14
memory let's define two more arrays one
3:17
which we will send to the device and
3:19
another where the data received from the
3:23
stored inside the main function after
3:26
initializing the clock we will
3:30
and give the delay of 100
3:32
milliseconds now let's prepare the data
3:36
eom according to the right format the
3:39
master needs to First send the 2 byte
3:41
memory location where the data will be
3:44
stored I am just setting a random memory
3:47
location here just remember that we need
3:49
two bytes for the same now we will copy
3:53
the rest of the data to the right array
3:56
let's use the mem copy function to do so
3:59
in include the string header file for
4:01
the mem copy function to work now write
4:04
the data by calling the function i2c WR
4:08
the total bytes we are writing is 13 now
4:11
which contains two address bytes and 11
4:13
data bytes let's give a delay of 1
4:16
second before performing the read from
4:22
memory now comes the read operation we
4:25
will go with the random read as we will
4:28
read from a random memory location here
4:31
the diagram shows how the master should
4:34
perform the read from the memory the
4:36
master will first send the device
4:38
address with the right bit then it will
4:41
send the 2 byte memory address where it
4:44
wants to read the data from the master
4:47
will then send the start condition again
4:49
it is actually called the repeated start
4:52
then the master will send the slave
4:54
address with the read bit the slave will
4:57
then start sending the data to the bus
5:00
the slave continues to send data as long
5:03
as the master sends the acknowledged
5:05
response after receiving the required
5:08
data the master will send the stop let's
5:12
write a new function to read the data
5:14
from the slave the function will contain
5:17
a lot of code from the right function so
5:19
let's copy it the parameter of this
5:22
function will be the same as the right
5:24
function in addition to it we will add
5:26
the 16bit memory address this is the
5:30
address where the master wants to read
5:32
the data from our initial part of the
5:35
function will remain the same where the
5:37
master sends the slave address with the
5:39
right bit if the slave sends the
5:42
acknowledgement for the address the
5:44
master continues to write the data the
5:47
master only needs to write two bytes for
5:49
the memory address let's define an array
5:53
of two bytes to store the memory address
5:56
store the lower address bite first and
5:59
then store the higher bite now the
6:01
master will send the address bites to
6:09
slave we don't need this part when the
6:12
address bytes have been transferred the
6:15
master will issue the repeated start
6:17
condition it can be sent by modifying
6:25
register we need to modify the command
6:27
bit to send the repeated start condition
6:30
let's send the repeated start condition
6:33
after the master has sent the memory
6:34
address bites if the slave does not
6:37
acknowledge the address the master will
6:40
send the stop condition the master will
6:43
now send the device address again but
6:45
with the read bit to send the address
6:48
with a read bit we will shift the
6:50
address to the left by one place and
6:52
then add a one to the least significant
7:04
now if the slave acknowledges the
7:06
address the master will start receiving
7:08
the data the index variable will track
7:11
how many data bytes have been received
7:14
the reception will continue as long as
7:17
the number of received bites are less
7:19
than the data size if you check the
7:22
status register the read interrupt flag
7:24
is set when the host bike tread
7:26
operation is complete so we will wait
7:29
for this bit to set which will imply
7:31
that the data has been received in the
7:33
data register once the bit is set we
7:37
will read the data from the data
7:39
register and store it into the
7:44
buffer when the required number of bytes
7:47
have been received the the master will
7:49
send the Knack response along with the
7:53
condition to send the Knack response we
7:56
just need to set the acknowledge action
7:57
bit to one and when the stop condition
8:00
is sent by the master The Knack response
8:03
automatically if the index is not equal
8:06
to size parameter which means that there
8:08
is still data left to be received the
8:11
master will send the acknowledgement for
8:13
each received bite to send the
8:16
acknowledgement we will again modify the
8:18
command bits in the control B
8:20
register the acknowledge action bit is
8:23
set to Zero by default so when the
8:25
receive transmit command is sent the
8:27
master sends the acknowledge M
8:30
response if the slave device does not
8:32
acknowledge the read address the master
8:35
will issue the stop condition let's also
8:38
return the success after all the datab
8:41
bytes have been received our read
8:44
function is ready let's build the code
8:48
errors there are no errors so let's
8:51
continue with the main
8:53
function now we will read the data from
8:55
the same address that we wrote the data
8:58
into the data will be stored in the eom
9:06
buffer we also need to Define this
9:08
function in the i2c master header file
9:12
let's build the code now we will also
9:15
see the output on the logic analyzer so
9:18
that we can analyze the entire right and
9:21
frame let's set the breako at the right
9:24
function and debug the
9:32
we have hit the break point add the eom
9:36
read buffer to the watch so that we can
9:38
see the data stored in
9:42
it let's step over the right
9:47
function I should better add the break
9:55
function all right we have hit the break
9:57
point again right now now the read
9:59
buffer does not have any data in it
10:02
let's step over the read function you
10:05
can see we have received the data hello
10:09
world this is the same data that we
10:11
stored in the memory there are two extra
10:14
characters and that is because I read 13
10:17
bytes instead of 11 now let's see the
10:27
analyzer here you can see the right and
10:29
read sections are separated by one
10:32
second let's check the right section
10:35
first the master sends the start
10:38
condition followed by the slave address
10:40
50 hex with the right bit the slave
10:43
acknowledges the address the master then
10:46
sends the memory address bytes 30 and Z
10:51
after receiving the acknowledgement the
10:53
master sends the data hello world it
10:57
receives the acknowledgement after every
11:00
in the end the master sends the stop
11:02
condition the orange dot now let's check
11:05
the read section the master sends the
11:08
start condition followed by the slave
11:11
address 50 hex with the right bit the
11:14
slave acknowledges the address the
11:17
master then sends the memory address
11:19
bytes 30 and Zer then the master sends
11:23
the start condition again followed by
11:25
the slave address 50 hex with the read
11:27
bit the slave acknowledges the address
11:31
and starts transmitting the data the
11:34
master sends the acknowledgement after
11:36
receiving each data bite after receiving
11:39
the 13th bite the master sends The Knack
11:42
response followed by the stop condition
11:45
just to demonstrate that we are
11:47
receiving the data from the slave memory
11:50
let's read the data from another memory
11:52
location here I am going to read the
11:55
data from location 20 although we stored
11:58
the data in Lo ation
12:04
again we have hit the break point and
12:06
the read buffer is empty right now here
12:09
you can see the data received is not the
12:12
same as what we stored actually the
12:14
memory is empty at this location and
12:17
hence we are receiving the FF hex let's
12:20
store something else to the location and
12:39
we have received the same data as what
12:41
we stored in the location so the re
12:45
function is working fine we are able to
12:48
write the data into the slave device and
12:50
read the data from a particular memory
12:52
location in the slave device note that
12:56
this code works for the devices whose
12:58
memory address is 2 by in size if you
13:01
have a device whose memory location is
13:03
only 1 byte long you need to modify it
13:07
or I will update the code so that it
13:09
works with all the memory sizes this is
13:12
it for the i2c Master Series for now we
13:16
covered the initialization writing and
13:18
reading data from different slave
13:21
devices we will cover a few sensors
13:23
based on the i2c protocol later in the
13:26
series in the next future tutorials on
13:29
the AVR series we will cover the SBI
13:33
protocol this is it for today you can
13:36
download the code from the link in the
13:39
description leave comments in case of
13:41
any doubt keep watching and have a nice