ST7735R 160x128 TFT Display

These routines work with a 160x128 colour TFT display based on an ST7735R driver. They are available from AliExpress [1]. Alternatively Adafruit have a very similar display [2].

TFTDisplay.jpg

The display uses an SPI interface. Connect the board to the TFT display using the following connections:

AliExpress TFT Display Adafruit TFT Display Connections
VCC VCC 3.3 V to 5 V
GND Gnd G
CS TFT_CS A4 (pin 18)
RESET RESET 3V
AO D/C A2 (pin 16)
SDA MOSI MO
SCK SCK SCK
LED LITE 3.3 V to 5 V

You can use any suitable pins for the CS and D/C connections to the display.

Defining the pins and display format

First the program defines the pins used for the CS and D/C pins:

(defvar dc 16)
(defvar cs 18)

The next statements define the main display commands used by the routines:

(defvar CASET #x2A)
(defvar RASET #x2B)
(defvar RAMWR #x2C)

Finally four statements define the display dimensions and offsets relative to the display driver chip:

(defvar yoff 0)
(defvar xoff 0)
(defvar xsize 160)
(defvar ysize 128)

By changing these variables you should be able to adapt the routines to work with other displays based on the same driver chip. 

General routines

The display supports three alternative colour modes; I used the 16-bit 565 RGB mode which uses two bytes per pixel.

The rgb routine defines a colour based on its three components, each of which should be a number from 0 to 255:

(defun rgb (r g b)
 (logior (ash (logand r #xf8) 8) (ash (logand g #xfc) 3) (ash b -3)))

The cmd routine writes a command byte to the display, followed by a variable number of data bytes:

(defun cmd (c &rest data)
  (with-spi (str cs)
    (digitalwrite dc 0)
    (write-byte c str)
    (digitalwrite dc 1)
    (dolist (d data)
      (write-byte d str))))

Initializing the display

The init routine initialises the display:

(defun init ()
  (pinmode dc t)
  (cmd #x01)      ; Software reset
  (delay 150)     ; delay 150 ms
  (cmd #x11)      ; Out of sleep mode
  (delay 500)     ; delay 500 ms
  (cmd #x3A #x05) ; Set color mode - 16-bit color
  (cmd #x29)      ; Display on
  (delay 100))

If you enter (init) you should see a random bit pattern on the display.

Clearing the display

The routine clear clears the display:

(defun clear (&optional j)
  (let ((c (if j #xFF 0)))
    (cmd CASET 0 yoff 0 (+ yoff ysize -1))
    (cmd RASET 0 xoff 0 (+ xoff xsize -1))
    (cmd #x3A #x03)
    (cmd RAMWR)
    (with-spi (str cs)
      (dotimes (p (* (/ xsize 2) ysize 3))
        (write-byte c str)))
    (cmd #x3A #x05)))

By default it clears the display to black; to clear it to white give the command:

(clear t)

Plotting a point

Finally the point routine plots a point at specified coordinates x (from 0 to 159) and ( from 0 to 127) in colour c:

(defun point (x y c)
  (cmd CASET 0 (+ yoff y) 0 (+ yoff y))
  (cmd RASET 0 (+ xoff x) 0 (+ xoff x))
  (cmd RAMWR (ash c -8) (logand c #xff)))

For demonstration programs see Plotting to a colour TFT display and Ray tracing with uLisp.


  1. ^ 1.8 inch TFT LCD Module 128x160 on AliExpress.
  2. ^ 1.8" Color TFT LCD display on Adafruit.