TFT with Ardiono sample code. NHD-2.8-240320AF-CSXP-F
/*****************************************************************************
* Program for writing to Newhaven Display NHD-2.8-240320AF-CSXP-FCTP with ST7789S controller.
* This code is written for the Arduino Due (AT91SAM3X8E) in 8080 Mode 8-Bit Parallel Interface
*
* Newhaven Display invests time and resources providing this open source code,
* Please support Newhaven Display by purchasing products from Newhaven Display!
*
* Copyright: M Arce (c) 2020, Newhaven Display International
*
* This code is provided as an example only and without any warranty by Newhaven Display.
* Newhaven Display accepts no responsibility for any issues resulting from its use.
* The developer of the final application incorporating any parts of this
* sample code is responsible for ensuring its safe and correct operation
* and for any consequences resulting from its use.
* See the GNU General Public License for more details.
*****************************************************************************/
/****************************************************************************
* This program cycles thru the colors red,green,blue,white, and black.
------------------------------------------------------------------------------
Arduino TFT Display |
------------------------------------------------------------------------------
GND 1 GND - Ground |
2-6 NC - No Connect |
2.8V 7 VDD - Supply Voltage for LCD (2.8v) |
2.8V 8 I0VDD - Supply Voltage for Logic (Tie to Vdd) |
9 NC - No Connect |
GND 10 CS - Active LOW Chip Select signal(can tie to GND) |
10 11 RS - D/C Data =1 , Command =0 |
11 12 WR - Active LOW Write signal |
12 13 RD - Active LOW Read signal |
14-21 - No Connect |
2 22 DB0 |
3 23 DB1 |
4 24 DB2 |
5 25 DB3 |
6 26 DB4 |
7 27 DB5 |
8 28 DB6 |
9 29 DB7 |
3.3V 30 RES |
3.3V 31 IM0 = 1 HIGH for 8080-II |
32 No Connect |
GND 33 Ground |
GND 34 LED-K1 Backlight Cathode |
GND 35 LED-K2 Backlight Cathode |
GND 36 LED-K3 Backlight Cathode |
GND 37 LED-K4 Backlight Cathode |
3.1V 38 LED-A Backlight Anode(100mA@ 3.1V) |
GND 39 Ground |
40 No Connect |
------------------------------------------------------------------------------
******************************************************************************/
#define RS 10 /* RS signal connected to Arduino digital pin 10*/
#define WR 11 /* WR signal connected to Arduino digital pin 11*/
#define RD 12 /* RD signal connected to Arduino digital pin 12*/
void setup(){ /*Initialization*/
Init_Ports(); /*Initialize Ports*/
Init_LCD(); /*Initialize TFT*/
}
void loop(){ /*Update screen based on read input*/
Display_Colors();
}
void comm_out(unsigned char c){ /*Send Command*/
digitalWrite(RS, LOW);
for(int i=7;i>=0;i--){
if(bitRead(c,i)==0 ){
digitalWrite(i+2,LOW);
}
else {
digitalWrite(i+2,HIGH);
}
}
digitalWrite(WR, LOW);
digitalWrite(WR, HIGH);
}
void data_out(unsigned char d){ /*Send Data*/
digitalWrite(RS, HIGH);
for(int i=7;i>=0;i--){
if(bitRead(d,i)==0 ){
digitalWrite(i+2,LOW);
}
else {
digitalWrite(i+2,HIGH);
}
}
digitalWrite(WR, LOW);
digitalWrite(WR, HIGH);
}
void Init_Ports(){ /*Initialize Ports*/
pinMode(RS,OUTPUT); /*Arduino Digital Pin 10*/
pinMode(WR,OUTPUT); /*Arduino Digital Pin 11*/
pinMode(RD,OUTPUT); /*Arduino Digital Pin 12*/
pinMode(2, OUTPUT); /*TFT DB0*/
pinMode(3, OUTPUT); /*TFT DB1*/
pinMode(4, OUTPUT); /*TFT DB2*/
pinMode(5, OUTPUT); /*TFT DB3*/
pinMode(6, OUTPUT); /*TFT DB4*/
pinMode(7, OUTPUT); /*TFT DB5*/
pinMode(8, OUTPUT); /*TFT DB6*/
pinMode(9, OUTPUT); /*TFT DB7*/
}
void Init_LCD(){ /*Initialize TFT*/
digitalWrite(RD, HIGH);
digitalWrite(WR, LOW);
comm_out(0x28); /*display off*/
comm_out(0x11); /*exit SLEEP mode*/
delay(100);
comm_out(0x36); /*MADCTL: memory data access control*/
data_out(0x10); /*refresh from bottom to top. */
comm_out(0x3A); /*COLMOD: Interface Pixel format *** 65K-colors in 16bit/pixel(5-6-5)...*/
data_out(0x55); /*format when using 16-bit interface to allow 1-byte per pixel*/
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(0x21);
comm_out(0x29); /*display ON 0 high 1*/
delay(10);
}
void Set_XY_Address(){
comm_out(0x2A); /*Set x column */
data_out(0x00);
data_out(0x00);
data_out(0x00);
data_out(0xEF);
comm_out(0x2B); /*Set y row */
data_out(0x00);
data_out(0x00);
data_out(0x01);
data_out(0x3F);
comm_out(0x2C); /*command to begin writing to frame memory */
}
void Display_Colors(){ /*fill screen with white pixels*/
unsigned int i;
Set_XY_Address();
for(i=0;i<76800;i++) /*fill screen with red pixels*/
{
data_out(0xF8);
data_out(0x00);
}
Set_XY_Address();
for(i=0;i<76800;i++) /*fill screen with green pixels*/
{
data_out(0x07);
data_out(0xe0);
}
Set_XY_Address();
for(i=0;i<76800;i++) /*fill screen with blue pixels*/
{
data_out(0x00);
data_out(0x1f);
}
Set_XY_Address();
for(i=0;i<76800;i++) /*fill screen with white pixels*/
{
data_out(0xFF);
data_out(0xFF);
}
Set_XY_Address();
for(i=0;i<76800;i++) /*fill screen with black pixels*/
{
data_out(0x00);
data_out(0x00);
}
}