Interfacing PIC MCU with ST7789 TFT Display | mikroC Projects

This tutorial shows how to interface Microchip PIC18F46K22 8-bit microcontroller with ST7789 TFT display.
The ST7789 TFT is a color display that uses SPI protocol. This display is an IPS display, it comes in different sizes (1.3″, 1.54″ …) but all of them should have the same resolution of 240×240 pixel.
This module works with 3.3V only, connecting it directly to a 5V system will not work and may damage its controller circuit!

The compiler used in this project is mikroElektronika mikroC PRO for PIC.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.

The following image shows a ST7789 display module provided by Adafruit Industries:

Adafruit ST7789 TFT display module
Adafruit ST7789 TFT display module

Another version of the ST7789 display module is shown below. This one has no CS (chip select) pin, its internally attached to GND:

ST7789 TFT display without CS pin

Project Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • ST7789 TFT display module (1.3″, 1.54″ …)
  • AMS1117 3V3 voltage regulator
  • 10 uF capacitor
  • 100 nF ceramic capacitor
  • 4 x 3.3k ohm resistor (+1 if the display module has CS pin)
  • 4 x 2.2k ohm resistor (+1 if the display module has CS pin)
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 with ST7789 SPI color TFT display

Interfacing PIC18F46K22 MCU with ST7789 TFT circuit:
Project circuit schematic diagram is shown below.

The ST7789 display module shown in project circuit diagram has 7 pins: (from right to left): GND (ground), VCC, SCL (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and BLK (back light).
Connecting the BLK pin is optional. The back light of the display turns off when the BLK pin is connected to the ground (GND).

PIC18F46K22 ST7789 TFT display circuit

All the grounded terminals are connected together.

The ST7789 TFT display works with 3.3V only (power supply and control lines). The display module is supplied with 3.3V that comes from the AMS1117 3V3 voltage regulator, this regulator steps down the 5V into 3.3V (supplies the display controller with regulated 3V3).

All PIC18F46K22 microcontroller output pins are 5V, connecting a 5V pin directly to the ST7789 TFT display may damage its controller circuit!
To connect the PIC18F46K22 with the display module, I used voltage divider for each line. This means there are 4 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.

If the display module has a CS pin (Chip Select) then it should be connected to the PIC18F46K22 microcontroller through another voltage divider (for example connecting it to pin RD2).

The PIC18F46K22 microcontroller has 2 hardware SPI modules (MSSP1 and MSSP2 modules).
In this project SPI1 module is used with SCK1 on pin RC3 (#18) and SDO1 (MOSI) on pin RC5 (#24). SCK1 and SDO1 pins of the PIC18F46K22 MCU are respectively connected to SCL and SDA pins of the ST7789 display module.

So, the ST7789 TFT display is connected to the PIC18F46K22 MCU as follows (each one through voltage divider):
RST pin is connected to pin RD0 (#19),
D/C pin is connected to pin RD1 (#21),
SCL pin is connected to pin RC3 (#18),
SDA pin is connected to pin RC5 (#24).

In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 64 MHz (16 MIPS), MCLR pin is configured as an input pin.
With clock frequency of 64MHz we get a maximum data transfer rate of 16 Mbps.

Interfacing PIC18F46K22 MCU with ST7789 TFT C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.6.0.

To be able to compile project C code with no error, 2 libraries are required:
The first library is a driver for the ST7789 TFT display, its full name (with extension) is ST7789.c, download link is below:
ST7789 TFT display library for mikroC compiler

The second library is graphics library, its full name is GFX_Library.c, download link is the one below:
Graphics library for mikroC compiler

after the download of the 2 library files, add both of them to the project folder.

The default connection setting of the mikroC ST7789 TFT library is hardware SPI1 module (SPI1 module must be initialized before initiating the display). Instead of hardware SPI1 module, software SPI or hardware SPI2 module can be used.

If TFT data pin (TFT_DIN) and clock pin (TFT_SCK) are defined in the main code (before #include “ST7789.c”) then the library will automatically use software SPI.
example (respectively, TFT_DIN and TFT_SCK are connected to RD3 and RD4):
#define TFT_DIN RD3_bit
#define TFT_SCK RD4_bit

If the line below is defined then the library will use hardware SPI2 module:
#define ST7789_HARD_SPI2

Hints:
The 2 library files are included in the main code as shown below:

As mentioned above, the ST7789 TFT is connected to PIC18F46K22 microcontroller SPI1 module pins (SCK1 and SDO1) which is initialized as shown below.
Note that if the display doesn’t work uncomment the second SPI1 initializing line and the first one should be commented (or removed).

The other pins: RST and DC are defined as shown below.
If the display module has a CS pin uncomment its related lines (#define TFT_CS and #define TFT_CS_DIR) and connect it to RD2 pin of the microcontroller through voltage divider.

Rest of code is described through comments.

Full mikroC code:

The video below shows my breadboard test circuit:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

10 thoughts on “Interfacing PIC MCU with ST7789 TFT Display | mikroC Projects”

  1. Using Mikroc and the PIC18F46K42, this project works perfectly but timer 1 stops running when tft_init(); is called. This chip uses the vectored interrupts. Would there be any reason for this? My project is on hold until i can sort this so any help would be appricated.

  2. Thanks to your effort, I have an 80 * 160 0.96 inch model. I can also use this library on it. but half of the screen works in an invisible spot. I hope I could explain what I mean. How can I set the screen resolution and rotation.
    If you have any information on this subject, can you help?

  3. Thanks for the reply but I had the two files in the project folder. I’ve managed to fix the issue of the missing routines by declaring the missing routines as “extern” (I believe it was the main.c) . Issue went away but then a million other compiling issues arose. I’ve followed all your instructions letter by letter and even made sure that my configurations bits matched your notes.
    Either way, Ive managed to port all three files into one (for now) and now all the errors are gone and I can compile the code. I haven’t yet gotten the hardware to test it.
    I would recommend you please review your instructions and see if the libraries are ok.
    Thank you

    1. The code _as it is_ should work properly and without any problem, it was tested with PIC18F46K22 and PIC18F4550 microcontrollers, compiler version is always 7.6.0
      Try with this:
      In the Library Manager uncheck all the built-in TFT libraries.

  4. Hello.
    Thanks for posting all this code.
    When I compile, I get told that there are a number of undefined identifiers.
    drawPixel, drawVLine, drawHline. I can’t find these subroutines anywhere. Any advice?
    Thank you.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top