Sample code for NHD-1.8-128160EF - SPI.
//---------------------------------------------------------
/*
(c)2019 Parham Keshavarzi - 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 3 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.
*/
//---------------------------------------------------------
/*******************************************************************************/
void setup()
{
DDRB = 0xFF; //Enable All outputs on PortB
PORTB = 0x00;
DDRD = 0xFF; //Enable All outputs on PortD
PORTD = 0x00;
DDRD |= (1<<DDD6); //SDIO = 1
DDRD |= (1<<DDD7); //SCL = 1
digitalWrite(RST, LOW);
delay(150);
digitalWrite(RST, HIGH);
delay(150);
command(0x11); //exit SLEEP mode
delay(100);
command(0x28); //display off
command(0x26); //select gamma curve
data(0x04);
command(0xB1); //frame rate control
data(0x0A);
data(0x14);
command(0xC0); //power control 1
data(0x0A);
data(0x00);
command(0xC1); //power control 2
data(0x02);
command(0xC5); //VCOM control 1
data(0x2F);
data(0x3E);
command(0xC7); //VCOM control 2
data(0x40);
command(0x2A); //column address set
data(0x00);
data(0x00); //start 0x0000
data(0x00);
data(0x7F); //end 0x007F
command(0x2B); //page address set
data(0x00);
data(0x00); //start 0x0000
data(0x00);
data(0x9F); //end 0x009F
command(0x36); //memory access control
data(0xC0); //C0 = RGB; C8 = BGR
command(0x3A); //pixel format = 18 bit per pixel
data(0x06);
command(0x29); //display ON
delay(10);
}
/*3-line Serial Interface*/
/****************************************************
* Functions *
*****************************************************/
void command(unsigned char c) //9-bit Transmission
{
digitalWrite(CS, LOW); ///CS = 0
PORTD &= ~(1<<PORTD6); //SDIO = 0 (Set D/C bit LOW for Command)
PORTD &= ~(1<<PORTD7); //SCL = 0
PORTD |=(1<< PORTD7); //SCL = 1
PORTD &= ~(1<<PORTD7); //SCL = 0
for (int i=0;i<8;i++)
{
if((c & 0x80)== 0x80) //Send MSB
PORTD |=(1<<PORTD6); //SDIO = 1
else
PORTD &= ~(1<<PORTD6); //SDIO = 0
c = (c<<1);
PORTD &= ~(1<<PORTD7); //SCL = 0
PORTD |=(1<< PORTD7); //SCL = 1
PORTD &= ~(1<<PORTD7); //SCL = 0
}
digitalWrite(CS, HIGH); ///CS = 1
}
void data(unsigned char d) //9-bit Transmission
{
digitalWrite(CS, LOW); ///CS = 0
PORTD |=(1<<PORTD6); //SDIO = 1 (Set D/C bit HIGH for Data)
PORTD &= ~(1<<PORTD7); //SCL = 0
PORTD |=(1<< PORTD7); //SCL = 1
PORTD &= ~(1<<PORTD7); //SCL = 0
for (int i=0;i<8;i++)
{
if((d & 0x80)== 0x80) //Send MSB
PORTD |=(1<<PORTD6); //SDIO = 1
else
PORTD &= ~(1<<PORTD6); //SDIO = 0
d = (d<<1);
PORTD &= ~(1<<PORTD7); //SCL = 0
PORTD |=(1<< PORTD7); //SCL = 1
PORTD &= ~(1<<PORTD7); //SCL = 0
}
digitalWrite(CS, HIGH); ///CS = 1
}
/*4-line Serial Interface*/
/****************************************************
* Functions *
*****************************************************/
void command(unsigned char c) //8-bit Transmission
{
digitalWrite(CS, LOW); ///CS = 0
digitalWrite(DC, LOW); //D/C = 0
for (int i=0;i<8;i++)
{
if((c & 0x80)== 0x80) //Send MSB
PORTD |=(1<<PORTD6); //SDIO = 1
else
PORTD &= ~(1<<PORTD6); //SDIO = 0
c = (c<<1);
PORTD &= ~(1<<PORTD7); //SCL = 0
PORTD |=(1<< PORTD7); //SCL = 1
PORTD &= ~(1<<PORTD7); //SCL = 0
}
digitalWrite(CS, HIGH); ///CS = 1
}
void data(unsigned char d) //8-bit Transmission
{
digitalWrite(CS, LOW); ///CS = 0
digitalWrite(DC, HIGH); //D/C = 1
for (int i=0;i<8;i++)
{
if((d & 0x80)== 0x80) //Send MSB
PORTD |=(1<<PORTD6); //SDIO = 1
else
PORTD &= ~(1<<PORTD6); //SDIO = 0
d = (d<<1);
PORTD &= ~(1<<PORTD7); //SCL = 0
PORTD |=(1<< PORTD7); //SCL = 1
PORTD &= ~(1<<PORTD7); //SCL = 0
}
digitalWrite(CS, HIGH); ///CS = 1
}