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

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:

The video below shows my breadboard test circuit:

Leave a Reply

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