PIC18F46K22 Interface with ST7789 TFT – Graphics Test Example

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!
CCS C Compiler is used in this project.
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
  • PIC Microcontroller programmer (PICkit 3, PICkit 4…)

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 turns off when the BLK pin 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 to the display module, I used voltage divider for each line. That 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 CCS C compiler, it was tested with versions 5.051 and 5.083.

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 CCS C compiler

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

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

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 with the following line:
Note that if the display module has a CS pin try with MODE = 0.

The other pins: RST and DC are defined as shown below:
If the display module has a CS pin uncomment the last line and connect it to RD2 pin of the microcontroller through voltage divider.

Rest of code is described through comments.

Full CCS C code:

Interfacing PIC18F46K22 MCU with ST7789 TFT Display Video:
The video below shows my DIY breadboard test circuit of PIC18F46K22 microcontroller and ST7789 display module:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

3 thoughts on “PIC18F46K22 Interface with ST7789 TFT – Graphics Test Example”

  1. Hello,
    Thanks for the code.

    I have errors saying that there are undefined expression such as : drawVLine, drawPixel, fillRect in the “GFX_Library.c” and almost all the “ST7789_SPI_XFER” in the “ST7789.c” file. I have included the files in the project file and included them with the lines.

    #include // include ST7789 display driver
    #include // include graphics library

    I am using CCS compiler with MPLAB X.

    Can someone help me please ?

    Thank you.

    1. I just ran the code, and it works fine.

      Request to the website owner.

      I have spent 2 days trying to fix the problem.. on the site I bought the display from, one user says

      I have a ST7789 240×280 display., your code works nearly fine, except the extra 40 lines are not being cleared or written to.
      This is a beautiful little display! It works great. They are set up for SPI mode 0. The only weird thing is that if you are using the Adafruit GFX ST7789 library, you will need to set the width to 240, the height to 280, and you will need to manually set the _rowstart to 20 or else the first 20 rows will not be visible, and the last 20 rows will not be addressable. The colors are excellent for an inexpensive LCD. The viewing angle is great since the display uses IPS technology. The displays do have rounded corners. The pixels in the corners do not exist. The picture attached draws a square box 10 pixels wide around the edges of the display. You can see the effects of the rounded display corners.

      can you help ?

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