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
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.
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:
1 2 | #include "ILI9341.c" // include ILI9341 TFT display driver #include "GFX_Library.c" // include graphics library source code |
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:
1 2 3 4 5 | // initialize SPI1 module SPI1_Init(); // initialize ILI9341 TFT tft_begin(); // init done! |
The other pins: RST, CS and DC are defined as shown below (TFT_RST is optional):
1 2 3 4 5 6 7 8 | // define ILI9341 TFT module pin connections // RST (optional), CS and DC #define TFT_CS RD0_bit #define TFT_RST RD1_bit #define TFT_DC RD2_bit #define TFT_CS_DIR TRISD0_bit #define TFT_RST_DIR TRISD1_bit #define TFT_DC_DIR TRISD2_bit |
Rest of code is described through comments.
Full mikroC code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | /**************************************************************************** Interfacing PIC18F46K22 microcontroller with ILI9341 TFT display (240x320 pixel). Graphics test example. C Code for mikroC PRO for PIC compiler. Internal oscillator used @ 64MHz Configuration words: CONFIG1H = 0x0028 CONFIG2L = 0x0018 CONFIG2H = 0x003C CONFIG3H = 0x0037 CONFIG4L = 0x0081 CONFIG5L = 0x000F CONFIG5H = 0x00C0 CONFIG6L = 0x000F CONFIG6H = 0x00E0 CONFIG7L = 0x000F CONFIG7H = 0x0040 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************** This is our GFX example for the Adafruit ILI9341 Breakout and Shield ----> http://www.adafruit.com/products/1651 Check out the links above for our tutorials and wiring diagrams These displays use SPI to communicate, 4 or 5 pins are required to interface (RST is optional) Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. MIT license, all text above must be included in any redistribution *****************************************************************************/ // define ILI9341 TFT module pin connections // RST (optional), CS and DC #define TFT_CS RD0_bit #define TFT_RST RD1_bit #define TFT_DC RD2_bit #define TFT_CS_DIR TRISD0_bit #define TFT_RST_DIR TRISD1_bit #define TFT_DC_DIR TRISD2_bit #include "ILI9341.c" // include ILI9341 TFT display driver #include "GFX_Library.c" // include graphics library source code void testFillScreen() { display_fillScreen(ILI9341_BLACK); display_fillScreen(ILI9341_RED); display_fillScreen(ILI9341_GREEN); display_fillScreen(ILI9341_BLUE); display_fillScreen(ILI9341_BLACK); } void testText() { display_fillScreen(ILI9341_BLACK); display_setCursor(0, 0); display_setTextColor(ILI9341_WHITE, ILI9341_WHITE); // set text color to white with transparant background display_setTextSize(1); display_puts("Hello World!\r\n"); display_setTextColor(ILI9341_YELLOW, ILI9341_YELLOW); display_setTextSize(2); display_printf("%7.2f\r\n", 1234.56); display_setTextColor(ILI9341_RED, ILI9341_RED); display_setTextSize(3); display_printf("%LX\r\n", 0xDEADBEEF); display_puts("\r\n"); // start a new line display_setTextColor(ILI9341_GREEN, ILI9341_GREEN); display_setTextSize(5); display_puts("Groop\r\n"); display_setTextSize(2); display_puts("I implore thee,\r\n"); display_setTextSize(1); display_puts("my foonting turlingdromes.\r\n"); display_puts("And hooptiously drangle me\r\n"); display_puts("with crinkly bindlewurdles,\r\n"); display_puts("Or I will rend thee\r\n"); display_puts("in the gobberwarts\r\n"); display_puts("with my blurglecruncheon,\r\n"); display_puts("see if I don't!\r\n"); } void testLines(uint16_t color) { int16_t x1, y1, x2, y2, w = display_width, h = display_height; display_fillScreen(ILI9341_BLACK); x1 = y1 = 0; y2 = h - 1; for(x2=0; x2<w; x2+=6) display_drawLine(x1, y1, x2, y2, color); x2 = w - 1; for(y2=0; y2<h; y2+=6) display_drawLine(x1, y1, x2, y2, color); display_fillScreen(ILI9341_BLACK); x1 = w - 1; y1 = 0; y2 = h - 1; for(x2=0; x2<w; x2+=6) display_drawLine(x1, y1, x2, y2, color); x2 = 0; for(y2=0; y2<h; y2+=6) display_drawLine(x1, y1, x2, y2, color); display_fillScreen(ILI9341_BLACK); x1 = 0; y1 = h - 1; y2 = 0; for(x2=0; x2<w; x2+=6) display_drawLine(x1, y1, x2, y2, color); x2 = w - 1; for(y2=0; y2<h; y2+=6) display_drawLine(x1, y1, x2, y2, color); display_fillScreen(ILI9341_BLACK); x1 = w - 1; y1 = h - 1; y2 = 0; for(x2=0; x2<w; x2+=6) display_drawLine(x1, y1, x2, y2, color); x2 = 0; for(y2=0; y2<h; y2+=6) display_drawLine(x1, y1, x2, y2, color); } void testFastLines(uint16_t color1, uint16_t color2) { int16_t x, y, w = display_width, h = display_height; display_fillScreen(ILI9341_BLACK); for(y=0; y<h; y+=5) display_drawHLine(0, y, w, color1); for(x=0; x<w; x+=5) display_drawVLine(x, 0, h, color2); } void testRects(uint16_t color) { int16_t n, i, i2, cx = display_width / 2, cy = display_height / 2; display_fillScreen(ILI9341_BLACK); n = min(display_width, display_height); for(i=2; i<n; i+=6) { i2 = i / 2; display_drawRect(cx-i2, cy-i2, i, i, color); } } void testFilledRects(uint16_t color1, uint16_t color2) { int16_t n, i, i2, cx = display_width / 2 - 1, cy = display_height / 2 - 1; display_fillScreen(ILI9341_BLACK); n = min(display_width, display_height); for(i=n; i>0; i-=6) { i2 = i / 2 - 1; display_fillRect(cx-i2, cy-i2, i, i, color1); // Outlines are not included in timing results display_drawRect(cx-i2, cy-i2, i, i, color2); } } void testFilledCircles(uint8_t radius, uint16_t color) { int16_t x, y, w = display_width, h = display_height, r2 = radius * 2; display_fillScreen(ILI9341_BLACK); for(x=radius; x<w; x+=r2) { for(y=radius; y<h; y+=r2) { display_fillCircle(x, y, radius, color); } } } void testCircles(uint8_t radius, uint16_t color) { int16_t x, y, r2 = radius * 2, w = display_width + radius, h = display_height + radius; for(x=0; x<w; x+=r2) { for(y=0; y<h; y+=r2) { display_drawCircle(x, y, radius, color); } } } void testTriangles() { int16_t n, i, cx = display_width / 2 - 1, cy = display_height / 2 - 1; uint16_t color = 0xF600; display_fillScreen(ILI9341_BLACK); n = min(cx, cy); for(i=0; i<n; i+=5) { display_drawTriangle( cx , cy - i, // peak cx - i, cy + i, // bottom left cx + i, cy + i, // bottom right color); color += 100; } } void testFilledTriangles() { int16_t i, cx = display_width / 2 - 1, cy = display_height / 2 - 1; display_fillScreen(ILI9341_BLACK); for(i=min(cx,cy); i>10; i-=5) { display_fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i, display_color565(0, i*2, i*2)); display_drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i, display_color565(i*2, i*2, 0)); } } void testRoundRects() { int16_t w, i, i2, cx = display_width / 2 - 1, cy = display_height / 2 - 1; display_fillScreen(ILI9341_BLACK); w = min(display_width, display_height); for(i=0; i<w; i+=6) { i2 = i / 2; display_drawRoundRect(cx-i2, cy-i2, i, i, i/8, display_color565(i, 0, 0)); } } void testFilledRoundRects() { int16_t i, i2, cx = display_width / 2 - 1, cy = display_height / 2 - 1; display_fillScreen(ILI9341_BLACK); for(i=min(display_width, display_height); i>20; i-=6) { i2 = i / 2; display_fillRoundRect(cx-i2, cy-i2, i, i, i/8, display_color565(0, i, 0)); } } // main function void main() { OSCCON = 0x70; // set internal oscillator to 16MHz PLLEN_bit = 1; // enable 4xPLL (clock frequency becomes 16 x 4 = 64MHz) ANSELC = 0; // configure all PORTC pins as digital ANSELD = 0; // configure all PORTD pins as digital // initialize SPI1 module SPI1_Init(); // initialize ILI9341 TFT tft_begin(); // init done! delay_ms(10); testFillScreen(); delay_ms(500); testText(); delay_ms(3000); testLines(ILI9341_CYAN); delay_ms(500); testFastLines(ILI9341_RED, ILI9341_BLUE); delay_ms(500); testRects(ILI9341_GREEN); delay_ms(500); testFilledRects(ILI9341_YELLOW, ILI9341_MAGENTA); delay_ms(500); testFilledCircles(10, ILI9341_MAGENTA); testCircles(10, ILI9341_WHITE); delay_ms(500); testTriangles(); delay_ms(500); testFilledTriangles(); delay_ms(500); testRoundRects(); delay_ms(500); testFilledRoundRects(); delay_ms(500); while(1) { uint8_t rotation; for(rotation=0; rotation<4; rotation++) { display_setRotation(rotation); testText(); delay_ms(1000); } } } // end of 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
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Hi SC,
Do you have a sample include add 3 button.
Thanks.
yes, the MikroC version threw the same errors but the version for CCS compiles without error. A fully functional 45 day demo of CCS is available for download.
can you shere your hex
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
How can I display image?