This post shows how to connect ST7735S TFT display to PIC18F4550 microcontroller and display different things (numbers, text, lines, circles β¦..). The compiler used is CCS PIC C.
To interface PIC18F4550 with the ST7735 TFT display we need a small library (driver) which can be downloaded from its original post at the following url:
ST7735 SPI TFT Display Driver for CCS PIC C compiler
Or you can just download it directly from the following link:
ST7735 SPI TFT Display Driver
Put the downloaded C file in your project folder.
Required Components:
- PIC18F4550 Microcontroller
- ST7735R (or S) 1.8β³ SPI TFT Display
- 5 x 1K Resistors (If the system is 3.3V there is no need for these resistors)
- Power Supply Source (+5V or +3.3V)
- Breadboard
- Jumper Wires
Interfacing PIC18F4550 with ST7735S 1.8β³ SPI TFT display circuit:
PIC18F4550 internal oscillator is used in this project and MCLR pin function is disabled.
The system power supply is 5V and if you are using a microcontroller TFT display of 3.3V remove all the 1K resistors (5 resistors) from the circuit and connect the TFT display directly to the microcontroller.
Interfacing PIC18F4550 with ST7735S 1.8β³ SPI TFT display CCS C code:
PIC18F4550 SPI module is used for to communicate with the TFT display. SPI module uses the following pins:
SDI (RB0): data input pin (not used in this project)
SCK (RB1): clock line
SDO (RC7): data output
// TFT module connections
#define TFT_CSΒ PIN_B2
#define TFT_DCΒ PIN_B3
#define TFT_SPI_HARDWARE
// End TFT module connections
My TFT display is ST7735R Black Tap (ST7735S) and for initializing this type of TFT display I used the following line:
TFT_BlackTab_Initialize();
If you have a TFT display with green or red tabs or a TFT with ST7735B controller read the driver topic above.
Note that green, red and black tabs have the same controller ST7735R.
The microcontroller runs with its internal oscillator at 8MHz.
This code is tested with CCS PIC C compiler PCWHD versions 4 and 5.
//Β PIC18F4550Β ST7735Β 1.8"Β SPIΒ TFTΒ displayΒ exampleΒ CCSΒ CΒ code //Β ST7735Β TFTΒ displayΒ driverΒ forΒ CCSΒ PICΒ CΒ compilerΒ isΒ required //Β http://ccspicc.blogspot.com/ //Β [email protected] //Β TFTΒ moduleΒ connections #define TFT_CS PIN_B2 #define TFT_DC PIN_B3 #define TFT_SPI_HARDWARE //Β EndΒ TFTΒ moduleΒ connections #include <18F4550.h> #fuses NOMCLR INTRC_IO #use delay(clock = 8000000) #include <ST7735_TFT.c> int8 k = 0; char *txt = "1.8 Inch ST7735 TFT display test example"; void testlines(unsigned int16 color) { Β Β unsigned int8 x, y; Β Β fillScreen(ST7735_BLACK); Β Β for (x=0; x < _width; x+=6) { Β Β Β Β drawLine(0,Β 0,Β x,Β _height-1,Β color); Β Β } Β Β for (y=0; y < _height; y+=6) { Β Β Β Β drawLine(0,Β 0,Β _width-1,Β y,Β color); Β Β } Β Β fillScreen(ST7735_BLACK); Β Β for (x=0; x < _width; x+=6) { Β Β Β Β drawLine(_width-1,Β 0,Β x,Β _height-1,Β color); Β Β } Β Β for (y=0; y < _height; y+=6) { Β Β Β Β drawLine(_width-1,Β 0,Β 0,Β y,Β color); Β Β } Β Β fillScreen(ST7735_BLACK); Β Β for (x=0; x < _width; x+=6) { Β Β Β Β drawLine(0,Β _height-1,Β x,Β 0,Β color); Β Β } Β Β for (y=0; y < _height; y+=6) { Β Β Β Β drawLine(0,Β _height-1,Β _width-1,Β y,Β color); Β Β } Β Β fillScreen(ST7735_BLACK); Β Β for (x=0; x < _width; x+=6) { Β Β Β Β drawLine(_width-1,Β _height-1,Β x,Β 0,Β color); Β Β } Β Β for (y=0; y < _height; y+=6) { Β Β Β Β drawLine(_width-1,Β _height-1,Β 0,Β y,Β color); Β Β } } void testfastlines(unsigned int16 color1, unsigned int16 color2) { Β Β int16 x, y; Β Β fillScreen(ST7735_BLACK); Β Β for (y = 0; y < _height; y += 5) { Β Β Β Β drawFastHLine(0,Β y,Β _width,Β color1); Β Β } Β Β for (x = 0; x < _width; x += 5) { Β Β Β Β drawFastVLine(x,Β 0,Β _height,Β color2); Β Β } } void testdrawrects(unsigned int16 color) { Β Β int16 x; Β Β fillScreen(ST7735_BLACK); Β Β for (x = 0; x < _width; x+=6) { Β Β Β Β drawRect(_width/2Β -x/2,Β _height/2Β -x/2Β ,Β x,Β x,Β color); Β Β } } void testfillrects(unsigned int16 color1, unsigned int16 color2) { Β Β int16 x; Β Β fillScreen(ST7735_BLACK); Β Β for (x = _width - 1; x > 6; x -= 6) { Β Β Β Β fillRect(_width/2Β -x/2,Β _height/2Β -x/2Β ,Β x,Β x,Β color1); Β Β Β Β drawRect(_width/2Β -x/2,Β _height/2Β -x/2Β ,Β x,Β x,Β color2); Β Β } } void testfillcircles(unsigned int8 radius, unsigned int16 color) { Β Β int16 x, y; Β Β for (x = radius; x < _width; x += radius * 2) { Β Β Β Β for (y = radius; y < _height; y += radius * 2) { Β Β Β Β Β Β fillCircle(x,Β y,Β radius,Β color); Β Β Β Β } Β Β } } void testdrawcircles(unsigned int8 radius, unsigned int16 color) { Β Β int16 x, y; Β Β for (x = 0; x < _width + radius; x += radius * 2) { Β Β Β Β for (y = 0; y < _height + radius; y += radius * 2) { Β Β Β Β Β Β drawCircle(x,Β y,Β radius,Β color); Β Β Β Β } Β Β } } void testroundrects() { Β Β int8 i, t; Β Β unsigned int16 color = 100; Β Β fillScreen(ST7735_BLACK); Β Β for(t = 0 ; t <= 4; t += 1) { Β Β Β Β unsigned int8 x = 0, y = 0, w = _width - 2, h = _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 int8 t, w, x, y, z; Β Β unsigned int16 color = 0xF800; Β Β fillScreen(ST7735_BLACK); Β Β wΒ =Β _width/2,Β xΒ =Β _heightΒ -Β 1,Β yΒ =Β 0,Β zΒ =Β _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 main(){ Β Β setup_oscillator(OSC_8MHZ);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Set internal oscillator to 8MHz Β Β setup_adc_ports(NO_ANALOGS);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Configure AN pins as digital Β Β TFT_BlackTab_Initialize(); Β Β fillScreen(ST7735_BLACK); Β Β drawtext(0,Β 5,Β txt,Β ST7735_WHITE,Β ST7735_BLACK,Β 1); Β Β setTextWrap(false); Β Β strcpyΒ (txt,Β "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); Β Β fillScreen(ST7735_BLACK); Β Β drawFastHLine(0,Β 53,Β _width,Β Β ST7735_WHITE); Β Β drawFastHLine(0,Β 106,Β _width,Β ST7735_WHITE); Β Β while(k++ < 20){ Β Β Β Β sprintf(txt,"%02u",k); Β Β Β Β drawtext(59,Β 25,Β txt,Β Β ST7735_GREEN,Β ST7735_BLACK,Β 1); Β Β Β Β drawtext(54,Β 75,Β txt,Β Β ST7735_BLUE,Β Β ST7735_BLACK,Β 2); Β Β Β Β drawtext(49,Β 125,Β txt,Β 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(TRUE){ Β Β Β Β invertDisplay(true); Β Β Β Β delay_ms(500); Β Β Β Β invertDisplay(false); Β Β Β Β delay_ms(500); Β Β } }
Some images from my home made circuit:
Interfacing PIC18F4550 with ST7735S 1.8β³ SPI TFT display video:
Project video β¦.