Interface TFT display with STM32

Today, in this tutorial, we will see how to interface a TFT display with STM32. This tutorial will only cover the parallel connection today.
I am using STM32CUBEIDE and STM32F103C8 microcontroller for this purpose.

Before I start, I want to mention that I did not write this code. This is a PORT from the mcufriend’s arduino code, which can be found HERE. I merely made some changes, so that it can be used with the CubeMx with a little modifications.

SETUP

I will start by creating a project in the CubeMx (or in the CUBEIDE). Here, we need to setup the TIMER to create a delay in microseconds. Below is my setup for the same.

Now comes the part for setting up the Pins for the display. As, we are using the parallel connection, there are 8 DATA Pins and 5 CONTROL Pins. It would be really easy, if you connect all the data pins to the same PORT and in the same order.
For eg -> LCD_D0 -> PB0, LCD_D1 -> PB1……………… LCD_D7->PB7.

But just to show you guys, how to program in random selection of pins, I am choosing some of the Pins in random.The pins selected are as follows:

Once the code is generated, we need to copy the following files into our project:
user_setting.h
fonts.h
functions.h
tft.h
fonts.c
tft.c

These files are in the code.zip, you can download at the end of the post.


Next, just open the user_setting.h file. This is where we need to modify the code according to our setup. According to my setup the defines are as follows

#define RD_PORT GPIOA
#define RD_PIN  GPIO_PIN_4
#define WR_PORT GPIOA
#define WR_PIN  GPIO_PIN_3
#define CD_PORT GPIOA          // RS PORT
#define CD_PIN  GPIO_PIN_2     // RS PIN
#define CS_PORT GPIOA
#define CS_PIN  GPIO_PIN_1
#define RESET_PORT GPIOA
#define RESET_PIN  GPIO_PIN_0

#define D0_PORT GPIOB
#define D0_PIN GPIO_PIN_0
#define D1_PORT GPIOB
#define D1_PIN GPIO_PIN_1
#define D2_PORT GPIOA
#define D2_PIN GPIO_PIN_15
#define D3_PORT GPIOB
#define D3_PIN GPIO_PIN_3
#define D4_PORT GPIOB
#define D4_PIN GPIO_PIN_4
#define D5_PORT GPIOB
#define D5_PIN GPIO_PIN_5
#define D6_PORT GPIOB
#define D6_PIN GPIO_PIN_6
#define D7_PORT GPIOA
#define D7_PIN GPIO_PIN_5

You can change them, however you have defined the pins for the LCD connection.


After defining the respective pins, we need to modify the TIMER function to create the delay in microseconds. The default setup is shown below. You can change the Timer handler, if you are not using TIMER1.

extern TIM_HandleTypeDef htim1;
void delay (uint32_t time)
{
	/* change your code here for the delay in microseconds */
	__HAL_TIM_SET_COUNTER(&htim1, 0);
	while ((__HAL_TIM_GET_COUNTER(&htim1))<time);
}

Now comes the most important part of the setup. That is, customising the “write_8” and “read_8” functions. Here you need to pay attention to whatever I am going to explain.

The write_8 function is defined below

  #define write_8(d) { \
   GPIOA->BSRR = 0b1000000000100000 << 16; \
   GPIOB->BSRR = 0b0000000001111011 << 16; \
   GPIOA->BSRR = (((d) & (1<<2)) << 13) \
               | (((d) & (1<<7)) >> 2); \
   GPIOB->BSRR = (((d) & (1<<0)) << 0) \
               | (((d) & (1<<1)) << 0) \
			   | (((d) & (1<<3)) << 0) \
			   | (((d) & (1<<4)) << 0) \
			   | (((d) & (1<<5)) << 0) \
			   | (((d) & (1<<6)) << 0); \
    }
  • This consists of 2 parts. First, we have to clear all the Pins, that are connected to the TFT as DATA Pins.
  • To clear the Pins, we have to write a HIGH (1) to the higher bits of the respective BSSR register.
  • As, in my setup, the pins PA5 and PA15 are connected to the LCD D7, and LCD D2. So in GPIOA->BSRR register, only the 5th and the 15th bits are HIGH.
  • The PB0, PB1, PB3 – PB6 are also connected to the data pins. And that’s why, in the GPIOB->BSRR register, the bits 0,1,3,4,5,6 are set to HIGH.
  • After Setting the Respective bits, we will shift them by 16 places, so that they get cleared.
  • After the pins are cleared, we have to write the data to the LCD. And to set a pin, we must set the lower bits of BSSR register as HIGH.
  • According to the Setup, the LCD_D2 is connected to the PA15. So if I want to write the DATA to the LCD_D2 pin, first I will select the 2nd bit of the data (d & (1<<2)), and than shift this by 13 using <<13. This will be like adding 2 with 13 to make a total of 15, and that’s where the LCD_D2 is connected to.
  • Similarly, LCD_D7 is connected to PA5. So to write the data, first we will select the 7th bit of the data (d & (1<<7)), and this time shift it RIGHT by 2 (>>2). This is like subtracting 7-2=5. And that’s where, the D7 is connected to.
  • The rest of the LCD Pins are connected to the respective Pins of GPIOB, and that’s why we don’t need to shift anything there.

The next part is reading the Pins for the data from the LCD. And we do that by reading the IDR (Input Data Register) of the Respective PORT.

The read_8 is defined below

  #define read_8() (          (((GPIOB->IDR & (1<<0)) >> 0) \
                           | ((GPIOB->IDR & (1<<1)) >> 0) \
                           | ((GPIOA->IDR & (1<<15)) >> 13) \
                           | ((GPIOB->IDR & (1<<3)) >> 0) \
                           | ((GPIOB->IDR & (1<<4)) >> 0) \
                           | ((GPIOB->IDR & (1<<5)) >> 0) \
                           | ((GPIOB->IDR & (1<<6)) >> 0) \
                           | ((GPIOA->IDR & (1<<5)) << 2)))

The process here remains the same. Except, we have to first select the GPIO Pin, and than shift it according to the position of the LCD Pin, that it is connected to. In the function above, we are first selecting the PB0 pin, and as it is connected to LCD_D0, we don’t need to shift it anywhere. Same for the PB1 also.
Next, we are selecting PA15, and as this one is connected to the LCD_D2, we need to shift it by 13 to the right ( >>13). This process continues for all other pins too.

After all the Pins work is done, we still need to select the delays according to our clock frequency. As I am using STM32F103C8 at 72 MHz, I am going to uncomment the respective code as shown below.

/************************** For 72 MHZ ****************************/
#define WRITE_DELAY { }
#define READ_DELAY  { RD_ACTIVE;  }

You still need to uncomment the support for the TFT type that you are using. I am using HX8347
#define SUPPORT_8347D



Some Insight into the CODE

ID = readID();

HAL_Delay(100);

tft_init (ID);
  • readID( ) reads the ID of the display, and store it in the variable ID.
  • tft_init (ID) initialises the display with the ID that we read.

fillScreen(BLACK);

fills the entire screen with BLACK colour. It’s more like clearing the display, if you like the BLACK background.


testFillScreen();
testLines(CYAN);
testFastLines(RED, BLUE);
testFilledCircles(10, MAGENTA);
testCircles(10, WHITE);

Above are some tests, that LCD is going to perform.


printnewtstr(100, RED, &mono12x7bold, 1, "HELLO WORLD");

scrollup(100);

printnewstr prints the string in the given ROW. We can customise the text colour, fonts, size of the string.
scrollup is going to scroll the contents of the display in vertically upward direction.



RESULT

Here is the Picture from the video

Check out the Video Below




Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Subscribe
Notify of

42 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×