How Did I write the I2C-LCD Library || Explained
0 views
Jul 23, 2022
Purchase the Products shown in this video from :: https://controllerstech.store ________________________________________________________________________________________ I2C-LCD in ESP32 :::: https://youtu.be/xCVAuSobOxU I2C-LCD in STM32 :::: https://youtu.be/rfRJGfK2t-A DOWNLOAD THIS PROJECT FROM :::: https://controllerstech.com/i2c-in-esp32-esp-idf-lcd-1602/ ________________________________________________________________________________________ ******* SUPPORT US BY DONATING****** https://paypal.me/controllertech *******Join the Membership******* https://www.youtube.com/channel/UCkdqtSMnhYuMsJkyHOxiPZQ/join Join the Discord Server https://discord.gg/D32JE5wKyD Join the Telegram Group https://t.me/controllerstechdiscuss
View Video Transcript
0:01
[Music]
0:09
hello and welcome to controllers tech
0:13
as mentioned in the previous video i
0:15
have brought another video explaining
0:17
how the i2c lcd library was written
0:21
this video is for all of you whoever
0:23
wants to understand the library
0:25
irrespective of what mcu you are using
0:28
this is the i2 c lcd library source file
0:32
that we are going to discuss today
0:34
before we jump into it let's see the
0:37
connection one more time
0:39
this is how the pcf 8574 is connected
0:42
with the lcd and as you can see it only
0:45
requires 2 wires to be connected to the
0:47
mcu
0:48
here i am showing the connection with
0:50
the stm32
0:52
but it remains the same for whatever mcu
0:55
you are using
0:57
all you need to do is connect the clock
0:59
pin to the i2c clock and the data pin to
1:02
the i2c data
1:04
the vcc must be 5 volts and ground to
1:07
ground
1:08
this is how the actual pcf 8574 looks
1:12
like and as you can see this end must be
1:14
connected to the lcd vss and the other
1:17
end to the led minus pin
1:20
by the way you can control the lcd
1:23
contrast using this potentiometer here
1:26
this picture actually represents how the
1:29
p-c-f is connected internally
1:32
this is very important to understand as
1:35
our library will be written based on
1:37
this connection diagram
1:39
as you can see only the four pins p4 to
1:43
p7 are connected to the lcd data pins
1:47
the pins p0 p1 and p2 are connected to
1:51
the rs rw and enable pins respectively
1:55
although it's not shown here this pin p3
1:58
is actually connected to the lcd
2:00
backlight
2:02
now that we have seen the connection
2:04
let's check out the lcd 1602
2:07
sheet also
2:09
there are a lot of data sheets available
2:11
but things mostly remain the same across
2:14
them
2:15
first of all we will check the
2:16
initialization part
2:19
as shown here this is the pattern to
2:22
initialize the lcd in 4-bit mode
2:25
we have seen in the connection diagram
2:27
that only four pins from the pcf are
2:30
actually connected to the data pins and
2:32
therefore we need to use the 4-bit mode
2:34
for the lcd
2:36
as mentioned first we need to wait for
2:39
more than 15 milliseconds and then we
2:41
need to send this
2:44
notice here that the rs bit represents
2:46
whether we are sending a command or the
2:48
data in order to send the command this
2:52
bit must be zero and to send the data it
2:55
must be one
2:57
also the rw bit indicates whether we are
3:00
reading from the lcd or writing on it in
3:04
order to write something we must keep
3:06
this bit zero
3:08
so here it means we are writing a
3:10
command to the lcd
3:13
basically the entire initialization
3:15
sequence will be a set of commands and
3:17
that's why the rs and rw bits will
3:20
always be zero
3:22
now we are only provided with the bits
3:24
d4 to d7 so we will keep the lower four
3:27
bits as zeros
3:30
this will make the command as zero cross
3:32
three zero
3:34
then we need to wait for at least four
3:36
point one milliseconds and send the same
3:38
command again
3:40
then wait for one hundred micro seconds
3:43
and send the same command again
3:51
here you can see in the library i have
3:54
performed the mentioned operations in a
3:56
similar way
4:05
now we will send another command that is
4:07
zero cross two zero and it will set the
4:10
interface to four bit length
4:18
we are using the function lcd send
4:20
command to send these commands to the
4:23
display
4:24
let's take a look at this function first
4:27
as the name suggests the function can be
4:30
used to send the commands to the lcd
4:33
it takes the 8-bit command as the
4:36
argument
4:37
but since we are using the lcd in 4-bit
4:39
mode we have to send this 8-bit command
4:42
into two parts of 4-bit each
4:45
so our first step would be to separate
4:47
the upper half and lower half of this
4:49
8-bit command
4:51
we will perform the and operation with
4:53
the 0 cross f0 and store this most
4:56
significant half in the data u variable
4:59
similarly we will shift the command by 4
5:02
bits to the left position and then
5:04
perform the and operation with the 0
5:06
cross f 0 and store this least
5:08
significant half in the data l variable
5:12
now we need to add some other
5:14
information to this command which will
5:16
include the rs bit the rw bit and the
5:19
backlight pin data
5:21
we will start preparing the final
5:23
commands and we will start by sending
5:25
the most significant half first
5:28
also remember that in order to send a
5:30
command or data to the display we need
5:33
to provide something called the strobe
5:37
so after we send the data or the command
5:40
we pull the enable pin high then pull it
5:42
low
5:44
this strobe is a kind of signal to the
5:46
lcd that we have set the respective data
5:49
and now it can process it
5:51
here in this library i am sending the
5:54
strobe along with the command itself
5:57
so basically i am going to send the same
5:59
command twice once with the enable pin
6:02
high and the again with the enable pin
6:05
low this means our p zero will be low to
6:08
indicate that this is a command the p 1
6:10
will be low to indicate that we are
6:12
performing a right operation and p 2
6:15
will be high to indicate that the enable
6:17
bit is high and p 3 will be high so that
6:20
the backlight remains on
6:22
this will make the commander 0 cross 0c
6:25
and we will add it with our original
6:27
command and store it in the first
6:29
position
6:31
we will send the same command again but
6:33
this time the enable pin will be low
6:37
so the first two bytes we write to the
6:39
lcd will have the command along with the
6:41
strobe
6:43
similarly we will store the least
6:45
significant half in the other two bytes
6:47
of the array
6:49
finally we are ready to send this array
6:51
of 4 bytes to the lcd
6:54
now we can use the respective i2c
6:56
function according to what controller
6:59
you are using and send these 4 bytes to
7:01
the lcd
7:03
so basically we need the 4 bytes to send
7:06
a single byte command
7:09
similarly to send the data to the
7:11
display we use this function lcd send
7:14
data it also takes the data byte as the
7:18
argument and then we edit that data
7:20
before sending 4 bytes
7:23
the only change we need to make is use
7:25
the is bit as 1 to indicate that we are
7:28
sending data this time
7:30
other than that everything is similar to
7:32
what we did while sending the command
7:37
so now that we know how to send data and
7:39
commands to the display let's continue
7:42
with our initialization function
7:45
we have reached up to this part where we
7:47
set the display into the 4-bit mode
7:50
the next few commands will be based on
7:52
what kind of setup you want with the
7:54
display
7:55
notice here that now we have all the 8
7:58
bytes for each command and they all will
8:00
be used in one way or another
8:03
here we have the function set command
8:05
and to understand it we need to see the
8:08
instruction set in detail
8:10
here the function set command and it
8:13
sets the display mode the number of rows
8:16
and the font size
8:18
dl controls the display mode 1 means
8:21
8-bit mode and 0 means 4-bit mode
8:25
n controls the number of rows one means
8:28
two rows and zero means only one row
8:32
and the f controls the font size you
8:34
want to use
8:36
i have already commented out everything
8:38
i used in the library and you are free
8:40
to modify these parameters according to
8:42
your convenience
8:44
the next command is the display switch
8:47
command
8:48
though it's not mentioned here you can
8:50
check it by the position of the one in
8:52
the command this command controls the
8:55
display and the cursor
8:57
we have to first turn off the display as
9:00
mentioned in the initialization sequence
9:06
the next command is to clear the display
9:09
here it is mentioned in the first
9:11
position
9:12
note that all these commands take some
9:15
time to execute and therefore we must
9:17
provide a small delay after each command
9:25
the next command is to set the entry
9:27
mode
9:29
here we can control the movement of the
9:31
cursor along with the shift in the
9:33
display
9:40
i have set the cursor to increment and
9:43
the display has no shift
9:45
if you turn on the display shift you can
9:48
control the shift direction by using
9:50
this command later in the code
9:53
finally we will turn on the display by
9:55
using the same command again
9:58
you can control the cursor and it's
10:00
blinking also
10:02
the pdf i am using doesn't seem to be
10:04
much detailed so i would advise that you
10:07
check a few other data sheets also
10:09
this entire sequence will initialize our
10:12
lcd
10:15
now we can use the lcd data send
10:18
function to send one byte of data to the
10:20
display
10:21
remember that you can only send data in
10:24
the ascii format so you can send a
10:26
single character using this function
10:29
for example lcd sent data a or p or
10:34
three
10:35
we have another function here which can
10:38
send the entire string to the display
10:41
it will simply call the send data
10:43
function as long as all the characters
10:45
from the string are printed
10:47
now let's talk about the lcd put cursor
10:49
function
10:51
this function sets the cursor at a
10:53
specific location on the display
10:56
so the lcd have ddram addresses and only
10:59
certain addresses are available to print
11:01
these characters
11:03
i have covered this in the tutorial i
11:05
wrote for the esp32
11:10
here are the ddram addresses available
11:12
for the lcd 1602
11:16
the top row address starts from 0 cross
11:18
8 0 and we can display 16 characters in
11:21
this
11:22
the bottom row starts from 0 cross c 0
11:26
and we can display another 16 characters
11:28
in it
11:29
so this function takes the row and
11:31
column as the parameter
11:33
and let's say if we want to print at the
11:36
zeroth row and fifth column
11:38
since the zeroth row starts from zero
11:41
cross 8 0 it will add the column to this
11:43
value
11:45
we will get the address 0 cross 8 5 and
11:47
we will send this as a command to the
11:49
lcd indicating that we want to print at
11:52
this address
11:53
now if you send some data it will
11:56
display at this particular location
11:59
similarly if you give the input as first
12:01
row and ninth column it will add the
12:03
column that is nine to zero cross c zero
12:07
this will result in the address zero
12:10
cross c nine
12:12
and then we can print at this particular
12:14
location by sending the data next time
12:17
these are all the functions available in
12:19
the library and i hope you understood
12:22
how i wrote them now let's also talk
12:25
about the slave address of the pcf 8574
12:28
device we are using
12:30
you can see the address is defined here
12:32
and i will explain why i am using this
12:35
address
12:36
let's check out the data sheet of the
12:38
pcf-8574
12:41
here are the addresses defined for
12:43
pcf-8574
12:45
or pcf-8574-a
12:48
i have the
12:50
pcf-8574 so i will explain its address
12:53
the four most significant bits are 0 1 0
12:56
0 and then the next 3 bits are a 0 a 1
13:01
and a 2.
13:02
these 3 bits are responsible for
13:05
controlling the address of the device
13:07
here are the pins a0 a1 and a2 and the
13:12
pins next to them are ground
13:14
right now the pins are high and if we
13:17
solder them to ground the respective
13:19
pins goes low
13:21
the next bit we need to consider is the
13:23
least significant bit which is the rw
13:26
bit
13:27
it is used along with the address to
13:29
indicate whether we are performing the
13:31
read or write to the device so by
13:34
default the address is 0 cross 4 e if we
13:37
consider the read write bit and 0 cross
13:40
2 7 if the read write bit is not taken
13:43
into account
13:45
depending on whether your m c u supports
13:47
7-bit address or 8-bit address you can
13:50
define it accordingly
13:53
so this is it for the video
13:55
i hope you understood how the library
13:57
was written
13:59
the usage of the library has already
14:02
been explained in the previous videos
14:05
anyway i will leave the links to those
14:07
videos in the description below
14:10
leave comments in case of any doubt
14:13
that's it for
14:14
today keep watching and have a nice day
14:18
ahead
14:20
[Music]
14:40
you
#Programming
#Software