PIC MCU with ILI9341 TFT – Graphics Test Example | mikroC Projects

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 pixel which means it has total of 76800 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).

The compiler used in this project is mikroElektronika mikroC PRO for PIC.
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 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 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 ILI9341 TFT display, its full name (with extension) is ILI9341.c, download link is below:
ILI9341 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 ILI9341 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_SDI) and clock pin (TFT_SCK) are defined in the main code (before #include “ILI9341.c”) then the library will automatically use software SPI.
example (respectively, TFT_SDI and TFT_SCK are connected to RD4 and RD5):
#define TFT_SDI RD4_bit
#define TFT_SCK RD5_bit

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

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). Hardware SPI1 module and the ILI9341 TFT display are initialized as:

The other pins: RST, CS and DC are defined as shown below (TFT_RST is optional):

Rest of code is described through comments.

Full mikroC 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 “PIC MCU with ILI9341 TFT – Graphics Test Example | mikroC Projects”

  1. I HAVE DOWNLOADED THE CODE BUT CANT COMPILE DUE TO SOME ERROS :

    422 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    424 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    448 324 Undeclared identifier ‘drawVLine’ in expression gfx_library.c
    451 324 Undeclared identifier ‘drawHLine’ in expression gfx_library.c
    473 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    474 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    475 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    476 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    488 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    489 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    490 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    491 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    492 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    493 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    494 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    495 324 Undeclared identifier ‘drawPixel’ in expression gfx_library.c
    497 312 Internal error ” gfx_library.c
    1 122 Compilation Started gfx_library.c
    539 123 Compiled Successfully ili9341.c
    516 1164 Variable ‘r’ has been eliminated by optimizer ili9341.c
    534 1164 Variable ‘hi’ has been eliminated by optimizer ili9341.c

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