Newhaven Products > Graphic LCDs

Arduino MEGA 2560 + NHD240128

<< < (2/2)

tvieil:
Here is my functional code reviewed and corrected for the Arduino IDE interface


--- Code: ---// T.VIEIL - Tuesday 6th May 2014
// Sample code to use NHD240128 with an Arduino MEGA 2560


// PORTA is used for LCD lines DB0 to DB7
// PA0(22)  to PA7(29) on Arduino MEGA 2560


// control signals
#define GLCD_WR PC0 // PORTC. PC0 (37) Active LOW Write signal
#define GLCD_RD PC1 // PORTC. PC1 (36)  Active LOW Read signal
#define GLCD_CE PC2 // PORTC. PC2 (35) Active LOW Chip Select signal
#define GLCD_CD PC3 // PORTC. PC3 (34)  Register select signal.  C/D=1: Command  C/D=0: Data
#define GLCD_RESET         PC4 // PORTC. PC4 (33) Active LOW Reset signal
#define GLCD_FS PC5 // PORTC. PC5 (32) Font select signal. H: 6x8,  L: 8x8   

// MD2  pin of LCD go to Vss (GND)


void setup() {


DDRA = 0xFF;
DDRC = ((1 << GLCD_WR) | (1 << GLCD_RD) | (1 << GLCD_CE) | (1 << GLCD_CD) | (1 << GLCD_RESET) | (1 << GLCD_FS));
PORTC |= ((1 << GLCD_WR) | (1 << GLCD_RD) | (1 << GLCD_CE) | (1 << GLCD_CD) | (1 << GLCD_RESET) | (1 << GLCD_FS));


// reset /RESET
PORTC &= ~(1 << GLCD_RESET);
delayMicroseconds(1000);

// set /RESET
PORTC |= (1 << GLCD_RESET);
PORTC &=  ~(1 << GLCD_FS);
delayMicroseconds(1000);

InitLCD();
 
}


void Writecom(int Command){
 
while (!StatusLCDCheck()) {}

PORTA = Command;                 //move data to portA

PORTC  &= ~((1 << GLCD_WR) | (1 << GLCD_CE));
delayMicroseconds(1);       
PORTC |= ((1 << GLCD_WR) | (1 << GLCD_CE));


}

void Writedata(int data){
 
while (!StatusLCDCheck()) {}
 
DDRA |= 0b11111111;  //pins 0 to 7 as outputs
PORTA = data;        //move data to portA

PORTC &= ~((1 << GLCD_WR) | (1 << GLCD_CE) | (1 << GLCD_CD));                       
delayMicroseconds(1);
//delay1();
PORTC |= ((1 << GLCD_WR) | (1 << GLCD_CE) | (1 << GLCD_CD));

}

boolean StatusLCDCheck() {
// Status Check
// The Status of RA6963 can be read from the data lines.
// RD  WR  CS  C/D  SD[7:0]
// L    H   L   H  Status Word

int LCD_STATUS;
DDRA = 0x00;  //pins 0 to 7 as inputs

PORTC &=  0b11111001; //RESET /CS et /RD
delayMicroseconds(1);
//delay1();
LCD_STATUS = PINA; //READ PORTA
PORTC |= 0b00000110;  // set /CS et /RD

DDRA = 0xFF;  //pins 0 to 7 as outputs
 
if (LCD_STATUS && B00000011) {

    return true;
  }
  else
{return false;}

}



void InitLCD(){

Writedata(0xE0);
Writedata(0x01);   //graphic home address =01E0h
Writecom(0x42);    //graphic home address set

Writedata(0x1E);
Writedata(0x00);   //graphic area = 001Eh
Writecom(0x43);    //graphic area control set 

Writedata(0x00);
Writedata(0x00);   //text address = 0000h
Writecom(0x40);    //text home address set

Writedata(0x1E);
Writedata(0x00);   //text area address = 001Eh
Writecom(0x41);    //text area control set 

Writedata(0x02);
Writedata(0x00);   
Writecom(0x22);    //set offset register

Writecom(0x9C);    //set display mode = text ON + graphic ON

Writecom(0x80);    //set display mode = Internal CGROM + OR

//-------------------------------------------------------------------------------------------------
//
// Clears text area of display RAM memory
//
//-------------------------------------------------------------------------------------------------

int i;


Writedata(0x0000 & 0xFF);   //TEXT HOME
Writedata(0x0000 >> 8);
Writecom(0x24);    //SET ADDRESS POINTER

for(i = 0; i < 900; i++) //TEXT SIZE
  {
Writedata(0x00);
Writecom(0xC0);   //DATA WRITE_AND_INCREMENT
  }


//-------------------------------------------------------------------------------------------------
//
// Clears graphics area of display RAM memory
//
//-------------------------------------------------------------------------------------------------

Writedata(0x01E0 & 0xFF);   //GRAPHIC HOME
Writedata(0x01E0 >> 8);
Writecom(0x24);             //SET ADDRESS POINTER

for(i = 0; i < 3840; i++)   //GRAPHIC SIZE
  {

Writedata(0x00);
Writecom(0xC0);   //DATA WRITE_AND_INCREMENT        
  }



}


void loop()
{
 
int i;

Writedata(0x0000 & 0xFF);   //TEXT HOME
Writedata(0x0000 >> 8);
Writecom(0x24);    //SET ADDRESS POINTER

boolean Halt = false;

while(Halt == false) {


//***********************************************
//***    it will display the characters of CGROM              ***
//***********************************************
//e.g 0x21 Letter A

for (i = 0; i<128;i++) {
  Writedata(i);                     // Write a character
  Writecom(0xC0);              //Data Write and Increment ADP
 }
 
Halt = true;
}

}
--- End code ---

Michael_L:
Thank you for sharing!

Navigation

[0] Message Index

[*] Previous page

Go to full version