I'm not using Adafruit or any other library because I'm trying to write my own driver so that I can hook up my LCD to an expansion port instead of the SPI bus.
However, after a lot of try I still can't get the initialization right to display anything at all. I tried the Adafruit_ST7789 library with the same wiring just to be sure it's not a hardware problem and it worked. For now I'm hooking up the LCD to the SPI bus of my ESP8266. I'm not sure in the end whether it is the initialization, could be the SPI write function, can anyone tell what I did wrong?
Thank you in advance!
#include <SPI.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define TFT_WIDTH 135
#define TFT_HEIGHT 240
void setup() {
Serial.begin(115200);
SPI.begin();
pinMode(TFT_CS, OUTPUT);
pinMode(TFT_DC, OUTPUT);
pinMode(TFT_RST, OUTPUT);
digitalWrite(TFT_CS, HIGH);
digitalWrite(TFT_RST, HIGH);
delay(100);
digitalWrite(TFT_RST, LOW);
delay(100);
digitalWrite(TFT_RST, HIGH);
initDisplay();
clearScreen();
drawPixel(10, 10, 0xF800);
}
void loop() {
}
void initDisplay() {
digitalWrite(TFT_CS, LOW);
// SWRESET
writeCommand(0x01);
delay(150);
// COLMOD
writeCommand(0x3A);
writeData(0x55);
delay(10);
// INVON
writeCommand(0x21);
delay(10);
// SLPOUT
writeCommand(0x11);
delay(10);
// NORON
writeCommand(0x13);
delay(10);
// DISPON
writeCommand(0x29);
delay(10);
// MADCTL
writeCommand(0x36);
writeData(0x08);
digitalWrite(TFT_CS, HIGH);
}
void clearScreen() {
digitalWrite(TFT_CS, LOW);
setAddrWindow(0, 0, TFT_WIDTH - 1, TFT_HEIGHT - 1);
for (int i = 0; i < TFT_WIDTH * TFT_HEIGHT; i++) {
writeColor(0x0000);
}
digitalWrite(TFT_CS, HIGH);
}
void drawPixel(int x, int y, uint16_t color) {
digitalWrite(TFT_CS, LOW);
setAddrWindow(x, y, x, y);
writeColor(color);
digitalWrite(TFT_CS, HIGH);
}
void setAddrWindow(int x0, int y0, int x1, int y1) {
writeCommand(0x2A); // CASET (Column Address Set)
writeData(x0 >> 8);
writeData(x0 & 0xFF);
writeData(x1 >> 8);
writeData(x1 & 0xFF);
writeCommand(0x2B); // RASET (Row Address Set)
writeData(y0 >> 8);
writeData(y0 & 0xFF);
writeData(y1 >> 8);
writeData(y1 & 0xFF);
writeCommand(0x2C); // RAMWR (Memory Write)
}
void writeCommand(uint8_t cmd) {
digitalWrite(TFT_DC, LOW);
SPI.transfer(cmd);
digitalWrite(TFT_DC, HIGH);
}
void writeData(uint8_t data) {
SPI.transfer(data);
}
void writeColor(uint16_t color) {
SPI.transfer(color >> 8);
SPI.transfer(color & 0xFF);
}
