TFT display sample code for NHD-2.4-240320CF-CTXI
//---------------------------------------------------------
/*
NHD_2_4_240320CF_CTXI_mega.ino
Program for writing to Newhaven Display 2.4" TFT with ST7789S controller
(c)2016 Paul Bartek - Newhaven Display International, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
//---------------------------------------------------------
// The 8 bit data bus is connected to PORTA of the Arduino Mega2560
// 5V voltage regulator on Arduino Mega has been replaced with a 3.3V regulator to provide 3.3V logic
int RS = 30; // RS signal connected to Arduino digital pin 30
int WR = 31; // /WR signal connected to Arduino digital pin 31
int RD = 32; // /RD signal connected to Arduino digital pin 32
int RES = 33; // /RES signal connected to Arduino digital pin 33
// /CS signal tied to GND
// IM0 signal tied to VDD
void comm_out(unsigned char c)
{
digitalWrite(RS, LOW);
PORTA = c;
digitalWrite(WR, LOW);
digitalWrite(WR, HIGH);
}
void data_out(unsigned char d)
{
digitalWrite(RS, HIGH);
PORTA = d;
digitalWrite(WR, LOW);
digitalWrite(WR, HIGH);
}
void disp()
{
unsigned int i;
comm_out(0x2C); //command to begin writing to frame memory
for(i=0;i<38400;i++) //fill screen with blue pixels
{
data_out(0x00);
data_out(0x1F);
data_out(0x00);
data_out(0x1F);
}
for(i=0;i<38400;i++) //fill screen with green pixels
{
data_out(0x07);
data_out(0xE0);
data_out(0x07);
data_out(0xE0);
}
for(i=0;i<38400;i++) //fill screen with red pixels
{
data_out(0xF8);
data_out(0x00);
data_out(0xF8);
data_out(0x00);
}
for(i=0;i<38400;i++) //fill screen with black pixels
{
data_out(0x00);
data_out(0x00);
data_out(0x00);
data_out(0x00);
}
for(i=0;i<38400;i++) //fill screen with white pixels
{
data_out(0xFF);
data_out(0xFF);
data_out(0xFF);
data_out(0xFF);
}
}
void setup()
{
DDRC = 0xFF;
PORTC = 0x00;
DDRA = 0xFF;
PORTA = 0x00;
digitalWrite(RD, HIGH);
digitalWrite(WR, LOW);
digitalWrite(RES, LOW);
delay(250);
digitalWrite(RES, HIGH);
delay(250);
comm_out(0x28); //display off
comm_out(0x11); //exit SLEEP mode
delay(100);
comm_out(0x36); //MADCTL: memory data access control
data_out(0x80);
//comm_out(0x3A); //COLMOD: Interface Pixel format *** 262K-colors in 18bit/pixel format when using 8-bit interface to allow 3-bytes per pixel
//data_out(0x66);
comm_out(0x3A); //COLMOD: Interface Pixel format *** 65K-colors in 16bit/pixel (5-6-5) format when using 16-bit interface to allow 1-byte per pixel
data_out(0x55);
comm_out(0xB2); //PORCTRK: Porch setting
data_out(0x0C);
data_out(0x0C);
data_out(0x00);
data_out(0x33);
data_out(0x33);
comm_out(0xB7); //GCTRL: Gate Control
data_out(0x35);
comm_out(0xBB); //VCOMS: VCOM setting
data_out(0x2B);
comm_out(0xC0); //LCMCTRL: LCM Control
data_out(0x2C);
comm_out(0xC2); //VDVVRHEN: VDV and VRH Command Enable
data_out(0x01);
data_out(0xFF);
comm_out(0xC3); //VRHS: VRH Set
data_out(0x11);
comm_out(0xC4); //VDVS: VDV Set
data_out(0x20);
comm_out(0xC6); //FRCTRL2: Frame Rate control in normal mode
data_out(0x0F);
comm_out(0xD0); //PWCTRL1: Power Control 1
data_out(0xA4);
data_out(0xA1);
comm_out(0xE0); //PVGAMCTRL: Positive Voltage Gamma control
data_out(0xD0);
data_out(0x00);
data_out(0x05);
data_out(0x0E);
data_out(0x15);
data_out(0x0D);
data_out(0x37);
data_out(0x43);
data_out(0x47);
data_out(0x09);
data_out(0x15);
data_out(0x12);
data_out(0x16);
data_out(0x19);
comm_out(0xE1); //NVGAMCTRL: Negative Voltage Gamma control
data_out(0xD0);
data_out(0x00);
data_out(0x05);
data_out(0x0D);
data_out(0x0C);
data_out(0x06);
data_out(0x2D);
data_out(0x44);
data_out(0x40);
data_out(0x0E);
data_out(0x1C);
data_out(0x18);
data_out(0x16);
data_out(0x19);
comm_out(0x2A); //X address set
data_out(0x00);
data_out(0x00);
data_out(0x00);
data_out(0xEF);
comm_out(0x2B); //Y address set
data_out(0x00);
data_out(0x00);
data_out(0x01);
data_out(0x3F);
delay(10);
comm_out(0x29); //display ON
delay(10);
}
void loop()
{
disp();
delay(1000);
}