ST7789 Controllers

This section covers GLCD devices that use the ST7789 graphics controller. The ST7789 is a TFT LCD Single Chip Driver with 240x240 or 320x240 Resolution and 65K colors.

GCBASIC supports 65K-color mode operations.

The GCBASIC constants shown below control the configuration of the ST7789 controller.    GCBASIC supports SPI hardware and software connectivity - this is shown in the tables below.

To use the ST7789 driver simply include the following in your user code. This will initialise the driver.

    #include <glcd.h>
    #DEFINE GLCD_TYPE       GLCD_TYPE_ST7789_240_240
    //  #DEFINE GLCD_TYPE   GLCD_TYPE_ST7789_320_240

    'Pin mappings for ST7789 - these MUST be specified
    #define GLCD_DC     porta.0           'example port setting
    #define GLCD_RESET  porta.2           'example port setting
    #define GLCD_DO     porta.4           'example port setting
    #define GLCD_SCK    porta.5           'example port setting

    'Optional to use the following - please check the datasheet for the specific GLCD.
    #define GLCD_CS     porta.1           'example port setting
    #define GLCD_DI     porta.3           'example port setting

The GCBASIC constants for the interface to the controller are shown in the table below.

Constants Controls Options

GLCD_TYPE

GLCD_TYPE_ST7789_240_240 or GLCD_TYPE_ST7789_320_240

Select one option to set geometry

GLCD_DC

Specifies the output pin that is connected to Data/Command IO pin on the GLCD.

Required

GLCD_Reset

Specifies the output pin that is connected to Reset pin on the GLCD.

Required

GLCD_DO

Specifies the output pin that is connected to Data Out (GLCD in) pin on the GLCD.

Required

GLCD_SCK

Specifies the output pin that is connected to Clock (CLK) pin on the GLCD.

Required

GLCD_DI

Specifies the output pin that is connected to Data In (GLCD out) pin on the GLCD.

Optional

GLCD_CS

Specifies the output pin that is connected to Chip Select (CS) on the GLCD.

Optional

     

HWSPIMode

Specifies the speed of the SPI communications for Hardware SPI only.

Optional defaults to MASTERFAST.

Options are MASTERSLOW,
MASTER,
MASTERFAST, or
MASTERULTRAFAST for specific AVRs only.

The GCBASIC constants for control display characteristics are shown in the table below.

Constants Controls Default

GLCD_WIDTH

The width parameter of the GLCD

320 or 240

GLCD_HEIGHT

The height parameter of the GLCD

240

GLCDFontWidth

Specifies the font width of the GCBASIC font set.

6 for GCB fonts, and 5 for OLED fonts.

GLCD_OLED_FONT

Specifies the use of the optional OLED font set.

The GLCDfntDefaultsize can be set to 1 or 2 only.

GLCDfntDefaultsize= 1. A small 8 height pixel font with variable width. GLCDfntDefaultsize= 2. A larger 10 width * 16 height pixel font.

Optional

The GCBASIC commands supported for this GLCD are shown in the table below.   Always review the appropiate library for the latest full set of supported commands.

Command Purpose Example

GLCDCLS

Clear screen of GLCD

GLCDCLS [,Optional LineColour]

GLCDPrint

Print string of characters on GLCD using GCB font set

GLCDPrint( Xposition, Yposition, Stringvariable )

GLCDDrawChar

Print character on GLCD using GCB font set

GLCDDrawChar( Xposition, Yposition, CharCode [,Optional LineColour] )

GLCDDrawString

Print characters on GLCD using GCB font set

GLCDDrawString( Xposition, Yposition, Stringvariable [,Optional LineColour] )

Box

Draw a box on the GLCD to a specific size

Box ( Xposition1, Yposition1, Xposition2, Yposition2 [,Optional LineColour]

FilledBox

Draw a box on the GLCD to a specific size that is filled with the foreground colour.

FilledBox (Xposition1, Yposition1, Xposition2, Yposition2 [,Optional LineColour] )

Line

Draw a line on the GLCD to a specific length that is filled with the specific attribute.

Line ( Xposition1, Yposition1, Xposition2, Yposition2 [,Optional LineColour] )

PSet

Set a pixel on the GLCD at a specific position that is set with the specific attribute.

PSet(Xposition, Yposition, Pixel Colour)

GLCDWriteByte

Set a byte value to the controller, see the datasheet for usage.

GLCDWriteByte ( LCDByte)

GLCDReadByte

Read a byte value from the controller, see the datasheet for usage.

bytevariable = GLCDReadByte

GLCDRotate

Rotate the display

LANDSCAPE, PORTRAIT_REV, LANDSCAPE_REV and PORTRAIT are supported

ST7789_[color]

Specify color as a parameter for many GLCD commands

Color constants for this device are shown in the list below.

ReadPixel

Read the pixel color at the specified XY coordination. Returns long variable with Red, Green and Blue encoded in the lower 24 bits.

ReadPixel( Xosition , Yposition ) or ReadPixel_ST7789( Xosition , Yposition )

Any color can be defined using a valid hexidecimal word value between 0x0000 to 0xFFFF.

    TFT_BLACK       0x0000
    TFT_NAVY        0x000F
    TFT_DARKGREEN   0x03E0
    TFT_DARKCYAN    0x03EF
    TFT_MAROON      0x7800
    TFT_PURPLE      0x780F
    TFT_OLIVE       0x7BE0
    TFT_LIGHTGREY   0xC618
    TFT_DARKGREY    0x7BEF
    TFT_BLUE        0x001F
    TFT_GREEN       0x07E0
    TFT_CYAN        0x07FF
    TFT_RED         0xF800
    TFT_MAGENTA     0xF81F
    TFT_YELLOW      0xFFE0
    TFT_WHITE       0xFFFF
    TFT_ORANGE      0xFD20
    TFT_GREENYELLOW 0xAFE5
    TFT_PINK        0xF81F

This example shows how to drive a ST7789 based Graphic LCD module with the built in commands of GCBASIC.

The library support PIC, AVR and LGT - change to suit your configuration.

Example #1

    #chip LGT8F328P
    #include <LGT8F328P.h>
    #option explicit

    #include <glcd.h>
    #include <glcd_st7789.h>
    #define ST7789_HardwareSPI
    #define HWSPIMode MASTERULTRAFAST

    // Can be either pixels geometry
        #define GLCD_TYPE GLCD_TYPE_ST7789_240_240
        //#define GLCD_TYPE GLCD_TYPE_ST7789_320_240

    //Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
    #define GLCD_DC       DIGITAL_8           ' Data command line
    #define GLCD_CS       DIGITAL_10          ' Chip select line
    #define GLCD_RESET    DIGITAL_9           ' Reset line
    #define GLCD_DI       DIGITAL_12          ' Data in | MISO    - Not used therefore not really required
    #define GLCD_DO       DIGITAL_11          ' Data out | MOSI
    #define GLCD_SCK      DIGITAL_13          ' Clock Line

    #define GLCD_EXTENDEDFONTSET1
    GLCDBackground = TFT_BLACK
    GLCDCLS TFT_BLACK

    GLCDfntDefaultsize = 2

    GLCDRotate Portrait_Rev
    GLCDPrint (0,0,"Hello World",TFT_GREEN)

    GLCDRotate Portrait
    GLCDPrint (0,0,"Hello World",TFT_GREEN)

    GLCDROTATE Landscape
    GLCDPrint (0,0,"Hello World",TFT_GREEN)

    GLCDROTATE Landscape_Rev
    GLCDPrint (0,0,"Hello World",TFT_GREEN)

Example #2

This example shows how to drive a ST7789 using a PIC with PPS.

    #chip 16F15376
    #option Explicit

        #startup InitPPS, 85

        Sub InitPPS
            #ifdef ST7789_HardwareSPI

                'This #ifdef is added to enable easy change from hardware SPI (using PPS) to software PPS that just uses the port assignments shown below.

                SSP1CLKPPS = 0x1    //RC3->MSSP1:SCK1
                RC3PPS = 0x15       //RC3->MSSP1:SCK1
                RC5PPS = 0x16       //RC5->MSSP1:SDO1
                SSP1DATPPS = 0x14   //RC4->MSSP1:SDI1

            #endif
        End Sub

    ' ********************** Setup the GLCD ************************************************

        #INCLUDE <glcd.h>
        #define GLCD_TYPE        GLCD__TYPE_ST7789_240_240
        // #define GLCD_TYPE     GLCD__TYPE_ST7789_320_240


        'This is a PPS chip, so, need to make the DO/SDO & SCK match the PPS assignments
        #DEFINE GLCD_DO     portC.5
        #DEFINE GLCD_SCK    portC.3

        'Additinal pin assignments for GLCD
        #DEFINE GLCD_DC     portA.4
        #DEFINE GLCD_RESET  portA.1
        'It is optional on the ST7789 to set the GLCD_CS... therefore, here but commented out
        '#DEFINE GLCD_CS     porte.0

        'Uncomment out the next line... enable or disable the PPS!!!
        #DEFINE ST7789_HardwareSPI    ' remove/comment out if you want to use software SPI.0

    ' ********************** DEMO REALLY STARTS HERE ************************************************
    GLCDPrint(0, 0, "Test of the ST7789 Device")
    end


Example #3

This example shows how to drive a ILI3941 with the OLED fonts. Note the use of the GLCDfntDefaultSize to select the size of the OLED font in use.

    #define GLCD_OLED_FONT                'The constant is required to support OLED fonts

    GLCDfntDefaultSize = 2
    GLCDFontWidth = 5
    GLCDPrint ( 40, 0, "OLED" )
    GLCDPrint ( 0, 18, "Typ:  ST7789" )
    GLCDPrint ( 0, 34, "Size: "+ Str(GLCD_WIDTH) +" x 240" )

    GLCDfntDefaultSize = 1
    GLCDPrint(20, 56,"https://goo.gl/gjrxkp")


Example #4

This example shows how to disable the large OLED Fontset. This disables the font to reduce memory usage.

When the extended OLED fontset is disabled every character will be shown as a block character.

    #define GLCD_OLED_FONT                'The constant is required to support OLED fonts
    #define GLCD_Disable_OLED_FONT2       'The constant to disable the extended OLED fontset.

    GLCDfntDefaultSize = 2
    GLCDFontWidth = 5
    GLCDPrint ( 40, 0, "OLED" )
    GLCDPrint ( 0, 18, "Typ:  ST7789" )
    GLCDPrint ( 0, 34, "Size: "+ Str(GLCD_WIDTH) +" x 240" )

    GLCDfntDefaultSize = 1
    GLCDPrint(20, 56,"https://goo.gl/gjrxkp")


For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset

Supported in <GLCD.H>