Interfacing PIC18F46K22 with ILI9341 TFT – Graphics Test Example

This post shows how to interface Microchip PIC18F46K22 8-bit microcontroller with ILI9341 TFT display.
The ILI9341 TFT module contains a display controller with the same name: ILI9341. It’s a color display that uses SPI interface protocol and requires 4 or 5 control pins, it’s low cost and easy to use. The resolution of this TFT display is 240 x 320 which means it has total of 76800 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).
CCS C Compiler is used in this project.

TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.

Project Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • ILI9341 TFT display module (2.2″, 2.4″, 2.8″ …)
  • 5 x 3.3k ohm resistor
  • 5 x 2.2k ohm resistor
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 with ILI9341 color TFT display 240x320 pixel

Interfacing PIC18F46K22 MCU with ILI9341 TFT display circuit:
Hardware circuit diagram of the example is shown below.

The ILI9341 TFT display board which is shown in project circuit diagram has 14 pins, the first 9 pins are for the display and the other 5 pins are for the touch module.

So, the display side pins are numbered from 1 to 9 (from left to right): VCC (5V), GND (ground), CS (chip select), RST (reset), DC (or D/C: data/command), MOSI (or SDI), SCK (clock), BL (back light LED) and MISO (or SDO).
MOSI: master-out slave-in.
SDI: serial data in.
MISO: master-in slave-out.
SDO: serial data out.

PIC18F46K22 ILI9341 TFT circuit

All the grounded terminals are connected together.

As mentioned above, the ILI9341 TFT display controller works with 3.3V only (power supply and control lines). The display module is supplied with 5V where GND pin is connected to circuit ground, VCC and BL pins are connected to circuit +5V. This module has a built-in 3.3V regulator which supplies the display controller with 3.3V from the 5V source.

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).

All PIC18F46K22 MCU output pins are 5V, connecting a 5V pin directly to the ILI9341 display board may damage its controller circuit. To avoid that, I used voltage divider for each line which means there are 5 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.

So, the ILI9341 TFT display is connected to the PIC18F46K22 MCU as follows (each one through voltage divider):
CS pin is connected to pin RD0 (#19),
RST pin is connected to pin RD1 (#20),
D/C pin is connected to pin RD2 (#21),
MOSI pin is connected to pin RC5 (#24),
SCK pin is connected to pin RC3 (#18).

In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 64 MHz, MCLR pin is configured as an input pin.

Interfacing PIC18F46K22 MCU with ILI9341 TFT display 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 ILI9341 TFT display, its full name (with extension) is ILI9341.c, download link is below:
ILI9341 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 ILI9341 TFT is connected to PIC18F46K22 microcontroller SPI1 module pins (SCK1 and SDO1) which is initialized with the following line:

The other pins: RST, CS and DC are defined as shown below:

Rest of code is described through comments.

Full CCS C code:

The video below shows my breadboard test circuit:

Proteus simulation:
We can simulate this project with Proteus ISIS software as shown in the following video (not perfect result as the hardware circuit).
Note that Proteus simulation circuit is not the same as real hardware circuit, project hardware circuit diagram is shown above.

Proteus simulation file download link is below, use version 8.6 or higher to open it:
PIC18F46K22 and ILI9341 TFT Proteus simulation

3 thoughts on “Interfacing PIC18F46K22 with ILI9341 TFT – Graphics Test Example”

  1. Hi
    I am trying to port your program to HI-Tech c but have an error ) missing in the last line.
    void startWrite(void);
    void endWrite(void);
    void writeCommand(uint8_t cmd);
    void setAddrWindow(uint16_t x1, uint16_t y1, uint16_t w, uint16_t h)
    uint8_t tft_readcommand8(uint8_t reg, uint8_t index=0);
    Could please help me solve the problem?

  2. hi. need info.
    my CCS C compiler does not include the PIC18F46K22 in the device.dat,
    and my PicKit 2 have same problem
    What shall I do ?
    What hardware og software do you uses ?
    I dont have an URL.

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