Monday, October 31, 2016

ST7735 SPI TFT Display Driver for CCS PIC C compiler


ST7735 SPI TFT Display Library for CCS PIC C compiler
The ST7735 SPI TFT library allows the interfacing of this display with any PIC microcontroller more simpler and easier. This library is based on the Adafruit TFT library for Arduino. This library works with any PIC microcontroller with or without SPI module (hardware SPI is faster than software SPI).
Basically the TFT display has 5 control lines:  RST (Reset, active low), CS (Chip Select, active low), DC (Data/Command), SDA (Data Line) and SCL (Clock Line).
Using ST7735 SPI TFT Library:
The ST7735 SPI TFT library can work with hardware SPI or software. The hardware SPI is much faster than the software SPI. If you want to use your microcontroller hardware SPI module the following variables must be defined:
#define TFT_CS
#define TFT_DC
#define TFT_SPI_HARDWARE

And if you want to use software SPI define the following parameters:
#define TFT_CS
#define TFT_DC
#define TFT_CLK
#define TFT_DATA
 
 
Examples:
The following ST7735 SPI TFT display is connected to microcontroller hardware SPI module pins:
#define TFT_CS   PIN_D1
#define TFT_DC   PIN_D0

#define TFT_SPI_HARDWARE

The following ST7735 SPI TFT is connected to the microcontroller where software SPI is used:
#define TFT_CS   PIN_D1
#define TFT_DC   PIN_D0
#define TFT_CLK  PIN_D2
#define TFT_DATA PIN_D3

Library Routines:
Generally there are 4 types of the ST7735 TFT display. One has ST7735B controller and the other 3 types are:
ST7735R Green Tab
ST7735R Red Tab
ST7735R Black Tab (ST7735S)
As for a simple LCD display the TFT display has an initialization sequence which can be made using the following functions according to the TFT display type:
TFT_ST7735B_Initialize();                 // For ST7735B
TFT_GreenTab_Initialize();               // For ST7735R green tab
TFT_RedTab_Initialize();                   // For ST7735R red tab
TFT_BlackTab_Initialize();                // For ST7735R black tab (ST7735S)
The other functions are:
drawPixel(unsigned int8 x, unsigned int8 y, unsigned int16 color);
fillRectangle(unsigned int8 x, unsigned int8 y, unsigned int8 w, unsigned int8 h, unsigned int16 color);
fillScreen(unsigned int16 color);
drawFastVLine(unsigned int8 x, unsigned int8 y, unsigned int8 h, unsigned int16 color);
drawFastHLine(unsigned int8 x, unsigned int8 y, unsigned int8 h, unsigned int16 color);
drawCircle(signed int16 x0, signed int16 y0, signed int16 r, unsigned int16 color);
drawCircleHelper(signed int16 x0, signed int16 y0, signed int16 r, unsigned int8 cornername, unsigned int16 color);
fillCircleHelper(signed int16 x0, signed int16 y0, signed int16 r, unsigned int8 cornername, signed int16 delta, unsigned int16 color);
fillCircle(signed int16 x0, signed int16 y0, signed int16 r, unsigned int16 color);
drawRect(unsigned int8 x, unsigned int8 y, unsigned int8 w, unsigned int8 h, unsigned int16 color);
fillRect(unsigned int8 x, unsigned int8 y, unsigned int8 w, unsigned int8 h, unsigned int16 color);
drawLine(signed int16 x0, signed int16 y0, signed int16 x1, signed int16 y1, unsigned int16 color);
drawRoundRect(unsigned int8 x, unsigned int8 y, unsigned int8 w, unsigned int8 h, unsigned int8 r, unsigned int16 color);
fillRoundRect(unsigned int8 x, unsigned int8 y, unsigned int8 w, unsigned int8 h, unsigned int8 r, unsigned int16 color);
drawTriangle(signed int16 x0, signed int16 y0, signed int16 x1, signed int16 y1, signed int16 x2, signed int16 y2, unsigned int16 color);
fillTriangle(signed int16 x0, signed int16 y0, signed int16 x1, signed int16 y1, signed int16 x2, signed int16 y2, unsigned int16 color);
drawChar(unsigned int8 x, unsigned int8 y, unsigned int16 c, unsigned int16 color, unsigned int16 bg,  unsigned int8 size);
setTextWrap(int1 w);
drawtext(unsigned int8 x, unsigned int8 y, char *_text, unsigned int16 color, unsigned int16 bg, unsigned int8 size);
invertDisplay(int1 i);
setScrollDefinition(unsigned int8 top_fix_height, unsigned int8 bottom_fix_height, int1 _scroll_direction);
VerticalScroll(unsigned int8 _vsp);
NormalDisplay();
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
Routines description:
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 (TRUE of FALSE).
invertDisplay: invert display colors (TRUE of FALSE).
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.
ST7735 SPI TFT Display Driver for CCS PIC C compiler download (Last Update: November 13, 2016):
The driver C file can be downloaded from the following url.
Put the C file in the CCS C driver folder of just in the project folder.
ST7735 SPI TFT Display Driver

Examples:
Some ST7735 SPI TFT library examples.
PIC12F1822 and ST7735 SPI TFT Example
Interfacing PIC18F4550 with 1.8" TFT display
ST7735 1.8" TFT display with PIC16F877A example