/*
*************************************************************************************************
*	  					ILI9325 262k color Graphical LCD Display Driver in Portrait
*										All Rights Reserved
* File name		: ILI9325P_8BIT.c
* Programmer 	: John Leung, TechToys Co. Hong Kong
* Web presence  : www.TechToys.com.hk
* Note			: 
* Language		: RealView Compiler Demo version from Keil
* Hardware		: LPC2103
* Date			: 23th July 2008
*************************************************************************************************
*										DESCRIPTION
*
* This module provides an interface to TY280T_240320 262k color graphical LCD module of 240*320dots
* Display controller chip is ILI9325
* 16-bit data bus mode 8080
*
* --------- LCD			MCU -----------------
* pin 40	n.c.		- NOT connected
* pin 38 	n.c. 		- NOT connected
* pin 36	LE  		- P0.3 Latch for 74HC573D
* pin 34:22	DB[7:0]		- P0.[15:8]
* pin 18	/RES		- RST of the system (MAX811)
* pin 16	RS			- P0.20
* pin 14	CS			- P0.19
* pin 12	RD			- P0.17
* pin 10	WR			- P0.18
* pin 8		n.c.		- NOT used
* pin 6		Vdd			- 3.3V
* pin 4		GND			- GND
* pin 2		EN			- P0.21
*
* Remarks: JP6 on TY280T-240320-BO should be CLOSED 
* for we are using 74HC573 for 16-bit mode
***********************************************************************************************
*/

#include "ILI9325P_8BIT.h"
#include "..\..\TMR0\tmr0.h"

/*
***********************************************************************************************
*									LOCAL FUNCTION PROTOTYPES
***********************************************************************************************
*/
void cDispSetReg(INT8U index, INT16U value);
void cDispSetAddress(INT8U addr2, INT8U addr1, INT8U addr0);

/*
*********************************************************************************************************
* Function:  void  cDispSetReg(INT8U index, INT16U value)
*
* Input: 'INT8U' index - register number
*        'INT16U' value - value to be set
*
* Return: none
*
*********************************************************************************************************
*/
void  cDispSetReg(INT8U index, INT16U value){

	SHORT_VAL Val;
	Val.Val = value;

	cDispWrCmd(index);
    cDispWrDat(Val.v[1],Val.v[0]);
}

/*
*********************************************************************************************************
*                         				DISPLAY INITIALIZATION
*
* Description : This function initializes the LCD module		
* Arguments   : none
*			
* Returns     : none
* Note		  : This function should be called once before any of the other functions
*********************************************************************************************************
*/

void cDispInit(void){

	cDispInitPort();
	
    // clear CS pin to Enable LCD 
    cDispCLR_bit.CS = 1;

	/*** Setup display for ILI9325 driver IC ***/ 
	/*** Start initial sequence ***/
    cDispSetReg(0xe3,0x3008);		//set internal timing
	cDispSetReg(0xe7,0x0012);
	cDispSetReg(0xef,0x1231);

	cDispSetReg(0x01,0x0100);		//set SS and SM bit
	cDispSetReg(0x02,0x0700);		//set 1 line inversion
	cDispSetReg(0x03,0x1030);		//set GRAM write direction and BGR=1
	cDispSetReg(0x04,0x0000);		//resize register
	cDispSetReg(0x08,0x0202);		//set the back porch and front porch
	cDispSetReg(0x09,0x0000);		//set non-display area refresh cycle ISC[3:0]
	cDispSetReg(0x0A,0x0000);		//FMARK function
	cDispSetReg(0x0C,0x0000);		//RGB interface setting
	cDispSetReg(0x0D,0x0000);		//Frame marker Position
	cDispSetReg(0x0F,0x0000);		//RGB interface Polarity

	/*** Power On sequence ***/
	cDispSetReg(0x10,0x0000);		//SAP, BT[3:0], AP, DSTB, SLP, STB
	cDispSetReg(0x11,0x0007);		//DC1[2:0], DC0[2:0], VC[2:0]
	cDispSetReg(0x12,0x0000);		//VREG1OUT voltage
	cDispSetReg(0x13,0x0000);		//VDV[4:0] for VCOM amplitude, discharge capacitor power voltage
	DelayMs(200);
	
	cDispSetReg(0x10,0x1690);		
	cDispSetReg(0x11,0x0220);
	DelayMs(50);

	cDispSetReg(0x12,0x0091);		
	DelayMs(50);

	cDispSetReg(0x13,0x1700);		//Set VDV[4:0] for VCOM amplitude
	cDispSetReg(0x29,0x001A);		//Set VCM[5:0]
	DelayMs(50);
	
	cDispSetReg(0x2B,0x000B);		//Set frame rate
	DelayMs(50);
	
	cDispSetReg(0x20,0x0000);		//set GRAM horizontal address
	cDispSetReg(0x21,0x0000);		//set GRAM vertical address
	
	/*** adjust gamma curve ***/
	cDispSetReg(0x30,0x0007);
	cDispSetReg(0x31,0x0507);
	cDispSetReg(0x32,0x0006);
	cDispSetReg(0x35,0x0001);
	cDispSetReg(0x36,0x0709);	
	cDispSetReg(0x37,0x0104);
	cDispSetReg(0x38,0x0502);
	cDispSetReg(0x39,0x0706);
	cDispSetReg(0x3C,0x0500);
	cDispSetReg(0x3D,0x000C);

	/*** Set GRAM area ***/
    cDispSetReg(0x50,0x0000);			//Horizontal Address Start Position
    cDispSetReg(0x51,0x00EF);           	//Horizontal Address end Position (239)
    cDispSetReg(0x52,0x0000);           	//Vertical Address Start Position
    cDispSetReg(0x53,0x013F);           	//Vertical Address end Position (319)
	cDispSetReg(0x60,0xA700);			//Gate scan line
	cDispSetReg(0x61,0x0001);			//NDL,VLE,REV
	cDispSetReg(0x6A,0x0000);			//set scrolling line
	
	/*** Partial Display Control***/
	cDispSetReg(0x80, 0x0000);
	cDispSetReg(0x81, 0x0000);
	cDispSetReg(0x82, 0x0000);
	cDispSetReg(0x83, 0x0000);
	cDispSetReg(0x84, 0x0000);
	cDispSetReg(0x85, 0x0000);

	/*** Panel control ***/
	cDispSetReg(0x90, 0x0010);
	cDispSetReg(0x92, 0x0000);
	cDispSetReg(0x93, 0x0003);
	cDispSetReg(0x95, 0x0110);
	cDispSetReg(0x97, 0x0000);
	cDispSetReg(0x98, 0x0000);

	cDispSetReg(0x07, 0x0173);

    // Disable LCD 
    cDispSET_bit.CS = 1;

    DelayMs(30);
}

/*
*********************************************************************************************************
*                         					WRITE ADDRESS POINTER
*
* Description : This function writes address pointer		
* Arguments   : addr0,addr1,addr2 - address bytes
*			
* Returns     : none
* Note		  : It is required to chip select the LCD before this function!
*********************************************************************************************************
*/

void cDispSetAddress(INT8U addr2, INT8U addr1, INT8U addr0)
{
	cDispWrCmd(0x20);
	cDispWrDat(0,addr0);
	cDispWrCmd(0x21);
	cDispWrDat(addr2,addr1);
	cDispWrCmd(0x22);
}

/*
*********************************************************************************************************
*                         			 CLEAR DISPLAY WITH PURE COLOR IN 16K
*
* Description : This function simply clears the display with a pure color
* Arguments   : INT16U color, in 16k mode. Color definition can be found under ili9320P_8bit.h
*			
* Returns     : note
* Note		  : This function needs low level pin toggle for CS pin; therefore it is hardware specific
*******************************************************************************************************
*/
void cDispClearDevice(INT16U color){

	long counter;
	SHORT_VAL Val;
	Val.Val = color;

    // clear  CS pin to ENABLE LCD 
    cDispCLR_bit.CS = 1;

    cDispSetAddress(0,0,0);
    for(counter=0; counter<(long)(SCREEN_HOR_SIZE)*(SCREEN_VER_SIZE); counter++){
        cDispWrDat(Val.v[1],Val.v[0]);
    }
    // set CS pin to DISABLE LCD 
    cDispSET_bit.CS = 1;

}

/*
*********************************************************************************************************
*                         			LOW LEVEL PORT INITIALIZATION
*
* Description : This function performs low level port initialization
* Arguments   : none
* Returns     : none
* Notes		  : Hardware specific
*********************************************************************************************************
*/
void cDispInitPort(void)
{
	cDispDIR_bit.DAT	= 0xFF;
	cDispDIR_bit.RS		= 1;
	cDispDIR_bit.WR		= 1;
	cDispDIR_bit.RD		= 1;
	cDispDIR_bit.CS		= 1;
	cDispDIR_bit.LED	= 1;
	cDispDIR_bit.LE		= 1;


	cDispSET_bit.CS		= 1;	//set CS, WR, RD high to start
	cDispSET_bit.WR		= 1;
	cDispSET_bit.RD		= 1;
	cDispSET_bit.LED	= 1;	//set LED EN pin high to enable backlight for TY240-240320-BO
	cDispSET_bit.LE		= 1;	//set LATCH LE pin of 74HC573 high, data latched from H->L

}

/*
*********************************************************************************************************
*                         				LOW LEVEL COMMAND WRITE TO LCD
*
* Description : This function performs low level command write to LCD
* Arguments   : (INT8U) 'cmd' 	is the command written to the LCD module
* Returns     : none
* Notes		  : Hardware specific.
*				We need to make sure JP6 of the LCD module has been shunt to enable 74HC573D
*				Normally an ARM processor would suit 16-bit addressing.
*********************************************************************************************************
*/
void cDispWrCmd(INT8U cmd)	
{
	cDispCLR_bit.RS  = 1;	//RS low for command
	cDispSET_bit.LE  = 1;	//take LE pin high prepared for latch
	cDispPIN_bit.DAT = 0x00;
	cDispCLR_bit.LE  = 1;	//LE pin low to latch the higher byte
	
 	cDispPIN_bit.DAT = cmd;
	cDispCLR_bit.WR	 = 1;  	//strobe WR here
	cDispSET_bit.WR  = 1;
}



/*
*********************************************************************************************************
*                         				LOW LEVEL DATA WRITE TO LCD
*
* Description : This function performs low level display data write to LCD
* Arguments   : (INT8U) 'byte1' 	is the high byte written to the LCD module
*				(INT8U) 'byte0'		is the lower byte
* Returns     : none
* Notes		  : Hardware specific.
*********************************************************************************************************
*/
 void cDispWrDat(INT8U byte1, INT8U byte0)	
{

	cDispSET_bit.RS  = 1;	//RS high for data
	cDispSET_bit.LE  = 1;	//take LE pin high prepared for latch
 	cDispPIN_bit.DAT = byte1;
	cDispCLR_bit.LE  = 1;	//take LE pin low to latch the higher byte

  	cDispPIN_bit.DAT = byte0;
	cDispCLR_bit.WR	 = 1;  	//strobe WR here
	cDispSET_bit.WR	 = 1;
}













