0:07
hello everyone today in this tutorial I
0:12
am going to show you guys how to
0:14
interface TFT display with stm32 this is
0:20
going to be a long ride so buckle up
0:23
kindly pay attention to every detail
0:25
there are a lot of settings involved I
0:28
am using HX 3 4 7g display and it is 240
0:34
by 320 TFT display as shown in the
0:38
picture before starting this tutorial I
0:41
want to mention that this is a port from
0:44
MCU friends is arduino code I have made
0:48
some changes to make it compatible with
0:50
cube MX let's start by creating the
0:54
project in cube ide i am using stm32
1:02
f103 controller the method should work
1:09
I have tested it on F 446 and F 407
1:15
controllers okay first things first
1:20
setup the RCC and serial wire debug we
1:25
need the delay in microseconds so I am
1:28
using timer 1 to do that I am assuming I
1:32
don't need to explain this part you can
1:35
check my other video if you don't know
1:38
how to use delay in microseconds
1:49
here comes the main part
1:52
all these TFTs have eight data pins and
1:56
five control pins it will be good for
1:59
you if you choose the port pins of any
2:02
one port from zero to seven to connect
2:04
them in the same order to the LCD data
2:08
pins but just to show you guys how to
2:12
program in the random cases I will
2:14
choose some of the pins as random Here I
2:18
am choosing pb0 and pv one and then
2:22
three four five six to be connected in
2:25
the same order to the LCD data pins then
2:31
pa5 to be connected to LCD d7 and P a 15
2:37
to LCD D to pin the connections are as
2:44
follows d0 d1 d2 d3 d4 d5 d6 d7 we have
2:55
to select five control pins also this is
2:59
for reset then C s then W R than RS and
3:05
finally Rd is selected as input the
3:13
final setup of the pins is as shown in
3:17
the picture next complete the rest of
3:20
the setup for the clock and once done
3:50
now we need to copy some library files
3:53
into the project folder just follow me
4:26
let's just refresh the project once next
4:35
we need to include those files in our
5:02
if you take a look at the functions dot
5:04
H file these are all the functions that
5:08
you can use for the TFT
5:24
all the modification that you are
5:27
required to do is in the user settings
5:30
dot H file I forgot to include the
5:33
functions dot H file coming back to
5:40
first of all changed the pins and ports
5:43
according to your setup most of them are
5:57
fine let's take a look at the
6:00
connections once LCD d7 is connected to
6:05
GPIO a5 I need to change that
6:23
here you define the width and height of
6:26
the LCD here you need to uncomment the
6:37
type of display you are using I am using
6:40
HX 8 3 4 7g so that one is defined
7:00
microsecond delay function is defined
7:02
here change it accordingly if you are
7:06
not using time or one here comes the
7:09
most important part of the setup
7:11
I have even written everything in the
7:14
comments you can read it here we are
7:18
going to modify the right eight function
7:20
first we need to clear all the data pins
7:24
and to do that we need to set the higher
7:28
bits of BSR our register for example to
7:34
b8 and b9 we will write one two third
7:38
fourth eighth and ninth position and
7:42
shift it left sixteen times
8:01
so in the gpio be BSS our register i am
8:06
going to write one in 0th first third
8:09
fourth fifth and sixth positions because
8:14
these are the GPIO B pins that are
8:17
connected to LC D data pins
8:31
for GPIO a only pins a two and a fifteen
8:36
are connected to the LCD data pins so
8:40
one is only written in the fifth and
8:57
next we need to write data to these pins
9:00
and to do so we will write to the lower
9:04
bits of BS SR register here I have
9:08
mentioned an example that if LCD d4 is
9:11
connected to b7 and LCD d6 is connected
9:15
to B 2 then in order to write the data
9:18
we need to select the 4th bit of data
9:21
because we are doing it for LCD d4 and
9:24
then shift that bit by 3 to make it 7
9:28
that's where the d4 is connected and
9:32
similarly to write to the LCD d6 we will
9:36
first select the 6th bit of data and
9:39
this time we will shift it to the right
9:42
by 4 that will be like subtracting and
9:46
the final result would be 2
9:49
that's where the d6 is connected in our
9:53
actual case the LCD D 2 pin is connected
9:57
to a 15 so in the GPIO a BS SR register
10:02
we need to first select the second bit
10:05
and then shift it by 13 to make a total
10:08
of 15 also LCD d7 is connected to a 5
10:36
so first we will select the seventh bit
10:39
of the data and then we need to shift
10:43
that bit to fifth location so shifted
11:06
next coming to GPIO be LCD d0 is
11:12
connected to b0 pin so we don't need to
11:15
shift it anywhere similarly other pins
11:19
are also connected in a proper order
11:21
so no shifting is required that's why I
11:25
told you to choose and connect in order
11:28
it makes things a bit easy this was the
11:33
writing data to the pins now to read the
11:37
value on the pins we need to read the
11:40
input data register again I have
11:44
mentioned an example here you can read
11:47
it basically unlike last time where we
11:51
selected the LCD pins first here we are
11:54
going to select the GPIO pins first
11:58
after selecting the GPIO pin we will
12:02
shift it to make it equal to the LCD pin
12:04
it is connected to you will get an idea
12:08
of what I am talking about in a while
12:11
let's take b0 first it is connected to
12:15
d0 so we won't shift it
12:18
same for the b1 also now here I have
12:23
selected a 15 and as it is connected to
12:26
LCD d2 I am shifting it by 13 to the
12:30
right 15 minus 13 makes up to next b3 b4
12:37
b5 and b6 are connected to the
12:40
respective pins so no shifting is
12:53
now in the end I am selecting Pinay five
12:57
and then shifting it by two in the left
12:59
to make it seven because a five is
13:03
connected to LCD D seven this completes
13:07
the important part of the setup next you
13:16
need to uncomment these lines based on
13:19
your clock frequency
13:57
let's write the main function now I am
14:01
defining a variable to store ID of the
14:13
next we need to start the timer for the
14:32
first of all read the ID of the display
14:35
and store it in the variable that we
14:38
created give some delay next initialize
14:51
the TFT with the ID then I am writing
15:29
okay print new STR prints the string in
15:34
a row its parameters are the row number
15:38
text color the font type that you can
15:40
find in the fonts dot C file size of the
15:44
text and the string itself
15:54
and in the end I am putting the display
15:58
in the continuous scrolling let's
16:11
compile the code and debug it I am
16:25
setting the live expression to monitor
16:27
the ID so I am getting correct ID here I
16:48
will continue the execution
17:18
you can see all the tests being
17:21
performed by the display
17:37
let's put this outside the while loop
17:40
and I will add a code to invert the
17:43
display correct the size of the string
17:53
also let's disable these and run the
18:35
so the things are working as expected
18:38
this is it guys these are some more
18:42
functions that you can use for now some
18:46
of them are in TFT dot H file I hope you
18:51
will get yours working too I will make
18:54
another video for the TFT displays that
18:57
use SPI this code might have some errors
19:01
at the moment I didn't test it
19:04
thoroughly yet you can download the code
19:07
from the link in the description