ST7735R SPI TFT driver for mikroC PRO for PIC

This is a small library (driver) of the ST7735R (ST7735S) SPI TFT display (1.8″ , 128×160 Pixel resolution) for mikroC PRO for PIC compiler. With this library we can easily connect the ST7735R TFT display with any PIC microcontroller with sufficient RAM and ROM which depends on the project.
This library is based on the Adafruit TFT library for Arduino. This library works with any PIC microcontroller with or without hardware SPI module (hardware SPI is faster than software SPI).
Basically the ST7735R TFT display has 5 control lines:  RST (Reset, active low), CS (Chip Select, active low), DC (Data/Command), SDA (Serial Data Line) and SCL (Serial Clock Line).

Some images of the ST7735R TFT module:

ST7735 1.8 TFT display

ST7735 TFT display

Using the ST7735 TFT library:
The ST7735 SPI TFT library can work with hardware SPI or software SPI. The hardware SPI is more faster than the software SPI. If you want to use your microcontroller hardware SPI module the following variables must be defined:

#define TFT_SPI_HARDWARE
#define TFT_CS
#define TFT_DC
#define TFT_CS_Direction
#define TFT_DC_Direction

And if you want to use software SPI define the following parameters:
#define TFT_CS
#define TFT_DC
#define TFT_CLK
#define TFT_DATA
#define TFT_CS_Direction
#define TFT_DC_Direction
#define TFT_CLK_Direction
#define TFT_DATA_Direction

Examples:
The following ST7735 SPI TFT display is connected to microcontroller hardware SPI module pins, CS is connected to RD0 and DC is connected to RD1:
#define TFT_SPI_HARDWARE
#define TFT_CS at RD0_bit;
#define TFT_DC at RD1_bit;
#define TFT_CS_Direction at TRISD0_bit;
#define TFT_DC_Direction at TRISD1_bit;

The following ST7735 SPI TFT is connected to the microcontroller where software SPI is used:
#define TFT_CS at RD0_bit;
#define TFT_DC at RD1_bit;
#define TFT_CLK at RD2_bit;
#define TFT_DATA at RD3_bit;
#define TFT_CS_Direction at TRISD0_bit;
#define TFT_DC_Direction at TRISD1_bit;
#define TFT_CLK_Direction at TRISD2_bit;
#define TFT_DATA_Direction at TRISD3_bit;

Library Functions:
void ST7735_TFT_Init();
void drawPixel(unsigned short x, unsigned short y, unsigned int color);
void fillRectangle(unsigned short x, unsigned short y, unsigned short w, unsigned short h, unsigned int color);
void fillScreen(unsigned int color);
void drawFastVLine(unsigned short x, unsigned short y, unsigned short h, unsigned int color);
void drawFastHLine(unsigned short x, unsigned short y, unsigned short h, unsigned int color);
void drawCircle(signed int x0, signed int y0, signed int r, unsigned int color);
void drawCircleHelper(signed int x0, signed int y0, signed int r, unsigned short cornername, unsigned int color);
void fillCircleHelper(signed int x0, signed int y0, signed int r, unsigned short cornername, signed int delta, unsigned int color);
void fillCircle(signed int x0, signed int y0, signed int r, unsigned int color);
void drawRect(unsigned short x, unsigned short y, unsigned short w, unsigned short h, unsigned int color);
void fillRect(unsigned short x, unsigned short y, unsigned short w, unsigned short h, unsigned int color);
void drawLine(signed int x0, signed int y0, signed int x1, signed int y1, unsigned int color);
void drawRoundRect(unsigned short x, unsigned short y, unsigned short w, unsigned short h, unsigned short r, unsigned int color);
void fillRoundRect(unsigned short x, unsigned short y, unsigned short w, unsigned short h, unsigned short r, unsigned int color);
void drawTriangle(signed int x0, signed int y0, signed int x1, signed int y1, signed int x2, signed int y2, unsigned int color);
void fillTriangle(signed int x0, signed int y0, signed int x1, signed int y1, signed int x2, signed int y2, unsigned int color);
void drawChar(unsigned short x, unsigned short y, unsigned int c, unsigned int color, unsigned int bg,  unsigned short size);
void setTextWrap(unsigned short w);
void drawtext(unsigned short x, unsigned short y, char *_text, unsigned int color, unsigned int bg, unsigned short size);
void invertDisplay(unsigned short i);
void setScrollDefinition(unsigned short top_fix_height, unsigned short bottom_fix_height, unsigned short _scroll_direction);
void VerticalScroll(unsigned short _vsp);
void NormalDisplay();
unsigned int Color565(unsigned int r, unsigned int g, unsigned int b);
void pushColor(unsigned int color)

Colors:
The following colors are defined in the library.
ST7735_BLACK
ST7735_BLUE
ST7735_RED
ST7735_GREEN
ST7735_CYAN
ST7735_MAGENTA
ST7735_YELLOW
ST7735_WHITE

Function descriptions:

ST7735_TFT_Init(): initializes the ST7735 module, must be called before any other function.
fillScreen:
fills the whole screen with a given color.
drawFastVLine: draws a vertical line starting at (x, y) with height h.
drawFastHLine: draws a horizontal line starting at (x, y) with width w.
drawCircle: draws a circle where (x0, y0) are center coordinates an r is circle radius.
drawRect: draws rectangle at (x,y) where h is height and w is width of the rectangle.
fillRect: fills a rectangle starting from coordinates (x, y) with width of w and height of h.
drawLine: draws a line from (x0, y0) to (x1, y1).
drawRoundRect: draws a rectangle with rounded edges. h: height, w:width, r: radius of the rounded edges.
drawTriangle: draws a triangle of coordinates (x0, y0), (x1, y1) and (x2, y2).
drawChar: writes a char (c) on the TFT at coordinates (x, y). size: char size.
drawText: Writes text (*text) on the TFT at coordinates (x, y). size: text size.
setTextWrap: turn on or off wrap of the text (1 or 0).
invertDisplay: invert display colors (1 or 0).
setScrollDefinition: This command defines the Vertical Scrolling Area of the display where:
top_fix_height: describes the Top Fixed Area,
bottom_fix_height: describes the Bottom Fixed Area and
_scroll_direction: is scroll direction (0 for top to bottom and 1 for bottom to top).
VerticalScroll: This command is used together with the last command. These two commands describe the scrolling area and the scrolling mode.
NormalDisplay(): returns the TFT display to its normal state for example exit from scrolling mode.
Color565: converts 24-bit color format into 16-bit format and returns the result.

Library download:
ST7735R library source file can be downloaded from the link below, library file full name is ‘ST7735_TFT.C’ . Adding this library to the project is so easy, just put the library file (ST7735_TFT.C) in the project folder and add #include <ST7735_TFT.c> to project source file.

ST7735R mikroC Library

Examples:
Interfacing PIC microcontroller with ST7735R SPI TFT – mikroC Projects

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.