ST7735R 160x128 TFT Display
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|
|CS||TFT_CS||A4 (pin 18)|
|AO||D/C||A2 (pin 16)|
|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.
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:
Plotting a point
Finally the point routine plots a point at specified coordinates x (from 0 to 159) and y ( 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)))