/*
*************************************************************************************************
*	  					ILI9320 262k color Graphical LCD Display Driver in Portrait
*										All Rights Reserved
* File name		: ILI9320P_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			: 13 Jan 2008
*************************************************************************************************
*										DESCRIPTION
*
* This module provides an interface to TY240_240320 262k color graphical LCD module of 240*320dots
* Display controller chip is ILI9320
* 8-bit data bus mode 8080
*
* LCD pinout function on PCB (TY240_240320_BO) summarized as below 
*
* --------- LCD			MCU -----------------
* pin 40	n.c.		- NOT used here
* pin 38 	VBATT+ 		- 3.3V supply for backlight
* pin 36	n.c.  		- NOT used here
* pin 34:22	DB[15:9]	- 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
***********************************************************************************************
*/

#include "ILI9320P_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 ILI9320 driver IC ***/ 
    cDispSetReg(0xe5,0x8000);			//start osc
	cDispSetReg(0x00,0x0001);
    DelayMs(10);
    cDispSetReg(0xa4,0x0001);       	//calb
    
    cDispSetReg(0x07,0x0000);			//display control(1)
	DelayMs(10);	

	/*** Display Setting ***/	
	cDispSetReg(0x01, 0x0100);			//Driver output control (1)
	cDispSetReg(0x02, 0x0700);			//LCD driving control
	cDispSetReg(0x03, 0x1030); 			//Entry mode
	cDispSetReg(0x04, 0x0000);			//Resize control
	cDispSetReg(0x08, 0x0202);			//Display control (2)
	cDispSetReg(0x09, 0x0000);			//Display control (3)

	/*** Power Control ***/
	cDispSetReg(0x07, 0x0101);			//power control 1 BT, AP
	cDispSetReg(0x17, 0x0001);
	cDispSetReg(0x10, 0x0000);
	cDispSetReg(0x11, 0x0007);			//power control 2 DC, VC
	cDispSetReg(0x12, 0x0000);			//power control 3 VRH
	cDispSetReg(0x13, 0x0000);			//power control 4 VCOM amplitude
	DelayMs(20);

  	cDispSetReg(0x10, 0x16B0);          //power control 1 BT,AP
    cDispSetReg(0x11, 0x0037);          //power control 2 DC,VC
	DelayMs(50);
    cDispSetReg(0x12, 0x013E);          //power control 3 VRH
	DelayMs(50);
    cDispSetReg(0x13, 0x1A00);          //power control 4 vcom amplitude
    cDispSetReg(0x29, 0x000F);          //power control 7 VCOMH
	DelayMs(50);
    cDispSetReg(0x20, 0x0000);          //Horizontal GRAM Address Set
    cDispSetReg(0x21, 0x0000);          //Vertical GRAM Address Set

    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, 0x2700);          //Driver Output Control 2
    cDispSetReg(0x61, 0x0001);          //Base Image Display Control
    cDispSetReg(0x6a, 0x0000);          //Base Image Display Control

    cDispSetReg(0x90, 0x0010);          //Panel Interface Control 1
    cDispSetReg(0x92, 0x0000);          //Panel Interface Control 2
    cDispSetReg(0x93, 0x0000);          //Panel Interface Control 3
    
 	/*** GAMMA Control ***/
    cDispSetReg(0x30, 0x0007);
    cDispSetReg(0x31, 0x0403);
    cDispSetReg(0x32, 0x0404);   
    cDispSetReg(0x35, 0x0002);
    cDispSetReg(0x36, 0x0707);
    cDispSetReg(0x37, 0x0606);
    cDispSetReg(0x38, 0x0106);
    cDispSetReg(0x39, 0x0007);
    cDispSetReg(0x3c, 0x0700);
    cDispSetReg(0x3d, 0x0707);

    cDispSetReg(0x07, 0x0173);

    // set CS pin to 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;

	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
}

/*
*********************************************************************************************************
*                         				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.
*********************************************************************************************************
*/
void cDispWrCmd(INT8U cmd)	
{
	cDispCLR_bit.RS  = 1;	//RS low for command

 	cDispPIN_bit.DAT = 0x00;
	cDispCLR_bit.WR	 = 1;  	//strobe WR here
	cDispSET_bit.WR  = 1;
 	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

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

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













