This post shows how to use mikroC library for ST7735R TFT with PIC16F887 microcontroller where hardware SPI module of the PIC16F887 is used in this example. A simulation of this example using Proteus is provided at the end of the post.
The ST7735 uses SPI communication protocol and the PIC16F887 has one SPI module where the PIC16F887 is the master device and the TFT is the slave device.
Generally the SPI protocol has 3 lines: SCK (serial clock), SDI (serial data in) and SDO (serial data out) but for interfacing the ST7735 TFT we use only 2 lines: SCK and SDO.
The PIC16F887 has one SPI module with SCK mapped to RC3 pin (#18) and SDO mapped to RC5 (#24).
To be able to compile the mikroC code below we need a small file named ST7735_TFT.c , this file is the driver (library) of this type of TFT. The library file download link can be found at the end of this topic:
ST7735R SPI TFT driver for mikroC PRO for PIC
After downloading the driver file just put it in the project folder.
Hardware Required:
- PIC16F887 microcontroller —> datasheet
- ST7735 1.8″ TFT display
- 20MHz crystal oscillator
- 2 x 22pF ceramic capacitor
- 5 x 1k ohm resistors
- Protoboard
- 5V Power supply source
- Jumper wires
- PIC Microcontroller programmer (PICkit 2, PICkit 3…)
Interfacing PIC microcontroller with ST7735R TFT circuit:
Example circuit diagram is shown below.
(All grounded terminals are connected together)
In this project I used 20MHz crystal oscillator which is the maximum speed of the PIC16F887, this gives the highest SPI data transfer rate (5Mbit/s). Lower crystal frequencies can be used or even the internal oscillator of the microcontroller.
5 x 1k ohm resistors are needed for the all control lines because the ST7735R works with 3.3V and the PIC16F887 works with 5V , it may be damaged if it connected directly to the microcontroller. The TFT is supplied with 5V because its board contains AMS1117 3.3V voltage regulator.
There is no pull-up resistor connected to MCLR pin because it is configured to work as a digital input pin (in the software).
Interfacing PIC microcontroller with ST7735R TFT mikroC code:
As mentioned above, in order to compile the mikroC code below without any error we have to add the driver source file of the ST7735R to the project folder, the full name of this file is ST7735_TFT.c and its download link can be found in the page with the link above.
Configuration words:
CONFIG1: 0x2CD2
CONFIG2: 0x0700
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 | // Interfacing ST7735R SPI TFT with PIC16F887 mikroC code // External HS oscillator used @ 20 MHz // Configuration words: CONFIG1 = 0x2CD2 // CONFIG2 = 0x0700 // SPI TFT module connections #define TFT_SPI_HARDWARE // Hardware SPI module is used sbit TFT_CS at RD0_bit; // TFT CS pin is connected to RD0 pin sbit TFT_DC at RD1_bit; // TFT DC pin is connected to RD1 pin sbit TFT_CS_Direction at TRISD0_bit; sbit TFT_DC_Direction at TRISD1_bit; // End SPI TFT module connections #include <ST7735_TFT.c> // Include ST7735 TFT driver source file unsigned short k = 0; char *txt = "Hello World!"; char *txt1 = " "; void testlines(unsigned int color) { unsigned short x, y; fillScreen(ST7735_BLACK); for (x=0; x < TFT_Width; x+=6) { drawLine(0, 0, x, TFT_Height-1, color); } for (y=0; y < TFT_Height; y+=6) { drawLine(0, 0, TFT_Width-1, y, color); } fillScreen(ST7735_BLACK); for (x=0; x < TFT_Width; x+=6) { drawLine(TFT_Width-1, 0, x, TFT_Height-1, color); } for (y=0; y < TFT_Height; y+=6) { drawLine(TFT_Width-1, 0, 0, y, color); } fillScreen(ST7735_BLACK); for (x=0; x < TFT_Width; x+=6) { drawLine(0, TFT_Height-1, x, 0, color); } for (y=0; y < TFT_Height; y+=6) { drawLine(0, TFT_Height-1, TFT_Width-1, y, color); } fillScreen(ST7735_BLACK); for (x=0; x < TFT_Width; x+=6) { drawLine(TFT_Width-1, TFT_Height-1, x, 0, color); } for (y=0; y < TFT_Height; y+=6) { drawLine(TFT_Width-1, TFT_Height-1, 0, y, color); } } void testfastlines(unsigned int color1, unsigned int color2) { unsigned int x, y; fillScreen(ST7735_BLACK); for (y = 0; y < TFT_Height; y += 5) { drawFastHLine(0, y, TFT_Width, color1); } for (x = 0; x < TFT_Width; x += 5) { drawFastVLine(x, 0, TFT_Height, color2); } } void testdrawrects(unsigned int color) { unsigned int x; fillScreen(ST7735_BLACK); for (x = 0; x < TFT_Width; x+=6) { drawRect(TFT_Width/2 -x/2, TFT_Height/2 -x/2 , x, x, color); } } void testfillrects(unsigned int color1, unsigned int color2) { unsigned int x; fillScreen(ST7735_BLACK); for (x = TFT_Width - 1; x > 6; x -= 6) { fillRect(TFT_Width/2 -x/2, TFT_Height/2 -x/2 , x, x, color1); drawRect(TFT_Width/2 -x/2, TFT_Height/2 -x/2 , x, x, color2); } } void testfillcircles(unsigned short radius, unsigned int color) { unsigned int x, y; for (x = radius; x < TFT_Width; x += radius * 2) { for (y = radius; y < TFT_Height; y += radius * 2) { fillCircle(x, y, radius, color); } } } void testdrawcircles(unsigned short radius, unsigned int color) { unsigned int x, y; for (x = 0; x < TFT_Width + radius; x += radius * 2) { for (y = 0; y < TFT_Height + radius; y += radius * 2) { drawCircle(x, y, radius, color); } } } void testroundrects() { unsigned short i, t; unsigned int color = 100; fillScreen(ST7735_BLACK); for(t = 0 ; t <= 4; t += 1) { unsigned short x = 0, y = 0, w = TFT_Width - 2, h = TFT_Height - 2; for(i = 0 ; i <= 16; i++) { drawRoundRect(x, y, w, h, 5, color); x += 2; y += 3; w -= 4; h -= 6; color += 1100; } color += 100; } } void testtriangles() { unsigned short t, w, x, y, z; unsigned int color = 0xF800; fillScreen(ST7735_BLACK); w = TFT_Width/2, x = TFT_Height - 1, y = 0, z = TFT_Width; for(t = 0 ; t <= 15; t++) { drawTriangle(w, y, y, x, z, x, color); x -= 4; y += 4; z -= 4; color += 100; } } void mediabuttons() { // play fillScreen(ST7735_BLACK); fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE); fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED); delay_ms(500); // pause fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE); fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN); fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN); delay_ms(500); // play color fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE); delay_ms(50); // pause color fillRoundRect(39, 98, 20, 45, 5, ST7735_RED); fillRoundRect(69, 98, 20, 45, 5, ST7735_RED); // play color fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN); } void tst(){ drawtext(0, 5, "1.8 Inch ST7735 TFT display test example", ST7735_WHITE, ST7735_BLACK, 1); } void main() { ST7735_TFT_Init(); // Initialize the TFT module fillScreen(ST7735_BLACK); tst(); setTextWrap(0); // Turn off text wrap // Draw text "Hello World!" drawtext(0, 30, txt, ST7735_RED, ST7735_BLACK, 1); drawtext(0, 47, txt, ST7735_YELLOW, ST7735_BLACK, 2); drawtext(0, 80, txt, ST7735_MAGENTA, ST7735_BLACK, 3); drawtext(0, 120, txt, ST7735_CYAN, ST7735_BLACK, 4); delay_ms(5000); // Wait 5 seconds fillScreen(ST7735_BLACK); drawFastHLine(0, 53, TFT_Width, ST7735_WHITE); drawFastHLine(0, 106, TFT_Width, ST7735_WHITE); // Display number while(k++ < 20){ txt1[0] = (k / 10)%10 + 48; txt1[1] = (k % 10) + 48; drawtext(59, 25, txt1, ST7735_GREEN, ST7735_BLACK, 1); drawtext(54, 75, txt1, ST7735_BLUE, ST7735_BLACK, 2); drawtext(49, 125, txt1, ST7735_RED, ST7735_BLACK, 3); delay_ms(500); } testlines(ST7735_YELLOW); delay_ms(1000); testfastlines(ST7735_RED, ST7735_BLUE); delay_ms(1000); testdrawrects(ST7735_GREEN); delay_ms(1000); testfillrects(ST7735_YELLOW, ST7735_MAGENTA); delay_ms(1000); fillScreen(ST7735_BLACK); testfillcircles(10, ST7735_BLUE); testdrawcircles(10, ST7735_WHITE); delay_ms(1000); testroundrects(); delay_ms(1000); testtriangles(); delay_ms(1000); mediabuttons(); delay_ms(1000); while(1) { invertDisplay(1); delay_ms(500); invertDisplay(0); delay_ms(500); } } // End of code |
PIC16F887 + ST7735R Proteus simulation video:
PIC16F887 + ST7735R Proteus simulation file download:
Download
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
168 324 Undeclared identifier ‘TFT_DATA’ in expression ST7735_TFT.c
IDK , but the #include cant be open
thats what my out log error says .
i need help if u there .
OLED ST7735R SPI 대신 OLED SSD1306 SPI 프로그램 을 부탁합니다..
Hi I’m sorry but I couldn’t find the library that you said, where I can download it? And how I can install it into Micro C v.7.1 for no errors during compile??
Download link:
ST7735 Library
after you download the C file which named “ST7735_TFT.c”, add it to your project folder!
relay it’s very helpful for me….. Thanks ( Simple Projects )