FSMC version: multi drive (ILI9486L, etc.) drives TFTLCD screen

The type of development board used is STM32F407 series

1, Screen related information

Resolution: 320 * 480
Size: 3.5 inches
Drive: ILI9486L
Pin interface:

We choose 16 BTS 8080 Series MCU here

IM [] is 010 and 16 data pins are used.

2, Drive settings


We can get some information through:

CSX: Chip selection signal, used to enable or disable ILI9486L chip
RESX: Reset signal
WRX: Read signal
RDX: Write signal
DB[17:0]: Parallel data bus
D/CX: data/Command selected signal,When D/CX= ' 1 'When, DB[17:0]Bit display RAM data
 Or command parameters. When D/CX= ' 0 'When, D B[17:0]Bits are commands.


From this table, we can get:

Write command: WRX At rising edge,RDX:In pull-up,D/CX In pull down
 Write data: WRX On the rising edge, RDX In the pull-up, D/CX In pull-up
 Read internal status: WRX In pull-up  RDX At rising edge D/CX In pull-up
 Read parameter display data: WRX In pull-up  RDX At rising edge D/CX In pull-up

Sequence diagram
Write timing:

It can be seen from the figure that when CSX is pulled down and selected, RESX is pulled up, D/CX is pulled down, and RDX is pulled up, WRX sends commands at the rising edge. When D/CX is pulled up and RDX is pulled up, WRX sends data at the rising edge.

Read timing

Similarly, when WRX is raised, RDX is at the rising edge, and D/CX is raised, it is in the read state.
Instruction list

Three common examples:
Column Address Set means to set the column address. First, send the command 2Ah to indicate that I want to set the column address, then SC means the column start address, and EC means the column end address.
Page Address Set is the page address setting. The operation method is similar to Column Address Set
Memory Write is to write. First send the command 2Ch, and then send the data to be written.

3, FSMC

As I said at the beginning, we use a 16 bit 8080 parallel interface. Let's take a look at the data manual of F4.

There is no 8080 interface in the interface part. How do we operate? We have used analog SPI in OLED to simulate the timing of SPI and realize the required functions. Can we simulate 8080 interface? Certainly.
We can see this explanation of FSMC in chapter 2.2.9 of the data manual

The clearing of the line drawing part shows that it supports Intel 8080 and Motorola 6800 modes and can flexibly adapt to specific LCD interfaces. So we can realize parallel 8080 through FSMC

FSMC block diagram:

FSMC_NE[4:1] - film selection
FSMC_NOE ------- read
FSMC_NWE-------- write
FSMC_A[25:0] - Address
FSMC_D[15:0] - parallel data line
Let's look at the clock tree of F4

It can be found that the internal storage is in the red box, which can be accessed directly through the AHB bus. The external storage in the purple box on the right is first transmitted to the AHB bus through the FSMC controller and then through the AHB3. Therefore, the first step is to connect the external storage with the FSMC
Storage mapping
Independent addressing: address the external memory chip independently. For example, 0x00 is the starting position of the external chip address in the external chip.
Unified addressing: in stm32, 0x00 represents an address in 32. Assuming that the internal address space of 32 is 0 ~ 1000, part of which is reserved for peripheral memory, and the internal address of 32 is useless. Assuming that 0-499 and 600-1000 are the internal addresses of 32, then 500-599 is the address of the external memory. At this time, the starting address of the external memory is no longer 0x00, but 500.
Take a look at the address mapping block diagram of F4

The above figure is the address mapping of the external storage space. The simple understanding is to mount the external chip address on the address in 32. The objective understanding means to store the address in the address. We can see that there are four addresses in external storage, bank1, bank2, bank3 and bank4. These four blocks are divided in detail in the F4 Reference Manual:

FSMC in FSMC block diagram_ NE [4:1] - film selection
First piece: 60 million h – 63ff FFH FSMC_NE1
The second piece: 64 million h – 67ff FFH FSMC_NE2
Block 3: 68000000 h – 6bff FFH FSMC_NE3
Block 4: 6c00 0000h – 6fff FFH FSMC_NE4


The mapping relationship can be seen in the figure above: storage area 1 NOR/PSRAM 1 is bank1. We only need to set bits 26 and 27 of HADDR to 00 to select bank1 and then FSMC_NE1 is selected by pulling down.
FSMC example
Having said so much, let's give an example!
There is a RAM chip such as IS62WV51216

IS62WV51216 block diagram

You can see a total of 512k addresses. One address stores 16 bits of data.
Review our FSMC block diagram

How does such a peripheral RAM connect with FSMC?

Is that it.
Next, we should pay attention to the address offset:



You can see that the mode is followed by the OType. Why is the offset address 4? One address corresponds to one byte, one byte is 8 bits, and the mode uses 32 bits, so it is 4 bytes, so the next OType needs to be offset by 4 bytes.
Take another look at the block diagram of IS62WV51216

A data is found to be 16 bits and 2 bytes. So each offset is 2 bytes.

So there's a problem?
First, an address in the CPU can only store one byte of data. When the CPU accesses an address of the external memory through the address line, if the data of an address of the external memory is just eight bits (i.e. one byte), it can be received once; However, when 16 bits of data (i.e. two bytes) are stored in an address of the external memory, the CPU starts to be silly, "I visit you once, and you give me two bytes of data!!!" What should I do?

On the CPU side, we 0x0 read FSMC_ For the two data 0x0 of a, we let the CPU 0x00 and 0x01 store FSMC_ Two data of a 0x00. The next time CPU receives FSMC from 0x02_ A. That is, offset two addresses at a time.

In order to solve this problem, the CPU can only send another address to access the same address of the external memory, receive the remaining byte, and then access the second address of the external memory, and so on... In this process, if the CPU still connects the address line to the external memory one-to-one, it can't make its second address continue to access the same address... (the explanation is too complicated, and I still haven't been particularly clear up to now). Just look at the result directly, that is, the CPU realizes two addresses in sequence to access one address of the peripheral port. Took away both bytes of data.

Analog 8080 timing


There are many kinds of FSMC timing. We need to choose the most similar one to simulate. For example, the corresponding pins are planned for this write timing

Note the D/CX pin in the figure above. The height of this pin determines whether the LCD sends commands or data. In the schematic diagram, we found that the D/CX is connected to A6, that is, the sixth in the address line. be careful!!!

Because of the offset, A6 is actually the seventh.
that is
HADDR[7]=0; Indicates a command
HADDR[7]=1; Represent data
hardware interface

FSMCIOpattern
FSMC_NE4PG12Reuse mapping FSMC
FSMC_NEWPD5Reuse mapping FSMC
FSMC_NOEPD4Reuse mapping FSMC
A6PF12Reuse mapping FSMC
LCD_BLPB15Universal push-pull
D0PD14Reuse mapping FSMC
D1PD15Reuse mapping FSMC
D2PD0Reuse mapping FSMC
D3PD1Reuse mapping FSMC
D4PE7Reuse mapping FSMC
D5PE8Reuse mapping FSMC
D6PE9Reuse mapping FSMC
D7PE10Reuse mapping FSMC
D8PE11Reuse mapping FSMC
D9PE12Reuse mapping FSMC
D10PE13Reuse mapping FSMC
D11PE14Reuse mapping FSMC
D12PE15Reuse mapping FSMC
D13PD8Reuse mapping FSMC
D14PD9Reuse mapping FSMC
D15PD10Reuse mapping FSMC

4, Code

First, we configure the IO mode as shown in the table above

/*IO to configure*/
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF|RCC_AHB1Periph_GPIOG, ENABLE);//Enable PD,PE,PF,PG clock
	RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC,ENABLE);//Enable FSMC clock
	
  GPIO_InitTypeDef  GPIO_InitStructure;
  //PB15 push-pull output, control backlight LCD_BL
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//Normal output mode
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOB, &GPIO_InitStructure);//Initialization / / PB15 push-pull output, control backlight

	//D0~D3 D13~D15 FSMC_NEW FSMC_NOE
	GPIO_InitStructure.GPIO_Pin = (3<<0)|(3<<4)|(7<<8)|(3<<14);//PD0,1,4,5,8,9,10,14,15 AF OUT
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOD, &GPIO_InitStructure);//initialization
  //D4~D12
	GPIO_InitStructure.GPIO_Pin = (0X1FF<<7);//PE7~15,AF OUT
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOE, &GPIO_InitStructure);//initialization
  //PF12,FSMC_A6
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOF, &GPIO_InitStructure);//initialization
	
  //PG12,FSMC_NE4
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOG, &GPIO_InitStructure);//initialization
//Multiplexing mapping to FSMC
    GPIO_PinAFConfig(GPIOD,GPIO_PinSource0,GPIO_AF_FSMC);//PD0,AF12
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource1,GPIO_AF_FSMC);//PD1,AF12
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource10,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource14,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_FSMC);//PD15,AF12

	GPIO_PinAFConfig(GPIOE,GPIO_PinSource7,GPIO_AF_FSMC);//PE7,AF12
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource15,GPIO_AF_FSMC);//PE15,AF12

	GPIO_PinAFConfig(GPIOF,GPIO_PinSource12,GPIO_AF_FSMC);//PF12,AF12
	GPIO_PinAFConfig(GPIOG,GPIO_PinSource12,GPIO_AF_FSMC);

Then we configure the read-write timing. It's best to call the ones configured by others and the ones given by the manufacturer. There are so many problems in our own configuration that we doubt life!!!

  //Read write timing
	FSMC_NORSRAMTimingInitTypeDef  readWriteTiming;
	FSMC_NORSRAMTimingInitTypeDef  writeTiming;
  //Read timing
	readWriteTiming.FSMC_AddressSetupTime = 0X0F;	 //Address establishment time (ADDSET) is 16 HCLK 1/168M=6ns*16=96ns
	readWriteTiming.FSMC_AddressHoldTime = 0x00;	 //Address hold time (ADDHLD) mode A is not used
	readWriteTiming.FSMC_DataSetupTime = 0x3C;			//The data storage time is 60 hclks 	= 6*60=360ns
	readWriteTiming.FSMC_BusTurnAroundDuration = 0x00;
	readWriteTiming.FSMC_CLKDivision = 0x00;
	readWriteTiming.FSMC_DataLatency = 0x00;
	readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A;	 //Mode A
  //Write timing
	writeTiming.FSMC_AddressSetupTime =0x09;	      //Address setup time (ADDSET) is 9 hclks = 54ns
	writeTiming.FSMC_AddressHoldTime = 0x00;	 //A ddress hold time
	writeTiming.FSMC_DataSetupTime = 0x08;		 //The data storage time is 6ns*9 HCLK=54ns
	writeTiming.FSMC_BusTurnAroundDuration = 0x00;
	writeTiming.FSMC_CLKDivision = 0x00;
	writeTiming.FSMC_DataLatency = 0x00;
	writeTiming.FSMC_AccessMode = FSMC_AccessMode_A;	 //Mode A

Next is the initialization of FSMC

	FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
	
	FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;//  Here we use NE4, which corresponds to BTCR[6],[7].
	FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // Do not reuse data address
	FSMC_NORSRAMInitStructure.FSMC_MemoryType =FSMC_MemoryType_SRAM;// FSMC_MemoryType_SRAM;  //SRAM
	FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//The memory data width is 16bit
	FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode =FSMC_BurstAccessMode_Disable;// FSMC_BurstAccessMode_Disable;
	FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
	FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait=FSMC_AsynchronousWait_Disable;
	FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
	FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
	FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;	//  Memory write enable
	FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
	FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // Reading and writing use different timing
	FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
	FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming; //Read write timing
	FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming;  //Write timing

	FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);  //Initialize FSMC configuration

	FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);  // Enable BANK1

Next is the application
application
After we initialize FSMC and IO, have we connected the screen. The following is to run the screen. The hardware part is built, and the next is the software part.
The first is data transmission:
How to determine whether we are transmitting commands or data? We use the value of address line A6 to judge, that is, HADDR[7], why is it offset by one bit, as mentioned above. We only need the value of A6. Is it possible to write whatever you want within the scope of NE4.
We define such an address:

//LCD address structure
typedef struct
{
  volatile uint16_t LCD_REG;//HADDR7 = A6 = 0 send command to LCD screen / / 0x6C00 007E
  volatile uint16_t LCD_RAM;//HADDR7 = A6 = 1 send data to LCD screen / / 0x6C00 0080
} LCD_TypeDef;
 //0x7e = 0111 1110 haddr [7] is 0, not 1.0 ~ 7. 7 is the eighth number
 #define LCD_BASE        ((uint32_t)(0x6C000000 | 0x0000007E))
 #define TFTLCD          ((LCD_TypeDef *) LCD_BASE)

If we have such two calls

TFTLCD->LCD_REG
TFTLCD->LCD_RAM
TFTLCD->LCD_ Is the base address 0x6C00007E called by reg
TFTLCD->LCD_ Is the address of ram call 0x6C000080

Is 0x6C00007E+2 0x6C000080
Why pay attention to LCD_ The typedef structure is uint16_t means two bytes, so add 2
0X7E : 0X0111 1110______A6=0 means write command
0X80 : 0X1000 0000______A6=1 means write data
So: TFTLCD - > LCD_ Reg is equivalent to writing commands
TFTLCD->LCD_ Ram means writing data
Let's look at the write function:

//Write command
void LCD_WR_REG(volatile uint16_t regval)
{
  regval=regval;		//When using - O2 optimization, the delay must be inserted
  TFTLCD->LCD_REG=regval;//Write the serial number of the register to be written
}
//Write LCD data
//data: value to write
void LCD_WR_DATA(volatile uint16_t data)
{
  data=data;			//When using - O2 optimization, the delay must be inserted
  TFTLCD->LCD_RAM=data;
}
//Read LCD data
//Return value: read value
uint16_t LCD_RD_DATA(void)
{
  volatile uint16_t ram;			//Prevent from being optimized
  ram=TFTLCD->LCD_RAM;
  return ram;
}
//Write register
//LCD_Reg: register address
//LCD_RegValue: data to write
void LCD_WriteReg(uint16_t LCD_Reg,uint16_t LCD_RegValue)
{
  TFTLCD->LCD_REG = LCD_Reg;		//Write the serial number of the register to be written
  TFTLCD->LCD_RAM = LCD_RegValue;//Write data
}
//Read register
//LCD_Reg: register address
//Return value: read data
uint16_t LCD_ReadReg(uint16_t LCD_Reg)
{
  LCD_WR_REG(LCD_Reg);		//Write the serial number of the register to be read
  Delay_us(5);
  return LCD_RD_DATA();		//Returns the read value
}

Next, can we start the screen according to the instructions on the screen.
This part of the code is relatively long, so I put it in the final overall engineering code. Too many versions of the process started.
For the principles and modes of display functions, pictures, characters and so on, please refer to my last OLED blog, OLED driver
The specific code is also included in the general engineering code below.

5, Engineering source code integration

bsp_lcd.c

#include "bsp_lcd.h"
#include "bsp_font.h"

//Brush color and background color of LCD
uint16_t POINT_COLOR=0x0000;	//stroke color 
uint16_t BACK_COLOR=0xFFFF;  //Background color

//Manage LCD important parameters
//The default is vertical screen
_lcd_dev lcddev;

//Write register function
//regval: register value
void LCD_WR_REG(volatile uint16_t regval)
{
  regval=regval;		//When using - O2 optimization, the delay must be inserted
  TFTLCD->LCD_REG=regval;//Write the serial number of the register to be written
}
//Write LCD data
//data: value to write
void LCD_WR_DATA(volatile uint16_t data)
{
  data=data;			//When using - O2 optimization, the delay must be inserted
  TFTLCD->LCD_RAM=data;
}
//Read LCD data
//Return value: read value
uint16_t LCD_RD_DATA(void)
{
  volatile uint16_t ram;			//Prevent from being optimized
  ram=TFTLCD->LCD_RAM;
  return ram;
}
//Write register
//LCD_Reg: register address
//LCD_RegValue: data to write
void LCD_WriteReg(uint16_t LCD_Reg,uint16_t LCD_RegValue)
{
  TFTLCD->LCD_REG = LCD_Reg;		//Write the serial number of the register to be written
  TFTLCD->LCD_RAM = LCD_RegValue;//Write data
}
//Read register
//LCD_Reg: register address
//Return value: read data
uint16_t LCD_ReadReg(uint16_t LCD_Reg)
{
  LCD_WR_REG(LCD_Reg);		//Write the serial number of the register to be read
  Delay_us(5);
  return LCD_RD_DATA();		//Returns the read value
}
//Start writing GRAM
void LCD_WriteRAM_Prepare(void)
{
  TFTLCD->LCD_REG=lcddev.wramcmd;
}
//LCD write GRAM
//RGB_Code: color value
void LCD_WriteRAM(uint16_t RGB_Code)
{
  TFTLCD->LCD_RAM = RGB_Code;//Write 16 bit GRAM
}
//The data read from ILI93xx is in GBR format, while when we write, it is in RGB format.
//Convert through this function
//c: Color value in GBR format
//Return value: color value in RGB format
uint16_t LCD_BGR2RGB(uint16_t c)
{
  uint16_t  r,g,b,rgb;
  b=(c>>0)&0x1f;
  g=(c>>5)&0x3f;
  r=(c>>11)&0x1f;
  rgb=(b<<11)+(g<<5)+(r<<0);
  return(rgb);
}
//It needs to be set when mdk -O1 time is optimized
//Delay i
void opt_delay(uint8_t i)
{
  while(i--);
}
//Read the color value of a point
//x. Y: coordinates
//Return value: the color of this point
uint16_t LCD_ReadPoint(uint16_t x,uint16_t y)
{
  uint16_t r=0,g=0,b=0;
  if(x>=lcddev.width||y>=lcddev.height)return 0;	//Out of range, return directly
  LCD_SetCursor(x,y);
  if(lcddev.id==0X9341||lcddev.id==0X9486||lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X1963)LCD_WR_REG(0X2E);//9341 / 6804 / 3510 / 1963 send read GRAM instruction
  else if(lcddev.id==0X5510)LCD_WR_REG(0X2E00);	//5510 send read GRAM instruction
  else LCD_WR_REG(0X22);      		 			//Other IC sends read GRAM instruction
  if(lcddev.id==0X9320)opt_delay(2);				//FOR 9320, delay 2us
  r=LCD_RD_DATA();								//dummy Read
  if(lcddev.id==0X1963)return r;					//Just read it directly
  opt_delay(2);
  r=LCD_RD_DATA();  		  						//Actual coordinate color
  if(lcddev.id==0X9341||lcddev.id==0X5310||lcddev.id==0X5510)		//9341/NT35310/NT35510 should be read out in two times
  {
    opt_delay(2);
    b=LCD_RD_DATA();
    g=r&0XFF;		//For 9341 / 5310 / 5510, the value of RG is read for the first time, with R in the front and G in the back, accounting for 8 bits respectively
    g<<=8;
  }
  if(lcddev.id==0X9325||lcddev.id==0X9486||lcddev.id==0X4535||lcddev.id==0X4531||lcddev.id==0XB505||lcddev.id==0XC505)return r;	//These IC s return color values directly
  else if(lcddev.id==0X9341||lcddev.id==0X5310||lcddev.id==0X5510)return (((r>>11)<<11)|((g>>10)<<5)|(b>>11));//ILI9341/NT35310/NT35510 need formula conversion
  else return LCD_BGR2RGB(r);						//Other IC
}
//LCD on display
void LCD_DisplayOn(void)
{
  if(lcddev.id==0X9341||lcddev.id==0X9486||lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X1963)LCD_WR_REG(0X29);	//Turn on display
  else if(lcddev.id==0X5510)LCD_WR_REG(0X2900);	//Turn on display
  else LCD_WriteReg(0X07,0x0173); 				 	//Turn on display
}
//LCD off display
void LCD_DisplayOff(void)
{
  if(lcddev.id==0X9341||lcddev.id==0X9486||lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X1963)LCD_WR_REG(0X28);	//Turn off display
  else if(lcddev.id==0X5510)LCD_WR_REG(0X2800);	//Turn off display
  else LCD_WriteReg(0X07,0x0);//Turn off display
}
//Set cursor position
//Xpos: abscissa
//Ypos: ordinate
void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
  if(lcddev.id==0X9341||lcddev.id==0X5310)
  {
    LCD_WR_REG(lcddev.setxcmd);
    LCD_WR_DATA(Xpos>>8);LCD_WR_DATA(Xpos&0XFF);
    LCD_WR_REG(lcddev.setycmd);
    LCD_WR_DATA(Ypos>>8);LCD_WR_DATA(Ypos&0XFF);
  }
	else if (lcddev.id==0X9486)
	{
		LCD_WR_REG(lcddev.setxcmd);
		LCD_WR_DATA(Xpos>>8);
		LCD_WR_DATA(Xpos&0XFF); 
		LCD_WR_DATA((lcddev.width-1)>>8);
		LCD_WR_DATA((lcddev.width-1)&0xFF);				
		LCD_WR_REG(lcddev.setycmd);
		LCD_WR_DATA(Ypos>>8);
		LCD_WR_DATA(Ypos&0XFF);
		LCD_WR_DATA((lcddev.height-1)>>8);
		LCD_WR_DATA((lcddev.height-1)&0xFF);
	}
	else if(lcddev.id==0X6804)
  {
    if(lcddev.dir==1)Xpos=lcddev.width-1-Xpos;//Horizontal screen processing
    LCD_WR_REG(lcddev.setxcmd);
    LCD_WR_DATA(Xpos>>8);LCD_WR_DATA(Xpos&0XFF);
    LCD_WR_REG(lcddev.setycmd);
    LCD_WR_DATA(Ypos>>8);LCD_WR_DATA(Ypos&0XFF);
  }else if(lcddev.id==0X1963)
  {
    if(lcddev.dir==0)//The x coordinate needs to be transformed
    {
      Xpos=lcddev.width-1-Xpos;
      LCD_WR_REG(lcddev.setxcmd);
      LCD_WR_DATA(0);LCD_WR_DATA(0);
      LCD_WR_DATA(Xpos>>8);LCD_WR_DATA(Xpos&0XFF);
    }else
    {
      LCD_WR_REG(lcddev.setxcmd);
      LCD_WR_DATA(Xpos>>8);LCD_WR_DATA(Xpos&0XFF);
      LCD_WR_DATA((lcddev.width-1)>>8);LCD_WR_DATA((lcddev.width-1)&0XFF);
    }
    LCD_WR_REG(lcddev.setycmd);
    LCD_WR_DATA(Ypos>>8);LCD_WR_DATA(Ypos&0XFF);
    LCD_WR_DATA((lcddev.height-1)>>8);LCD_WR_DATA((lcddev.height-1)&0XFF);

  }else if(lcddev.id==0X5510)
  {
    LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(Xpos>>8);
    LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(Xpos&0XFF);
    LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(Ypos>>8);
    LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(Ypos&0XFF);
  }else
  {
    if(lcddev.dir==1)Xpos=lcddev.width-1-Xpos;//The horizontal screen is actually turning the x,y coordinates
    LCD_WriteReg(lcddev.setxcmd, Xpos);
    LCD_WriteReg(lcddev.setycmd, Ypos);
  }
}
//Set the automatic scanning direction of LCD
//Note: other functions may be affected by this function setting (especially 9341 / 6804),
//Therefore, it is generally set to L2R_U2D is enough. If it is set to other scanning methods, the display may be abnormal
//dir:0~7, representing 8 directions (see LCD. H for specific definition)
//9320/9325/9328/4531/4535/1505/b505/5408/9341/5310/5510/1963 and other IC s have been actually tested
void LCD_Scan_Dir(uint8_t dir)
{
  uint16_t regval=0;
  uint16_t dirreg=0;
  uint16_t temp;
  if((lcddev.dir==1&&lcddev.id!=0X6804&&lcddev.id!=0X1963)||(lcddev.dir==0&&lcddev.id==0X1963))//When the screen is horizontal, do not change the scanning direction for 6804 and 1963! Change direction when vertical screen
  {
    switch(dir)//Direction conversion
    {
    case 0:dir=6;break;
    case 1:dir=7;break;
    case 2:dir=4;break;
    case 3:dir=5;break;
    case 4:dir=1;break;
    case 5:dir=0;break;
    case 6:dir=3;break;
    case 7:dir=2;break;
    }
  }
  if(lcddev.id==0x9341||lcddev.id==0X9486||lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X5510||lcddev.id==0X1963)//9341 / 6804 / 5310 / 5510 / 1963, special treatment
  {
    switch(dir)
    {
    case L2R_U2D://From left to right, from top to bottom
      regval|=(0<<7)|(0<<6)|(0<<5);
      break;
    case L2R_D2U://From left to right, from bottom to top
      regval|=(1<<7)|(0<<6)|(0<<5);
      break;
    case R2L_U2D://Right to left, top to bottom
      regval|=(0<<7)|(1<<6)|(0<<5);
      break;
    case R2L_D2U://Right to left, bottom to top
      regval|=(1<<7)|(1<<6)|(0<<5);
      break;
    case U2D_L2R://From top to bottom, from left to right
      regval|=(0<<7)|(0<<6)|(1<<5);
      break;
    case U2D_R2L://Top to bottom, right to left
      regval|=(0<<7)|(1<<6)|(1<<5);
      break;
    case D2U_L2R://From bottom to top, from left to right
      regval|=(1<<7)|(0<<6)|(1<<5);
      break;
    case D2U_R2L://Bottom to top, right to left
      regval|=(1<<7)|(1<<6)|(1<<5);
      break;
    }
    if(lcddev.id==0X5510)dirreg=0X3600;
    else dirreg=0X36;
    if((lcddev.id!=0X5310)&&(lcddev.id!=0X5510)&&(lcddev.id!=0X1963))regval|=0X08;//5310 / 5510 / 1963 BGR not required
    if(lcddev.id==0X6804)regval|=0x02;//The BIT6 of 6804 and 9341 are reversed
    LCD_WriteReg(dirreg,regval);
    if(lcddev.id!=0X1963)//1963 no coordinate processing
    {
      if(regval&0X20)
      {
        if(lcddev.width<lcddev.height)//Swap X,Y
        {
          temp=lcddev.width;
          lcddev.width=lcddev.height;
          lcddev.height=temp;
        }
      }else
      {
        if(lcddev.width>lcddev.height)//Swap X,Y
        {
          temp=lcddev.width;
          lcddev.width=lcddev.height;
          lcddev.height=temp;
        }
      }
    }
    if(lcddev.id==0X5510)
    {
      LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(0);
      LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(0);
      LCD_WR_REG(lcddev.setxcmd+2);LCD_WR_DATA((lcddev.width-1)>>8);
      LCD_WR_REG(lcddev.setxcmd+3);LCD_WR_DATA((lcddev.width-1)&0XFF);
      LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(0);
      LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(0);
      LCD_WR_REG(lcddev.setycmd+2);LCD_WR_DATA((lcddev.height-1)>>8);
      LCD_WR_REG(lcddev.setycmd+3);LCD_WR_DATA((lcddev.height-1)&0XFF);
    }else
    {
      LCD_WR_REG(lcddev.setxcmd);
      LCD_WR_DATA(0);LCD_WR_DATA(0);
      LCD_WR_DATA((lcddev.width-1)>>8);LCD_WR_DATA((lcddev.width-1)&0XFF);
      LCD_WR_REG(lcddev.setycmd);
      LCD_WR_DATA(0);LCD_WR_DATA(0);
      LCD_WR_DATA((lcddev.height-1)>>8);LCD_WR_DATA((lcddev.height-1)&0XFF);
    }
  }else
  {
    switch(dir)
    {
    case L2R_U2D://From left to right, from top to bottom
      regval|=(1<<5)|(1<<4)|(0<<3);
      break;
    case L2R_D2U://From left to right, from bottom to top
      regval|=(0<<5)|(1<<4)|(0<<3);
      break;
    case R2L_U2D://Right to left, top to bottom
      regval|=(1<<5)|(0<<4)|(0<<3);
      break;
    case R2L_D2U://Right to left, bottom to top
      regval|=(0<<5)|(0<<4)|(0<<3);
      break;
    case U2D_L2R://From top to bottom, from left to right
      regval|=(1<<5)|(1<<4)|(1<<3);
      break;
    case U2D_R2L://Top to bottom, right to left
      regval|=(1<<5)|(0<<4)|(1<<3);
      break;
    case D2U_L2R://From bottom to top, from left to right
      regval|=(0<<5)|(1<<4)|(1<<3);
      break;
    case D2U_R2L://Bottom to top, right to left
      regval|=(0<<5)|(0<<4)|(1<<3);
      break;
    }
    dirreg=0X03;
    regval|=1<<12;
    LCD_WriteReg(dirreg,regval);
  }
}
//Draw points
//x. Y: coordinates
//POINT_COLOR: the color of this point
void LCD_DrawPoint(uint16_t x,uint16_t y)
{
  LCD_SetCursor(x,y);		//Set cursor position
  LCD_WriteRAM_Prepare();	//Start writing GRAM
  TFTLCD->LCD_RAM=POINT_COLOR;
}
//Quick draw point
//x. Y: coordinates
//Color: color
void LCD_Fast_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
{
	LCD_SetCursor(x,y);		//Set cursor position (0x2A 0x2B)
  LCD_WriteRAM_Prepare();	//Start writing GRAM(0x2C)
  TFTLCD->LCD_RAM=color;  //Write display color
}
//SSD1963 backlight setting
//pwm: backlight level, 0 ~ 100 The bigger, the brighter
void LCD_SSD_BackLightSet(uint8_t pwm)
{
  LCD_WR_REG(0xBE);	//Configure PWM output
  LCD_WR_DATA(0x05);	//1 set PWM frequency
  LCD_WR_DATA((uint16_t)(pwm*2.55));//2 set PWM duty cycle
  LCD_WR_DATA(0x01);	//3 setting C
  LCD_WR_DATA(0xFF);	//4 setting D
  LCD_WR_DATA(0x00);	//5 setting E
  LCD_WR_DATA(0x00);	//6 setting F
}

//Set LCD display direction
//dir:0, vertical screen; 1. Horizontal screen
void LCD_Display_Dir(uint8_t dir)
{
  if(dir==0)			//Vertical screen
  {
    lcddev.dir=0;	//Vertical screen
    lcddev.width=240;
    lcddev.height=320;
    if(lcddev.id==0X9341||lcddev.id==0X9486||lcddev.id==0X6804||lcddev.id==0X5310)
    {
      lcddev.wramcmd=0X2C;
      lcddev.setxcmd=0X2A;
      lcddev.setycmd=0X2B;
      if(lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X9486)
      {
        lcddev.width=320;
        lcddev.height=480;
      }
    }else if(lcddev.id==0x5510)
    {
      lcddev.wramcmd=0X2C00;
      lcddev.setxcmd=0X2A00;
      lcddev.setycmd=0X2B00;
      lcddev.width=480;
      lcddev.height=800;
    }else if(lcddev.id==0X1963)
    {
      lcddev.wramcmd=0X2C;	//Sets the instruction to write to GRAM
      lcddev.setxcmd=0X2B;	//Set write X coordinate instruction
      lcddev.setycmd=0X2A;	//Set write Y coordinate instruction
      lcddev.width=480;		//Set width 480
      lcddev.height=800;		//Set height 800
    }else
    {
      lcddev.wramcmd=0X22;
      lcddev.setxcmd=0X20;
      lcddev.setycmd=0X21;
    }
  }else 				//Horizontal screen
  {
    lcddev.dir=1;	//Horizontal screen
    lcddev.width=320;
    lcddev.height=240;
    if(lcddev.id==0X9341||lcddev.id==0X9486||lcddev.id==0X5310)
    {
      lcddev.wramcmd=0X2C;
      lcddev.setxcmd=0X2A;
      lcddev.setycmd=0X2B;
    }else if(lcddev.id==0X6804)
    {
      lcddev.wramcmd=0X2C;
      lcddev.setxcmd=0X2B;
      lcddev.setycmd=0X2A;
    }else if(lcddev.id==0x5510)
    {
      lcddev.wramcmd=0X2C00;
      lcddev.setxcmd=0X2A00;
      lcddev.setycmd=0X2B00;
      lcddev.width=800;
      lcddev.height=480;
    }else if(lcddev.id==0X1963)
    {
      lcddev.wramcmd=0X2C;	//Sets the instruction to write to GRAM
      lcddev.setxcmd=0X2A;	//Set write X coordinate instruction
      lcddev.setycmd=0X2B;	//Set write Y coordinate instruction
      lcddev.width=800;		//Set width 800
      lcddev.height=480;		//Set height 480
    }else
    {
      lcddev.wramcmd=0X22;
      lcddev.setxcmd=0X21;
      lcddev.setycmd=0X20;
    }
    if(lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X9486)
    {
      lcddev.width=480;
      lcddev.height=320;
    }
  }
  LCD_Scan_Dir(DFT_SCAN_DIR);	//Default scan direction
}
//Set the window and automatically set the drawing point coordinates to the upper left corner of the window (sx,sy)
//sx,sy: window start coordinate (upper left corner)
//width,height: window width and height must be greater than 0!!
//Form size: width*height
void LCD_Set_Window(uint16_t sx,uint16_t sy,uint16_t width,uint16_t height)
{
  uint8_t hsareg,heareg,vsareg,veareg;
  uint16_t hsaval,heaval,vsaval,veaval;
  uint16_t twidth,theight;
  twidth=sx+width-1;
  theight=sy+height-1;
  if(lcddev.id==0X9341||lcddev.id==0X9486||lcddev.id==0X5310||lcddev.id==0X6804||(lcddev.dir==1&&lcddev.id==0X1963))
  {
    LCD_WR_REG(lcddev.setxcmd);
    LCD_WR_DATA(sx>>8);
    LCD_WR_DATA(sx&0XFF);
    LCD_WR_DATA(twidth>>8);
    LCD_WR_DATA(twidth&0XFF);
    LCD_WR_REG(lcddev.setycmd);
    LCD_WR_DATA(sy>>8);
    LCD_WR_DATA(sy&0XFF);
    LCD_WR_DATA(theight>>8);
    LCD_WR_DATA(theight&0XFF);
  }else if(lcddev.id==0X1963)//1963 special treatment of vertical screen
  {
    sx=lcddev.width-width-sx;
    height=sy+height-1;
    LCD_WR_REG(lcddev.setxcmd);
    LCD_WR_DATA(sx>>8);
    LCD_WR_DATA(sx&0XFF);
    LCD_WR_DATA((sx+width-1)>>8);
    LCD_WR_DATA((sx+width-1)&0XFF);
    LCD_WR_REG(lcddev.setycmd);
    LCD_WR_DATA(sy>>8);
    LCD_WR_DATA(sy&0XFF);
    LCD_WR_DATA(height>>8);
    LCD_WR_DATA(height&0XFF);
  }else if(lcddev.id==0X5510)
  {
    LCD_WR_REG(lcddev.setxcmd);LCD_WR_DATA(sx>>8);
    LCD_WR_REG(lcddev.setxcmd+1);LCD_WR_DATA(sx&0XFF);
    LCD_WR_REG(lcddev.setxcmd+2);LCD_WR_DATA(twidth>>8);
    LCD_WR_REG(lcddev.setxcmd+3);LCD_WR_DATA(twidth&0XFF);
    LCD_WR_REG(lcddev.setycmd);LCD_WR_DATA(sy>>8);
    LCD_WR_REG(lcddev.setycmd+1);LCD_WR_DATA(sy&0XFF);
    LCD_WR_REG(lcddev.setycmd+2);LCD_WR_DATA(theight>>8);
    LCD_WR_REG(lcddev.setycmd+3);LCD_WR_DATA(theight&0XFF);
  }else	//Other IC drivers
  {
    if(lcddev.dir==1)//Horizontal screen
    {
      //Window value
      hsaval=sy;
      heaval=theight;
      vsaval=lcddev.width-twidth-1;
      veaval=lcddev.width-sx-1;
    }else
    {
      hsaval=sx;
      heaval=twidth;
      vsaval=sy;
      veaval=theight;
    }
    hsareg=0X50;heareg=0X51;//Horizontal window register
    vsareg=0X52;veareg=0X53;//Vertical window register
    //Set register value
    LCD_WriteReg(hsareg,hsaval);
    LCD_WriteReg(heareg,heaval);
    LCD_WriteReg(vsareg,vsaval);
    LCD_WriteReg(veareg,veaval);
    LCD_SetCursor(sx,sy);	//Set cursor position
  }
}
//Initialize lcd
//This initialization function can initialize various ILI93XX LCD, but other functions are based on ILI9320!!!
//There is no test on other types of driver chips!
void TFTLCD_Init(void)
{
  volatile uint32_t i=0;
	/*IO to configure*/
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF|RCC_AHB1Periph_GPIOG, ENABLE);//Enable PD,PE,PF,PG clock
	RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC,ENABLE);//Enable FSMC clock
	
  GPIO_InitTypeDef  GPIO_InitStructure;
  //PB15 push-pull output, control backlight LCD_BL
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//Normal output mode
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOB, &GPIO_InitStructure);//Initialization / / PB15 push-pull output, control backlight

	//D0~D3 D13~D15 FSMC_NEW FSMC_NOE
	GPIO_InitStructure.GPIO_Pin = (3<<0)|(3<<4)|(7<<8)|(3<<14);//PD0,1,4,5,8,9,10,14,15 AF OUT
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOD, &GPIO_InitStructure);//initialization
  //D4~D12
	GPIO_InitStructure.GPIO_Pin = (0X1FF<<7);//PE7~15,AF OUT
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOE, &GPIO_InitStructure);//initialization
	
	
  //PF12,FSMC_A6
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOF, &GPIO_InitStructure);//initialization
	
  //PG12,FSMC_NE4
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplex output
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push pull output
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull up
	GPIO_Init(GPIOG, &GPIO_InitStructure);//initialization

//Multiplexing mapping to FSMC
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource0,GPIO_AF_FSMC);//PD0,AF12
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource1,GPIO_AF_FSMC);//PD1,AF12
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource10,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource14,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_FSMC);//PD15,AF12

	GPIO_PinAFConfig(GPIOE,GPIO_PinSource7,GPIO_AF_FSMC);//PE7,AF12
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_FSMC);
	GPIO_PinAFConfig(GPIOE,GPIO_PinSource15,GPIO_AF_FSMC);//PE15,AF12

	GPIO_PinAFConfig(GPIOF,GPIO_PinSource12,GPIO_AF_FSMC);//PF12,AF12
	GPIO_PinAFConfig(GPIOG,GPIO_PinSource12,GPIO_AF_FSMC);

  //Read write timing
	FSMC_NORSRAMTimingInitTypeDef  readWriteTiming;
	FSMC_NORSRAMTimingInitTypeDef  writeTiming;
  //Read timing
	readWriteTiming.FSMC_AddressSetupTime = 0X0F;	 //Address establishment time (ADDSET) is 16 HCLK 1/168M=6ns*16=96ns
	readWriteTiming.FSMC_AddressHoldTime = 0x00;	 //Address hold time (ADDHLD) mode A is not used
	readWriteTiming.FSMC_DataSetupTime = 0x3C;			//The data storage time is 60 hclks 	= 6*60=360ns
	readWriteTiming.FSMC_BusTurnAroundDuration = 0x00;
	readWriteTiming.FSMC_CLKDivision = 0x00;
	readWriteTiming.FSMC_DataLatency = 0x00;
	readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A;	 //Mode A
  //Write timing
	writeTiming.FSMC_AddressSetupTime =0x09;	      //Address setup time (ADDSET) is 9 hclks = 54ns
	writeTiming.FSMC_AddressHoldTime = 0x00;	 //A ddress hold time
	writeTiming.FSMC_DataSetupTime = 0x08;		 //The data storage time is 6ns*9 HCLK=54ns
	writeTiming.FSMC_BusTurnAroundDuration = 0x00;
	writeTiming.FSMC_CLKDivision = 0x00;
	writeTiming.FSMC_DataLatency = 0x00;
	writeTiming.FSMC_AccessMode = FSMC_AccessMode_A;	 //Mode A

	FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
	
	FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;//  Here we use NE4, which corresponds to BTCR[6],[7].
	FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // Do not reuse data address
	FSMC_NORSRAMInitStructure.FSMC_MemoryType =FSMC_MemoryType_SRAM;// FSMC_MemoryType_SRAM;  //SRAM
	FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//The memory data width is 16bit
	FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode =FSMC_BurstAccessMode_Disable;// FSMC_BurstAccessMode_Disable;
	FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
	FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait=FSMC_AsynchronousWait_Disable;
	FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
	FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
	FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;	//  Memory write enable
	FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
	FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // Reading and writing use different timing
	FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
	FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming; //Read write timing
	FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming;  //Write timing

	FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);  //Initialize FSMC configuration

	FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);  // Enable BANK1




  Delay_ms(50); // delay 50 ms
  LCD_WriteReg(0x0000,0x0001);
  Delay_ms(50); // delay 50 ms
  lcddev.id = LCD_ReadReg(0x0000);
  if(lcddev.id<0XFF||lcddev.id==0XFFFF||lcddev.id==0X9300||lcddev.id==0X9400)//The ID read is incorrect. Add lcddev Id = = 0x9300 judgment, because 9341 will be read as 9300 without being reset
  {
    //Attempt to read 9341 ID
    LCD_WR_REG(0XD3);
    lcddev.id=LCD_RD_DATA();	//dummy read
    lcddev.id=LCD_RD_DATA();	//Read 0X00
    lcddev.id=LCD_RD_DATA();   	//Read 93
    lcddev.id<<=8;
    lcddev.id|=LCD_RD_DATA();  	//Read 41
    if(lcddev.id!=0X9341 && lcddev.id!=0X9486)		//Non 9341, try 6804
    {
      LCD_WR_REG(0XBF);
      lcddev.id=LCD_RD_DATA(); 	//dummy read
      lcddev.id=LCD_RD_DATA();   	//Read back 0X01
      lcddev.id=LCD_RD_DATA(); 	//Read back 0XD0
      lcddev.id=LCD_RD_DATA();	//Read back 0X68 here
      lcddev.id<<=8;
      lcddev.id|=LCD_RD_DATA();	//Read back 0X04 here
      if(lcddev.id!=0X6804)		//It's not 6804. Try to see if it's NT35310
      {
        LCD_WR_REG(0XD4);
        lcddev.id=LCD_RD_DATA();//dummy read
        lcddev.id=LCD_RD_DATA();//Read back 0X01
        lcddev.id=LCD_RD_DATA();//Read back 0X53
        lcddev.id<<=8;
        lcddev.id|=LCD_RD_DATA();	//Read back 0X10 here
        if(lcddev.id!=0X5310)		//It's not NT35310. Try to see if it's NT35510
        {
          LCD_WR_REG(0XDA00);
          lcddev.id=LCD_RD_DATA();		//Read back 0X00
          LCD_WR_REG(0XDB00);
          lcddev.id=LCD_RD_DATA();		//Read back 0X80
          lcddev.id<<=8;
          LCD_WR_REG(0XDC00);
          lcddev.id|=LCD_RD_DATA();		//Read back 0X00
          if(lcddev.id==0x8000)lcddev.id=0x5510;//The ID read back by NT35510 is 8000H. In order to facilitate differentiation, we force it to be set to 5510
          if(lcddev.id!=0X5510)			//It's not NT5510. Try to see if it's SSD1963
          {
            LCD_WR_REG(0XA1);
            lcddev.id=LCD_RD_DATA();
            lcddev.id=LCD_RD_DATA();	//Read back 0X57
            lcddev.id<<=8;
            lcddev.id|=LCD_RD_DATA();	//Read back 0X61
            if(lcddev.id==0X5761)lcddev.id=0X1963;//The ID read back by SSD1963 is 5761H. In order to facilitate differentiation, we force it to 1963
          }
        }
      }
    }
  }
  if(lcddev.id==0X9341||lcddev.id==0X5310||lcddev.id==0X5510||lcddev.id==0X1963)//If it is these IC s, set the WR timing to the fastest
  {
    //Reconfigure the timing of writing the timing control register
    FSMC_Bank1E->BWTR[6]&=~(0XF<<0);//Address setup time (ADDSET) reset
    FSMC_Bank1E->BWTR[6]&=~(0XF<<8);//Data saving time reset
    FSMC_Bank1E->BWTR[6]|=3<<0;		//Address setup time (ADDSET) is 3 hclks = 18ns
    FSMC_Bank1E->BWTR[6]|=2<<8; 	//The data storage time (DATAST) is 6ns*3 hclks = 18ns
  }else if(lcddev.id==0X6804||lcddev.id==0XC505)	//6804/C505 speed can't go up, so it has to be reduced
  {
    //Reconfigure the timing of writing the timing control register
    FSMC_Bank1E->BWTR[6]&=~(0XF<<0);//Address setup time (ADDSET) reset
    FSMC_Bank1E->BWTR[6]&=~(0XF<<8);//Data saving time reset
    FSMC_Bank1E->BWTR[6]|=10<<0;	//Address setup time (ADDSET) is 10 hclks = 60ns
    FSMC_Bank1E->BWTR[6]|=12<<8; 	//The data storage time (DATAST) is 6ns*13 HCLK=78ns
  }
//  printf(" TFTLCD ID:%x\r\n",lcddev.id); // Print LCD ID
  if(lcddev.id==0X9341)	//9341 initialization
  {
    LCD_WR_REG(0xCF);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC1);
    LCD_WR_DATA(0X30);
    LCD_WR_REG(0xED);
    LCD_WR_DATA(0x64);
    LCD_WR_DATA(0x03);
    LCD_WR_DATA(0X12);
    LCD_WR_DATA(0X81);
    LCD_WR_REG(0xE8);
    LCD_WR_DATA(0x85);
    LCD_WR_DATA(0x10);
    LCD_WR_DATA(0x7A);
    LCD_WR_REG(0xCB);
    LCD_WR_DATA(0x39);
    LCD_WR_DATA(0x2C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x34);
    LCD_WR_DATA(0x02);
    LCD_WR_REG(0xF7);
    LCD_WR_DATA(0x20);
    LCD_WR_REG(0xEA);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_REG(0xC0);    //Power control
    LCD_WR_DATA(0x1B);   //VRH[5:0]
    LCD_WR_REG(0xC1);    //Power control
    LCD_WR_DATA(0x01);   //SAP[2:0];BT[3:0]
    LCD_WR_REG(0xC5);    //VCM control
    LCD_WR_DATA(0x30); 	 //3F
    LCD_WR_DATA(0x30); 	 //3C
    LCD_WR_REG(0xC7);    //VCM control2
    LCD_WR_DATA(0XB7);
    LCD_WR_REG(0x36);    // Memory Access Control
    LCD_WR_DATA(0x48);
    LCD_WR_REG(0x3A);
    LCD_WR_DATA(0x55);
    LCD_WR_REG(0xB1);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x1A);
    LCD_WR_REG(0xB6);    // Display Function Control
    LCD_WR_DATA(0x0A);
    LCD_WR_DATA(0xA2);
    LCD_WR_REG(0xF2);    // 3Gamma Function Disable
    LCD_WR_DATA(0x00);
    LCD_WR_REG(0x26);    //Gamma curve selected
    LCD_WR_DATA(0x01);
    LCD_WR_REG(0xE0);    //Set Gamma
    LCD_WR_DATA(0x0F);
    LCD_WR_DATA(0x2A);
    LCD_WR_DATA(0x28);
    LCD_WR_DATA(0x08);
    LCD_WR_DATA(0x0E);
    LCD_WR_DATA(0x08);
    LCD_WR_DATA(0x54);
    LCD_WR_DATA(0XA9);
    LCD_WR_DATA(0x43);
    LCD_WR_DATA(0x0A);
    LCD_WR_DATA(0x0F);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_REG(0XE1);    //Set Gamma
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x15);
    LCD_WR_DATA(0x17);
    LCD_WR_DATA(0x07);
    LCD_WR_DATA(0x11);
    LCD_WR_DATA(0x06);
    LCD_WR_DATA(0x2B);
    LCD_WR_DATA(0x56);
    LCD_WR_DATA(0x3C);
    LCD_WR_DATA(0x05);
    LCD_WR_DATA(0x10);
    LCD_WR_DATA(0x0F);
    LCD_WR_DATA(0x3F);
    LCD_WR_DATA(0x3F);
    LCD_WR_DATA(0x0F);
    LCD_WR_REG(0x2B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x01);
    LCD_WR_DATA(0x3f);
    LCD_WR_REG(0x2A);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xef);
    LCD_WR_REG(0x11); //Exit Sleep
    Delay_ms(120);
    LCD_WR_REG(0x29); //display on
  }
	else if (lcddev.id==0x9486)
	{
		LCD_WR_REG(0XF2);
		LCD_WR_DATA(0x18);
		LCD_WR_DATA(0xA3);
		LCD_WR_DATA(0x12);
		LCD_WR_DATA(0x02);
		LCD_WR_DATA(0XB2);
		LCD_WR_DATA(0x12);
		LCD_WR_DATA(0xFF);
		LCD_WR_DATA(0x10);
		LCD_WR_DATA(0x00);
		LCD_WR_REG(0XF8);
		LCD_WR_DATA(0x21);
		LCD_WR_DATA(0x04);
		LCD_WR_REG(0XF9);
		LCD_WR_DATA(0x00);
		LCD_WR_DATA(0x08);
		LCD_WR_REG(0x36);
		LCD_WR_DATA(0x08);   //Set RGB, screen with cable arrangement
//		LCD_WR_DATA(0x00);   // Set RGB, screen without cable arrangement
		LCD_WR_REG(0x3A);
		LCD_WR_DATA(0x05);   //Set 16 bit BPP
		LCD_WR_REG(0xB4);
		LCD_WR_DATA(0x01);//0x00
		LCD_WR_REG(0xB6);
		LCD_WR_DATA(0x02);
		LCD_WR_DATA(0x22);
		LCD_WR_REG(0xC1);
		LCD_WR_DATA(0x41);
		LCD_WR_REG(0xC5);
		LCD_WR_DATA(0x00);
		LCD_WR_DATA(0x07);//0X18
		LCD_WR_REG(0xE0);
		LCD_WR_DATA(0x0F);
		LCD_WR_DATA(0x1F);
		LCD_WR_DATA(0x1C);
		LCD_WR_DATA(0x0C);
		LCD_WR_DATA(0x0F);
		LCD_WR_DATA(0x08);
		LCD_WR_DATA(0x48);
		LCD_WR_DATA(0x98);
		LCD_WR_DATA(0x37);
		LCD_WR_DATA(0x0A);
		LCD_WR_DATA(0x13);
		LCD_WR_DATA(0x04);
		LCD_WR_DATA(0x11);
		LCD_WR_DATA(0x0D);
		LCD_WR_DATA(0x00);
		LCD_WR_REG(0xE1);
		LCD_WR_DATA(0x0F);
		LCD_WR_DATA(0x32);
		LCD_WR_DATA(0x2E);
		LCD_WR_DATA(0x0B);
		LCD_WR_DATA(0x0D);
		LCD_WR_DATA(0x05);
		LCD_WR_DATA(0x47);
		LCD_WR_DATA(0x75);
		LCD_WR_DATA(0x37);
		LCD_WR_DATA(0x06);
		LCD_WR_DATA(0x10);
		LCD_WR_DATA(0x03);
		LCD_WR_DATA(0x24);
		LCD_WR_DATA(0x20);
		LCD_WR_DATA(0x00);
		LCD_WR_REG(0x11);   //Quit sleep
		Delay_ms(120);
		LCD_WR_REG(0x29);   //Turn on display
	}
	else if(lcddev.id==0x6804) //6804 initialization
  {
    LCD_WR_REG(0X11);
    Delay_ms(20);
    LCD_WR_REG(0XD0);//VCI1  VCL  VGH  VGL DDVDH VREG1OUT power amplitude setting
    LCD_WR_DATA(0X07);
    LCD_WR_DATA(0X42);
    LCD_WR_DATA(0X1D);
    LCD_WR_REG(0XD1);//VCOMH VCOM_AC amplitude setting
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X1a);
    LCD_WR_DATA(0X09);
    LCD_WR_REG(0XD2);//Operational Amplifier Circuit Constant Current Adjust , charge pump frequency setting
    LCD_WR_DATA(0X01);
    LCD_WR_DATA(0X22);
    LCD_WR_REG(0XC0);//REV SM GS
    LCD_WR_DATA(0X10);
    LCD_WR_DATA(0X3B);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X02);
    LCD_WR_DATA(0X11);

    LCD_WR_REG(0XC5);// Frame rate setting = 72HZ  when setting 0x03
    LCD_WR_DATA(0X03);

    LCD_WR_REG(0XC8);//Gamma setting
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X25);
    LCD_WR_DATA(0X21);
    LCD_WR_DATA(0X05);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X0a);
    LCD_WR_DATA(0X65);
    LCD_WR_DATA(0X25);
    LCD_WR_DATA(0X77);
    LCD_WR_DATA(0X50);
    LCD_WR_DATA(0X0f);
    LCD_WR_DATA(0X00);

    LCD_WR_REG(0XF8);
    LCD_WR_DATA(0X01);

    LCD_WR_REG(0XFE);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X02);

    LCD_WR_REG(0X20);//Exit invert mode

    LCD_WR_REG(0X36);
    LCD_WR_DATA(0X08);//It was a

    LCD_WR_REG(0X3A);
    LCD_WR_DATA(0X55);//16 bit mode
    LCD_WR_REG(0X2B);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X01);
    LCD_WR_DATA(0X3F);

    LCD_WR_REG(0X2A);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X00);
    LCD_WR_DATA(0X01);
    LCD_WR_DATA(0XDF);
    Delay_ms(120);
    LCD_WR_REG(0X29);
  }else if(lcddev.id==0x5310)
  {
    LCD_WR_REG(0xED);
    LCD_WR_DATA(0x01);
    LCD_WR_DATA(0xFE);

    LCD_WR_REG(0xEE);
    LCD_WR_DATA(0xDE);
    LCD_WR_DATA(0x21);

    LCD_WR_REG(0xF1);
    LCD_WR_DATA(0x01);
    LCD_WR_REG(0xDF);
    LCD_WR_DATA(0x10);

    //VCOMvoltage//
    LCD_WR_REG(0xC4);
    LCD_WR_DATA(0x8F);	  //5f

    LCD_WR_REG(0xC6);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xE2);
    LCD_WR_DATA(0xE2);
    LCD_WR_DATA(0xE2);
    LCD_WR_REG(0xBF);
    LCD_WR_DATA(0xAA);

    LCD_WR_REG(0xB0);
    LCD_WR_DATA(0x0D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x0D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x11);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x19);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x21);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x2D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x5D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x5D);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB1);
    LCD_WR_DATA(0x80);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x8B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x96);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x02);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x03);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB3);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB4);
    LCD_WR_DATA(0x8B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x96);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA1);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB5);
    LCD_WR_DATA(0x02);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x03);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x04);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB6);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB7);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3F);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x5E);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x64);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x8C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xAC);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xDC);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x70);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x90);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xEB);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xDC);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xB8);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xBA);
    LCD_WR_DATA(0x24);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC1);
    LCD_WR_DATA(0x20);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x54);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xFF);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC2);
    LCD_WR_DATA(0x0A);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x04);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC3);
    LCD_WR_DATA(0x3C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3A);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x39);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x37);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x36);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x32);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x2F);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x2C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x29);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x26);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x24);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x24);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x23);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x36);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x32);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x2F);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x2C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x29);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x26);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x24);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x24);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x23);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC4);
    LCD_WR_DATA(0x62);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x05);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x84);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF0);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x18);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA4);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x18);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x50);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x0C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x17);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x95);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xE6);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC5);
    LCD_WR_DATA(0x32);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x65);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x76);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x88);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC6);
    LCD_WR_DATA(0x20);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x17);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x01);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC7);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC8);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xC9);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE0);
    LCD_WR_DATA(0x16);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x1C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x21);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x36);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x46);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x52);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x64);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x7A);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x8B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA8);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xB9);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC4);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xCA);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD9);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xE0);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE1);
    LCD_WR_DATA(0x16);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x1C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x22);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x36);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x45);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x52);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x64);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x7A);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x8B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA8);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xB9);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC4);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xCA);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD8);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xE0);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE2);
    LCD_WR_DATA(0x05);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x0B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x1B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x34);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x4F);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x61);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x79);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x88);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x97);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA6);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xB7);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC7);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD1);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD6);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xDD);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);
    LCD_WR_REG(0xE3);
    LCD_WR_DATA(0x05);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x1C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x33);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x50);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x62);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x78);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x88);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x97);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA6);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xB7);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC7);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD1);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD5);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xDD);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE4);
    LCD_WR_DATA(0x01);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x01);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x02);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x2A);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x4B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x5D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x74);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x84);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x93);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xB3);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xBE);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC4);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xCD);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD3);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xDD);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);
    LCD_WR_REG(0xE5);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x02);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x29);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x3C);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x4B);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x5D);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x74);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x84);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x93);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xA2);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xB3);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xBE);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xC4);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xCD);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xD3);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xDC);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xF3);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE6);
    LCD_WR_DATA(0x11);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x34);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x56);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x76);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x77);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x66);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x88);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xBB);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x66);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x55);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x55);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x45);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x43);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE7);
    LCD_WR_DATA(0x32);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x55);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x76);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x66);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x67);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x67);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x87);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xBB);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x77);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x56);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x23);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x33);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x45);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE8);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x87);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x88);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x77);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x66);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x88);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xAA);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0xBB);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x99);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x66);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x55);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x55);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x44);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x55);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xE9);
    LCD_WR_DATA(0xAA);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0x00);
    LCD_WR_DATA(0xAA);

    LCD_WR_REG(0xCF);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xF0);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x50);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xF3);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xF9);
    LCD_WR_DATA(0x06);
    LCD_WR_DATA(0x10);
    LCD_WR_DATA(0x29);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0x3A);
    LCD_WR_DATA(0x55);	//66

    LCD_WR_REG(0x11);
    Delay_ms(100);
    LCD_WR_REG(0x29);
    LCD_WR_REG(0x35);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0x51);
    LCD_WR_DATA(0xFF);
    LCD_WR_REG(0x53);
    LCD_WR_DATA(0x2C);
    LCD_WR_REG(0x55);
    LCD_WR_DATA(0x82);
    LCD_WR_REG(0x2c);
  }else if(lcddev.id==0x5510)
  {
    LCD_WriteReg(0xF000,0x55);
    LCD_WriteReg(0xF001,0xAA);
    LCD_WriteReg(0xF002,0x52);
    LCD_WriteReg(0xF003,0x08);
    LCD_WriteReg(0xF004,0x01);
    //AVDD Set AVDD 5.2V
    LCD_WriteReg(0xB000,0x0D);
    LCD_WriteReg(0xB001,0x0D);
    LCD_WriteReg(0xB002,0x0D);
    //AVDD ratio
    LCD_WriteReg(0xB600,0x34);
    LCD_WriteReg(0xB601,0x34);
    LCD_WriteReg(0xB602,0x34);
    //AVEE -5.2V
    LCD_WriteReg(0xB100,0x0D);
    LCD_WriteReg(0xB101,0x0D);
    LCD_WriteReg(0xB102,0x0D);
    //AVEE ratio
    LCD_WriteReg(0xB700,0x34);
    LCD_WriteReg(0xB701,0x34);
    LCD_WriteReg(0xB702,0x34);
    //VCL -2.5V
    LCD_WriteReg(0xB200,0x00);
    LCD_WriteReg(0xB201,0x00);
    LCD_WriteReg(0xB202,0x00);
    //VCL ratio
    LCD_WriteReg(0xB800,0x24);
    LCD_WriteReg(0xB801,0x24);
    LCD_WriteReg(0xB802,0x24);
    //VGH 15V (Free pump)
    LCD_WriteReg(0xBF00,0x01);
    LCD_WriteReg(0xB300,0x0F);
    LCD_WriteReg(0xB301,0x0F);
    LCD_WriteReg(0xB302,0x0F);
    //VGH ratio
    LCD_WriteReg(0xB900,0x34);
    LCD_WriteReg(0xB901,0x34);
    LCD_WriteReg(0xB902,0x34);
    //VGL_REG -10V
    LCD_WriteReg(0xB500,0x08);
    LCD_WriteReg(0xB501,0x08);
    LCD_WriteReg(0xB502,0x08);
    LCD_WriteReg(0xC200,0x03);
    //VGLX ratio
    LCD_WriteReg(0xBA00,0x24);
    LCD_WriteReg(0xBA01,0x24);
    LCD_WriteReg(0xBA02,0x24);
    //VGMP/VGSP 4.5V/0V
    LCD_WriteReg(0xBC00,0x00);
    LCD_WriteReg(0xBC01,0x78);
    LCD_WriteReg(0xBC02,0x00);
    //VGMN/VGSN -4.5V/0V
    LCD_WriteReg(0xBD00,0x00);
    LCD_WriteReg(0xBD01,0x78);
    LCD_WriteReg(0xBD02,0x00);
    //VCOM
    LCD_WriteReg(0xBE00,0x00);
    LCD_WriteReg(0xBE01,0x64);
    //Gamma Setting
    LCD_WriteReg(0xD100,0x00);
    LCD_WriteReg(0xD101,0x33);
    LCD_WriteReg(0xD102,0x00);
    LCD_WriteReg(0xD103,0x34);
    LCD_WriteReg(0xD104,0x00);
    LCD_WriteReg(0xD105,0x3A);
    LCD_WriteReg(0xD106,0x00);
    LCD_WriteReg(0xD107,0x4A);
    LCD_WriteReg(0xD108,0x00);
    LCD_WriteReg(0xD109,0x5C);
    LCD_WriteReg(0xD10A,0x00);
    LCD_WriteReg(0xD10B,0x81);
    LCD_WriteReg(0xD10C,0x00);
    LCD_WriteReg(0xD10D,0xA6);
    LCD_WriteReg(0xD10E,0x00);
    LCD_WriteReg(0xD10F,0xE5);
    LCD_WriteReg(0xD110,0x01);
    LCD_WriteReg(0xD111,0x13);
    LCD_WriteReg(0xD112,0x01);
    LCD_WriteReg(0xD113,0x54);
    LCD_WriteReg(0xD114,0x01);
    LCD_WriteReg(0xD115,0x82);
    LCD_WriteReg(0xD116,0x01);
    LCD_WriteReg(0xD117,0xCA);
    LCD_WriteReg(0xD118,0x02);
    LCD_WriteReg(0xD119,0x00);
    LCD_WriteReg(0xD11A,0x02);
    LCD_WriteReg(0xD11B,0x01);
    LCD_WriteReg(0xD11C,0x02);
    LCD_WriteReg(0xD11D,0x34);
    LCD_WriteReg(0xD11E,0x02);
    LCD_WriteReg(0xD11F,0x67);
    LCD_WriteReg(0xD120,0x02);
    LCD_WriteReg(0xD121,0x84);
    LCD_WriteReg(0xD122,0x02);
    LCD_WriteReg(0xD123,0xA4);
    LCD_WriteReg(0xD124,0x02);
    LCD_WriteReg(0xD125,0xB7);
    LCD_WriteReg(0xD126,0x02);
    LCD_WriteReg(0xD127,0xCF);
    LCD_WriteReg(0xD128,0x02);
    LCD_WriteReg(0xD129,0xDE);
    LCD_WriteReg(0xD12A,0x02);
    LCD_WriteReg(0xD12B,0xF2);
    LCD_WriteReg(0xD12C,0x02);
    LCD_WriteReg(0xD12D,0xFE);
    LCD_WriteReg(0xD12E,0x03);
    LCD_WriteReg(0xD12F,0x10);
    LCD_WriteReg(0xD130,0x03);
    LCD_WriteReg(0xD131,0x33);
    LCD_WriteReg(0xD132,0x03);
    LCD_WriteReg(0xD133,0x6D);
    LCD_WriteReg(0xD200,0x00);
    LCD_WriteReg(0xD201,0x33);
    LCD_WriteReg(0xD202,0x00);
    LCD_WriteReg(0xD203,0x34);
    LCD_WriteReg(0xD204,0x00);
    LCD_WriteReg(0xD205,0x3A);
    LCD_WriteReg(0xD206,0x00);
    LCD_WriteReg(0xD207,0x4A);
    LCD_WriteReg(0xD208,0x00);
    LCD_WriteReg(0xD209,0x5C);
    LCD_WriteReg(0xD20A,0x00);

    LCD_WriteReg(0xD20B,0x81);
    LCD_WriteReg(0xD20C,0x00);
    LCD_WriteReg(0xD20D,0xA6);
    LCD_WriteReg(0xD20E,0x00);
    LCD_WriteReg(0xD20F,0xE5);
    LCD_WriteReg(0xD210,0x01);
    LCD_WriteReg(0xD211,0x13);
    LCD_WriteReg(0xD212,0x01);
    LCD_WriteReg(0xD213,0x54);
    LCD_WriteReg(0xD214,0x01);
    LCD_WriteReg(0xD215,0x82);
    LCD_WriteReg(0xD216,0x01);
    LCD_WriteReg(0xD217,0xCA);
    LCD_WriteReg(0xD218,0x02);
    LCD_WriteReg(0xD219,0x00);
    LCD_WriteReg(0xD21A,0x02);
    LCD_WriteReg(0xD21B,0x01);
    LCD_WriteReg(0xD21C,0x02);
    LCD_WriteReg(0xD21D,0x34);
    LCD_WriteReg(0xD21E,0x02);
    LCD_WriteReg(0xD21F,0x67);
    LCD_WriteReg(0xD220,0x02);
    LCD_WriteReg(0xD221,0x84);
    LCD_WriteReg(0xD222,0x02);
    LCD_WriteReg(0xD223,0xA4);
    LCD_WriteReg(0xD224,0x02);
    LCD_WriteReg(0xD225,0xB7);
    LCD_WriteReg(0xD226,0x02);
    LCD_WriteReg(0xD227,0xCF);
    LCD_WriteReg(0xD228,0x02);
    LCD_WriteReg(0xD229,0xDE);
    LCD_WriteReg(0xD22A,0x02);
    LCD_WriteReg(0xD22B,0xF2);
    LCD_WriteReg(0xD22C,0x02);
    LCD_WriteReg(0xD22D,0xFE);
    LCD_WriteReg(0xD22E,0x03);
    LCD_WriteReg(0xD22F,0x10);
    LCD_WriteReg(0xD230,0x03);
    LCD_WriteReg(0xD231,0x33);
    LCD_WriteReg(0xD232,0x03);
    LCD_WriteReg(0xD233,0x6D);
    LCD_WriteReg(0xD300,0x00);
    LCD_WriteReg(0xD301,0x33);
    LCD_WriteReg(0xD302,0x00);
    LCD_WriteReg(0xD303,0x34);
    LCD_WriteReg(0xD304,0x00);
    LCD_WriteReg(0xD305,0x3A);
    LCD_WriteReg(0xD306,0x00);
    LCD_WriteReg(0xD307,0x4A);
    LCD_WriteReg(0xD308,0x00);
    LCD_WriteReg(0xD309,0x5C);
    LCD_WriteReg(0xD30A,0x00);

    LCD_WriteReg(0xD30B,0x81);
    LCD_WriteReg(0xD30C,0x00);
    LCD_WriteReg(0xD30D,0xA6);
    LCD_WriteReg(0xD30E,0x00);
    LCD_WriteReg(0xD30F,0xE5);
    LCD_WriteReg(0xD310,0x01);
    LCD_WriteReg(0xD311,0x13);
    LCD_WriteReg(0xD312,0x01);
    LCD_WriteReg(0xD313,0x54);
    LCD_WriteReg(0xD314,0x01);
    LCD_WriteReg(0xD315,0x82);
    LCD_WriteReg(0xD316,0x01);
    LCD_WriteReg(0xD317,0xCA);
    LCD_WriteReg(0xD318,0x02);
    LCD_WriteReg(0xD319,0x00);
    LCD_WriteReg(0xD31A,0x02);
    LCD_WriteReg(0xD31B,0x01);
    LCD_WriteReg(0xD31C,0x02);
    LCD_WriteReg(0xD31D,0x34);
    LCD_WriteReg(0xD31E,0x02);
    LCD_WriteReg(0xD31F,0x67);
    LCD_WriteReg(0xD320,0x02);
    LCD_WriteReg(0xD321,0x84);
    LCD_WriteReg(0xD322,0x02);
    LCD_WriteReg(0xD323,0xA4);
    LCD_WriteReg(0xD324,0x02);
    LCD_WriteReg(0xD325,0xB7);
    LCD_WriteReg(0xD326,0x02);
    LCD_WriteReg(0xD327,0xCF);
    LCD_WriteReg(0xD328,0x02);
    LCD_WriteReg(0xD329,0xDE);
    LCD_WriteReg(0xD32A,0x02);
    LCD_WriteReg(0xD32B,0xF2);
    LCD_WriteReg(0xD32C,0x02);
    LCD_WriteReg(0xD32D,0xFE);
    LCD_WriteReg(0xD32E,0x03);
    LCD_WriteReg(0xD32F,0x10);
    LCD_WriteReg(0xD330,0x03);
    LCD_WriteReg(0xD331,0x33);
    LCD_WriteReg(0xD332,0x03);
    LCD_WriteReg(0xD333,0x6D);
    LCD_WriteReg(0xD400,0x00);
    LCD_WriteReg(0xD401,0x33);
    LCD_WriteReg(0xD402,0x00);
    LCD_WriteReg(0xD403,0x34);
    LCD_WriteReg(0xD404,0x00);
    LCD_WriteReg(0xD405,0x3A);
    LCD_WriteReg(0xD406,0x00);
    LCD_WriteReg(0xD407,0x4A);
    LCD_WriteReg(0xD408,0x00);
    LCD_WriteReg(0xD409,0x5C);
    LCD_WriteReg(0xD40A,0x00);
    LCD_WriteReg(0xD40B,0x81);

    LCD_WriteReg(0xD40C,0x00);
    LCD_WriteReg(0xD40D,0xA6);
    LCD_WriteReg(0xD40E,0x00);
    LCD_WriteReg(0xD40F,0xE5);
    LCD_WriteReg(0xD410,0x01);
    LCD_WriteReg(0xD411,0x13);
    LCD_WriteReg(0xD412,0x01);
    LCD_WriteReg(0xD413,0x54);
    LCD_WriteReg(0xD414,0x01);
    LCD_WriteReg(0xD415,0x82);
    LCD_WriteReg(0xD416,0x01);
    LCD_WriteReg(0xD417,0xCA);
    LCD_WriteReg(0xD418,0x02);
    LCD_WriteReg(0xD419,0x00);
    LCD_WriteReg(0xD41A,0x02);
    LCD_WriteReg(0xD41B,0x01);
    LCD_WriteReg(0xD41C,0x02);
    LCD_WriteReg(0xD41D,0x34);
    LCD_WriteReg(0xD41E,0x02);
    LCD_WriteReg(0xD41F,0x67);
    LCD_WriteReg(0xD420,0x02);
    LCD_WriteReg(0xD421,0x84);
    LCD_WriteReg(0xD422,0x02);
    LCD_WriteReg(0xD423,0xA4);
    LCD_WriteReg(0xD424,0x02);
    LCD_WriteReg(0xD425,0xB7);
    LCD_WriteReg(0xD426,0x02);
    LCD_WriteReg(0xD427,0xCF);
    LCD_WriteReg(0xD428,0x02);
    LCD_WriteReg(0xD429,0xDE);
    LCD_WriteReg(0xD42A,0x02);
    LCD_WriteReg(0xD42B,0xF2);
    LCD_WriteReg(0xD42C,0x02);
    LCD_WriteReg(0xD42D,0xFE);
    LCD_WriteReg(0xD42E,0x03);
    LCD_WriteReg(0xD42F,0x10);
    LCD_WriteReg(0xD430,0x03);
    LCD_WriteReg(0xD431,0x33);
    LCD_WriteReg(0xD432,0x03);
    LCD_WriteReg(0xD433,0x6D);
    LCD_WriteReg(0xD500,0x00);
    LCD_WriteReg(0xD501,0x33);
    LCD_WriteReg(0xD502,0x00);
    LCD_WriteReg(0xD503,0x34);
    LCD_WriteReg(0xD504,0x00);
    LCD_WriteReg(0xD505,0x3A);
    LCD_WriteReg(0xD506,0x00);
    LCD_WriteReg(0xD507,0x4A);
    LCD_WriteReg(0xD508,0x00);
    LCD_WriteReg(0xD509,0x5C);
    LCD_WriteReg(0xD50A,0x00);
    LCD_WriteReg(0xD50B,0x81);

    LCD_WriteReg(0xD50C,0x00);
    LCD_WriteReg(0xD50D,0xA6);
    LCD_WriteReg(0xD50E,0x00);
    LCD_WriteReg(0xD50F,0xE5);
    LCD_WriteReg(0xD510,0x01);
    LCD_WriteReg(0xD511,0x13);
    LCD_WriteReg(0xD512,0x01);
    LCD_WriteReg(0xD513,0x54);
    LCD_WriteReg(0xD514,0x01);
    LCD_WriteReg(0xD515,0x82);
    LCD_WriteReg(0xD516,0x01);
    LCD_WriteReg(0xD517,0xCA);
    LCD_WriteReg(0xD518,0x02);
    LCD_WriteReg(0xD519,0x00);
    LCD_WriteReg(0xD51A,0x02);
    LCD_WriteReg(0xD51B,0x01);
    LCD_WriteReg(0xD51C,0x02);
    LCD_WriteReg(0xD51D,0x34);
    LCD_WriteReg(0xD51E,0x02);
    LCD_WriteReg(0xD51F,0x67);
    LCD_WriteReg(0xD520,0x02);
    LCD_WriteReg(0xD521,0x84);
    LCD_WriteReg(0xD522,0x02);
    LCD_WriteReg(0xD523,0xA4);
    LCD_WriteReg(0xD524,0x02);
    LCD_WriteReg(0xD525,0xB7);
    LCD_WriteReg(0xD526,0x02);
    LCD_WriteReg(0xD527,0xCF);
    LCD_WriteReg(0xD528,0x02);
    LCD_WriteReg(0xD529,0xDE);
    LCD_WriteReg(0xD52A,0x02);
    LCD_WriteReg(0xD52B,0xF2);
    LCD_WriteReg(0xD52C,0x02);
    LCD_WriteReg(0xD52D,0xFE);
    LCD_WriteReg(0xD52E,0x03);
    LCD_WriteReg(0xD52F,0x10);
    LCD_WriteReg(0xD530,0x03);
    LCD_WriteReg(0xD531,0x33);
    LCD_WriteReg(0xD532,0x03);
    LCD_WriteReg(0xD533,0x6D);
    LCD_WriteReg(0xD600,0x00);
    LCD_WriteReg(0xD601,0x33);
    LCD_WriteReg(0xD602,0x00);
    LCD_WriteReg(0xD603,0x34);
    LCD_WriteReg(0xD604,0x00);
    LCD_WriteReg(0xD605,0x3A);
    LCD_WriteReg(0xD606,0x00);
    LCD_WriteReg(0xD607,0x4A);
    LCD_WriteReg(0xD608,0x00);
    LCD_WriteReg(0xD609,0x5C);
    LCD_WriteReg(0xD60A,0x00);
    LCD_WriteReg(0xD60B,0x81);

    LCD_WriteReg(0xD60C,0x00);
    LCD_WriteReg(0xD60D,0xA6);
    LCD_WriteReg(0xD60E,0x00);
    LCD_WriteReg(0xD60F,0xE5);
    LCD_WriteReg(0xD610,0x01);
    LCD_WriteReg(0xD611,0x13);
    LCD_WriteReg(0xD612,0x01);
    LCD_WriteReg(0xD613,0x54);
    LCD_WriteReg(0xD614,0x01);
    LCD_WriteReg(0xD615,0x82);
    LCD_WriteReg(0xD616,0x01);
    LCD_WriteReg(0xD617,0xCA);
    LCD_WriteReg(0xD618,0x02);
    LCD_WriteReg(0xD619,0x00);
    LCD_WriteReg(0xD61A,0x02);
    LCD_WriteReg(0xD61B,0x01);
    LCD_WriteReg(0xD61C,0x02);
    LCD_WriteReg(0xD61D,0x34);
    LCD_WriteReg(0xD61E,0x02);
    LCD_WriteReg(0xD61F,0x67);
    LCD_WriteReg(0xD620,0x02);
    LCD_WriteReg(0xD621,0x84);
    LCD_WriteReg(0xD622,0x02);
    LCD_WriteReg(0xD623,0xA4);
    LCD_WriteReg(0xD624,0x02);
    LCD_WriteReg(0xD625,0xB7);
    LCD_WriteReg(0xD626,0x02);
    LCD_WriteReg(0xD627,0xCF);
    LCD_WriteReg(0xD628,0x02);
    LCD_WriteReg(0xD629,0xDE);
    LCD_WriteReg(0xD62A,0x02);
    LCD_WriteReg(0xD62B,0xF2);
    LCD_WriteReg(0xD62C,0x02);
    LCD_WriteReg(0xD62D,0xFE);
    LCD_WriteReg(0xD62E,0x03);
    LCD_WriteReg(0xD62F,0x10);
    LCD_WriteReg(0xD630,0x03);
    LCD_WriteReg(0xD631,0x33);
    LCD_WriteReg(0xD632,0x03);
    LCD_WriteReg(0xD633,0x6D);
    //LV2 Page 0 enable
    LCD_WriteReg(0xF000,0x55);
    LCD_WriteReg(0xF001,0xAA);
    LCD_WriteReg(0xF002,0x52);
    LCD_WriteReg(0xF003,0x08);
    LCD_WriteReg(0xF004,0x00);
    //Display control
    LCD_WriteReg(0xB100, 0xCC);
    LCD_WriteReg(0xB101, 0x00);
    //Source hold time
    LCD_WriteReg(0xB600,0x05);
    //Gate EQ control
    LCD_WriteReg(0xB700,0x70);
    LCD_WriteReg(0xB701,0x70);
    //Source EQ control (Mode 2)
    LCD_WriteReg(0xB800,0x01);
    LCD_WriteReg(0xB801,0x03);
    LCD_WriteReg(0xB802,0x03);
    LCD_WriteReg(0xB803,0x03);
    //Inversion mode (2-dot)
    LCD_WriteReg(0xBC00,0x02);
    LCD_WriteReg(0xBC01,0x00);
    LCD_WriteReg(0xBC02,0x00);
    //Timing control 4H w/ 4-delay
    LCD_WriteReg(0xC900,0xD0);
    LCD_WriteReg(0xC901,0x02);
    LCD_WriteReg(0xC902,0x50);
    LCD_WriteReg(0xC903,0x50);
    LCD_WriteReg(0xC904,0x50);
    LCD_WriteReg(0x3500,0x00);
    LCD_WriteReg(0x3A00,0x55);  //16-bit/pixel
    LCD_WR_REG(0x1100);
    Delay_us(120);
    LCD_WR_REG(0x2900);
  }else if(lcddev.id==0x9325)//9325
  {
    LCD_WriteReg(0x00E5,0x78F0);
    LCD_WriteReg(0x0001,0x0100);
    LCD_WriteReg(0x0002,0x0700);
    LCD_WriteReg(0x0003,0x1030);
    LCD_WriteReg(0x0004,0x0000);
    LCD_WriteReg(0x0008,0x0202);
    LCD_WriteReg(0x0009,0x0000);
    LCD_WriteReg(0x000A,0x0000);
    LCD_WriteReg(0x000C,0x0000);
    LCD_WriteReg(0x000D,0x0000);
    LCD_WriteReg(0x000F,0x0000);
    //power on sequence VGHVGL
    LCD_WriteReg(0x0010,0x0000);
    LCD_WriteReg(0x0011,0x0007);
    LCD_WriteReg(0x0012,0x0000);
    LCD_WriteReg(0x0013,0x0000);
    LCD_WriteReg(0x0007,0x0000);
    //vgh
    LCD_WriteReg(0x0010,0x1690);
    LCD_WriteReg(0x0011,0x0227);
    //delayms(100);
    //vregiout
    LCD_WriteReg(0x0012,0x009D); //0x001b
    //delayms(100);
    //vom amplitude
    LCD_WriteReg(0x0013,0x1900);
    //delayms(100);
    //vom H
    LCD_WriteReg(0x0029,0x0025);
    LCD_WriteReg(0x002B,0x000D);
    //gamma
    LCD_WriteReg(0x0030,0x0007);
    LCD_WriteReg(0x0031,0x0303);
    LCD_WriteReg(0x0032,0x0003);// 0006
    LCD_WriteReg(0x0035,0x0206);
    LCD_WriteReg(0x0036,0x0008);
    LCD_WriteReg(0x0037,0x0406);
    LCD_WriteReg(0x0038,0x0304);//0200
    LCD_WriteReg(0x0039,0x0007);
    LCD_WriteReg(0x003C,0x0602);// 0504
    LCD_WriteReg(0x003D,0x0008);
    //ram
    LCD_WriteReg(0x0050,0x0000);
    LCD_WriteReg(0x0051,0x00EF);
    LCD_WriteReg(0x0052,0x0000);
    LCD_WriteReg(0x0053,0x013F);
    LCD_WriteReg(0x0060,0xA700);
    LCD_WriteReg(0x0061,0x0001);
    LCD_WriteReg(0x006A,0x0000);
    //
    LCD_WriteReg(0x0080,0x0000);
    LCD_WriteReg(0x0081,0x0000);
    LCD_WriteReg(0x0082,0x0000);
    LCD_WriteReg(0x0083,0x0000);
    LCD_WriteReg(0x0084,0x0000);
    LCD_WriteReg(0x0085,0x0000);
    //
    LCD_WriteReg(0x0090,0x0010);
    LCD_WriteReg(0x0092,0x0600);

    LCD_WriteReg(0x0007,0x0133);
    LCD_WriteReg(0x00,0x0022);//
  }else if(lcddev.id==0x9328)//ILI9328   OK
  {
    LCD_WriteReg(0x00EC,0x108F);// internal timeing
    LCD_WriteReg(0x00EF,0x1234);// ADD
    //LCD_WriteReg(0x00e7,0x0010);
    //LCD_WriteReg(0x0000,0x0001);// Turn on the internal clock
    LCD_WriteReg(0x0001,0x0100);
    LCD_WriteReg(0x0002,0x0700);//Power on
    //LCD_WriteReg(0x0003,(1<<3)|(1<<4) ); 	//65K  RGB
    //Drive table (register 03H)
    //BIT3=AM BIT4:5=ID0:1
    //AM ID0 ID1   FUNCATION
    // 0  0   0	   R->L D->U
    // 1  0   0	   D->U	R->L
    // 0  1   0	   L->R D->U
    // 1  1   0    D->U	L->R
    // 0  0   1	   R->L U->D
    // 1  0   1    U->D	R->L
    // 0111 l->r u->d normally use this
    // 1  1   1	   U->D	L->R
    LCD_WriteReg(0x0003,(1<<12)|(3<<4)|(0<<3) );//65K
    LCD_WriteReg(0x0004,0x0000);
    LCD_WriteReg(0x0008,0x0202);
    LCD_WriteReg(0x0009,0x0000);
    LCD_WriteReg(0x000a,0x0000);//display setting
    LCD_WriteReg(0x000c,0x0001);//display setting
    LCD_WriteReg(0x000d,0x0000);//0f3c
    LCD_WriteReg(0x000f,0x0000);
    //Power configuration
    LCD_WriteReg(0x0010,0x0000);
    LCD_WriteReg(0x0011,0x0007);
    LCD_WriteReg(0x0012,0x0000);
    LCD_WriteReg(0x0013,0x0000);
    LCD_WriteReg(0x0007,0x0001);
    Delay_ms(50);
    LCD_WriteReg(0x0010,0x1490);
    LCD_WriteReg(0x0011,0x0227);
    Delay_ms(50);
    LCD_WriteReg(0x0012,0x008A);
    Delay_ms(50);
    LCD_WriteReg(0x0013,0x1a00);
    LCD_WriteReg(0x0029,0x0006);
    LCD_WriteReg(0x002b,0x000d);
    Delay_ms(50);
    LCD_WriteReg(0x0020,0x0000);
    LCD_WriteReg(0x0021,0x0000);
    Delay_ms(50);
    //Gamma correction 
    LCD_WriteReg(0x0030,0x0000);
    LCD_WriteReg(0x0031,0x0604);
    LCD_WriteReg(0x0032,0x0305);
    LCD_WriteReg(0x0035,0x0000);
    LCD_WriteReg(0x0036,0x0C09);
    LCD_WriteReg(0x0037,0x0204);
    LCD_WriteReg(0x0038,0x0301);
    LCD_WriteReg(0x0039,0x0707);
    LCD_WriteReg(0x003c,0x0000);
    LCD_WriteReg(0x003d,0x0a0a);
    Delay_ms(50);
    LCD_WriteReg(0x0050,0x0000); //Horizontal GRAM start position
    LCD_WriteReg(0x0051,0x00ef); //Horizontal GRAM end position
    LCD_WriteReg(0x0052,0x0000); //Vertical GRAM start position
    LCD_WriteReg(0x0053,0x013f); //Vertical GRAM end position

    LCD_WriteReg(0x0060,0xa700);
    LCD_WriteReg(0x0061,0x0001);
    LCD_WriteReg(0x006a,0x0000);
    LCD_WriteReg(0x0080,0x0000);
    LCD_WriteReg(0x0081,0x0000);
    LCD_WriteReg(0x0082,0x0000);
    LCD_WriteReg(0x0083,0x0000);
    LCD_WriteReg(0x0084,0x0000);
    LCD_WriteReg(0x0085,0x0000);

    LCD_WriteReg(0x0090,0x0010);
    LCD_WriteReg(0x0092,0x0600);
    //Turn on display settings
    LCD_WriteReg(0x0007,0x0133);
  }else if(lcddev.id==0x9320)//Test OK
  {
    LCD_WriteReg(0x00,0x0000);
    LCD_WriteReg(0x01,0x0100);	//Driver Output Contral.
    LCD_WriteReg(0x02,0x0700);	//TFTLCD Driver Waveform Contral.
    LCD_WriteReg(0x03,0x1030);//Entry Mode Set.
    //LCD_WriteReg(0x03,0x1018);	//Entry Mode Set.

    LCD_WriteReg(0x04,0x0000);	//Scalling Contral.
    LCD_WriteReg(0x08,0x0202);	//Display Contral 2.(0x0207)
    LCD_WriteReg(0x09,0x0000);	//Display Contral 3.(0x0000)
    LCD_WriteReg(0x0a,0x0000);	//Frame Cycle Contal.(0x0000)
    LCD_WriteReg(0x0c,(1<<0));	//Extern Display Interface Contral 1.(0x0000)
    LCD_WriteReg(0x0d,0x0000);	//Frame Maker Position.
    LCD_WriteReg(0x0f,0x0000);	//Extern Display Interface Contral 2.
    Delay_ms(50);
    LCD_WriteReg(0x07,0x0101);	//Display Contral.
    Delay_ms(50);
    LCD_WriteReg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4));	//Power Control 1.(0x16b0)
    LCD_WriteReg(0x11,0x0007);								//Power Control 2.(0x0001)
    LCD_WriteReg(0x12,(1<<8)|(1<<4)|(0<<0));				//Power Control 3.(0x0138)
    LCD_WriteReg(0x13,0x0b00);								//Power Control 4.
    LCD_WriteReg(0x29,0x0000);								//Power Control 7.

    LCD_WriteReg(0x2b,(1<<14)|(1<<4));
    LCD_WriteReg(0x50,0);	//Set X Star
    //Horizontal GRAM end position Set X End
    LCD_WriteReg(0x51,239);	//Set Y Star
    LCD_WriteReg(0x52,0);	//Set Y End.t.
    LCD_WriteReg(0x53,319);	//

    LCD_WriteReg(0x60,0x2700);	//Driver Output Control.
    LCD_WriteReg(0x61,0x0001);	//Driver Output Control.
    LCD_WriteReg(0x6a,0x0000);	//Vertical Srcoll Control.

    LCD_WriteReg(0x80,0x0000);	//Display Position? Partial Display 1.
    LCD_WriteReg(0x81,0x0000);	//RAM Address Start? Partial Display 1.
    LCD_WriteReg(0x82,0x0000);	//RAM Address End-Partial Display 1.
    LCD_WriteReg(0x83,0x0000);	//Displsy Position? Partial Display 2.
    LCD_WriteReg(0x84,0x0000);	//RAM Address Start? Partial Display 2.
    LCD_WriteReg(0x85,0x0000);	//RAM Address End? Partial Display 2.

    LCD_WriteReg(0x90,(0<<7)|(16<<0));	//Frame Cycle Contral.(0x0013)
    LCD_WriteReg(0x92,0x0000);	//Panel Interface Contral 2.(0x0000)
    LCD_WriteReg(0x93,0x0001);	//Panel Interface Contral 3.
    LCD_WriteReg(0x95,0x0110);	//Frame Cycle Contral.(0x0110)
    LCD_WriteReg(0x97,(0<<8));	//
    LCD_WriteReg(0x98,0x0000);	//Frame Cycle Contral.
    LCD_WriteReg(0x07,0x0173);	//(0x0173)
  }else if(lcddev.id==0X9331)//OK |/|/|
  {
    LCD_WriteReg(0x00E7, 0x1014);
    LCD_WriteReg(0x0001, 0x0100); // set SS and SM bit
    LCD_WriteReg(0x0002, 0x0200); // set 1 line inversion
    LCD_WriteReg(0x0003,(1<<12)|(3<<4)|(1<<3));//65K
    //LCD_WriteReg(0x0003, 0x1030); // set GRAM write direction and BGR=1.
    LCD_WriteReg(0x0008, 0x0202); // set the back porch and front porch
    LCD_WriteReg(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0]
    LCD_WriteReg(0x000A, 0x0000); // FMARK function
    LCD_WriteReg(0x000C, 0x0000); // RGB interface setting
    LCD_WriteReg(0x000D, 0x0000); // Frame marker Position
    LCD_WriteReg(0x000F, 0x0000); // RGB interface polarity
    //*************Power On sequence ****************//
    LCD_WriteReg(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB
    LCD_WriteReg(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
    LCD_WriteReg(0x0012, 0x0000); // VREG1OUT voltage
    LCD_WriteReg(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
    Delay_ms(200); // Dis-charge capacitor power voltage
    LCD_WriteReg(0x0010, 0x1690); // SAP, BT[3:0], AP, DSTB, SLP, STB
    LCD_WriteReg(0x0011, 0x0227); // DC1[2:0], DC0[2:0], VC[2:0]
    Delay_ms(50); // Delay 50ms
    LCD_WriteReg(0x0012, 0x000C); // Internal reference voltage= Vci;
    Delay_ms(50); // Delay 50ms
    LCD_WriteReg(0x0013, 0x0800); // Set VDV[4:0] for VCOM amplitude
    LCD_WriteReg(0x0029, 0x0011); // Set VCM[5:0] for VCOMH
    LCD_WriteReg(0x002B, 0x000B); // Set Frame Rate
    Delay_ms(50); // Delay 50ms
    LCD_WriteReg(0x0020, 0x0000); // GRAM horizontal Address
    LCD_WriteReg(0x0021, 0x013f); // GRAM Vertical Address
    // ----------- Adjust the Gamma Curve ----------//
    LCD_WriteReg(0x0030, 0x0000);
    LCD_WriteReg(0x0031, 0x0106);
    LCD_WriteReg(0x0032, 0x0000);
    LCD_WriteReg(0x0035, 0x0204);
    LCD_WriteReg(0x0036, 0x160A);
    LCD_WriteReg(0x0037, 0x0707);
    LCD_WriteReg(0x0038, 0x0106);
    LCD_WriteReg(0x0039, 0x0707);
    LCD_WriteReg(0x003C, 0x0402);
    LCD_WriteReg(0x003D, 0x0C0F);
    //------------------ Set GRAM area ---------------//
    LCD_WriteReg(0x0050, 0x0000); // Horizontal GRAM Start Address
    LCD_WriteReg(0x0051, 0x00EF); // Horizontal GRAM End Address
    LCD_WriteReg(0x0052, 0x0000); // Vertical GRAM Start Address
    LCD_WriteReg(0x0053, 0x013F); // Vertical GRAM Start Address
    LCD_WriteReg(0x0060, 0x2700); // Gate Scan Line
    LCD_WriteReg(0x0061, 0x0001); // NDL,VLE, REV
    LCD_WriteReg(0x006A, 0x0000); // set scrolling line
    //-------------- Partial Display Control ---------//
    LCD_WriteReg(0x0080, 0x0000);
    LCD_WriteReg(0x0081, 0x0000);
    LCD_WriteReg(0x0082, 0x0000);
    LCD_WriteReg(0x0083, 0x0000);
    LCD_WriteReg(0x0084, 0x0000);
    LCD_WriteReg(0x0085, 0x0000);
    //-------------- Panel Control -------------------//
    LCD_WriteReg(0x0090, 0x0010);
    LCD_WriteReg(0x0092, 0x0600);
    LCD_WriteReg(0x0007, 0x0133); // 262K color and display ON
  }else if(lcddev.id==0x5408)
  {
    LCD_WriteReg(0x01,0x0100);
    LCD_WriteReg(0x02,0x0700);//TFTLCD Driving Waveform Contral
    LCD_WriteReg(0x03,0x1030);//Entry Mode settings
    //Automatic increment mode of pointer from left to right from top to bottom
    //Normal Mode(Window Mode disable)
    //RGB format
    //8-bus setting for 2-time transmission of 16 bit data
    LCD_WriteReg(0x04,0x0000); //Scalling Control register
    LCD_WriteReg(0x08,0x0207); //Display Control 2
    LCD_WriteReg(0x09,0x0000); //Display Control 3
    LCD_WriteReg(0x0A,0x0000); //Frame Cycle Control
    LCD_WriteReg(0x0C,0x0000); //External Display Interface Control 1
    LCD_WriteReg(0x0D,0x0000); //Frame Maker Position
    LCD_WriteReg(0x0F,0x0000); //External Display Interface Control 2
    Delay_ms(20);
    //TFT liquid crystal color image display method 14
    LCD_WriteReg(0x10,0x16B0); //0x14B0 //Power Control 1
    LCD_WriteReg(0x11,0x0001); //0x0007 //Power Control 2
    LCD_WriteReg(0x17,0x0001); //0x0000 //Power Control 3
    LCD_WriteReg(0x12,0x0138); //0x013B //Power Control 4
    LCD_WriteReg(0x13,0x0800); //0x0800 //Power Control 5
    LCD_WriteReg(0x29,0x0009); //NVM read data 2
    LCD_WriteReg(0x2a,0x0009); //NVM read data 3
    LCD_WriteReg(0xa4,0x0000);
    LCD_WriteReg(0x50,0x0000); //Set the X-axis start column of the operation window
    LCD_WriteReg(0x51,0x00EF); //Sets the X-axis end column of the operation window
    LCD_WriteReg(0x52,0x0000); //Sets the start line of the Y axis of the operation window
    LCD_WriteReg(0x53,0x013F); //Sets the Y-axis end line of the operation window
    LCD_WriteReg(0x60,0x2700); //Driver Output Control
    //Set the number of points on the screen and the starting line of scanning
    LCD_WriteReg(0x61,0x0001); //Driver Output Control
    LCD_WriteReg(0x6A,0x0000); //Vertical Scroll Control
    LCD_WriteReg(0x80,0x0000); //Display Position – Partial Display 1
    LCD_WriteReg(0x81,0x0000); //RAM Address Start – Partial Display 1
    LCD_WriteReg(0x82,0x0000); //RAM address End - Partial Display 1
    LCD_WriteReg(0x83,0x0000); //Display Position – Partial Display 2
    LCD_WriteReg(0x84,0x0000); //RAM Address Start – Partial Display 2
    LCD_WriteReg(0x85,0x0000); //RAM address End – Partail Display2
    LCD_WriteReg(0x90,0x0013); //Frame Cycle Control
    LCD_WriteReg(0x92,0x0000);  //Panel Interface Control 2
    LCD_WriteReg(0x93,0x0003); //Panel Interface control 3
    LCD_WriteReg(0x95,0x0110);  //Frame Cycle Control
    LCD_WriteReg(0x07,0x0173);
    Delay_ms(50);
  }
  else if(lcddev.id==0x1505)//OK
  {
    // second release on 3/5  ,luminance is acceptable,water wave appear during camera preview
    LCD_WriteReg(0x0007,0x0000);
    Delay_ms(50);
    LCD_WriteReg(0x0012,0x011C);//0x011A   why need to set several times?
    LCD_WriteReg(0x00A4,0x0001);//NVM
    LCD_WriteReg(0x0008,0x000F);
    LCD_WriteReg(0x000A,0x0008);
    LCD_WriteReg(0x000D,0x0008);
    //Gamma correction 
    LCD_WriteReg(0x0030,0x0707);
    LCD_WriteReg(0x0031,0x0007); //0x0707
    LCD_WriteReg(0x0032,0x0603);
    LCD_WriteReg(0x0033,0x0700);
    LCD_WriteReg(0x0034,0x0202);
    LCD_WriteReg(0x0035,0x0002); //?0x0606
    LCD_WriteReg(0x0036,0x1F0F);
    LCD_WriteReg(0x0037,0x0707); //0x0f0f  0x0105
    LCD_WriteReg(0x0038,0x0000);
    LCD_WriteReg(0x0039,0x0000);
    LCD_WriteReg(0x003A,0x0707);
    LCD_WriteReg(0x003B,0x0000); //0x0303
    LCD_WriteReg(0x003C,0x0007); //?0x0707
    LCD_WriteReg(0x003D,0x0000); //0x1313//0x1f08
    Delay_ms(50);
    LCD_WriteReg(0x0007,0x0001);
    LCD_WriteReg(0x0017,0x0001);//Turn on the power
    Delay_ms(50);
    //Power configuration
    LCD_WriteReg(0x0010,0x17A0);
    LCD_WriteReg(0x0011,0x0217);//reference voltage VC[2:0]   Vciout = 1.00*Vcivl
    LCD_WriteReg(0x0012,0x011E);//0x011c  //Vreg1out = Vcilvl*1.80   is it the same as Vgama1out ?
    LCD_WriteReg(0x0013,0x0F00);//VDV[4:0]-->VCOM Amplitude VcomL = VcomH - Vcom Ampl
    LCD_WriteReg(0x002A,0x0000);
    LCD_WriteReg(0x0029,0x000A);//0x0001F  Vcomh = VCM1[4:0]*Vreg1out    gate source voltage??
    LCD_WriteReg(0x0012,0x013E);// 0x013C  power supply on
    //Coordinates Control//
    LCD_WriteReg(0x0050,0x0000);//0x0e00
    LCD_WriteReg(0x0051,0x00EF);
    LCD_WriteReg(0x0052,0x0000);
    LCD_WriteReg(0x0053,0x013F);
    //Pannel Image Control//
    LCD_WriteReg(0x0060,0x2700);
    LCD_WriteReg(0x0061,0x0001);
    LCD_WriteReg(0x006A,0x0000);
    LCD_WriteReg(0x0080,0x0000);
    //Partial Image Control//
    LCD_WriteReg(0x0081,0x0000);
    LCD_WriteReg(0x0082,0x0000);
    LCD_WriteReg(0x0083,0x0000);
    LCD_WriteReg(0x0084,0x0000);
    LCD_WriteReg(0x0085,0x0000);
    //Panel Interface Control//
    LCD_WriteReg(0x0090,0x0013);//0x0010 frenqucy
    LCD_WriteReg(0x0092,0x0300);
    LCD_WriteReg(0x0093,0x0005);
    LCD_WriteReg(0x0095,0x0000);
    LCD_WriteReg(0x0097,0x0000);
    LCD_WriteReg(0x0098,0x0000);

    LCD_WriteReg(0x0001,0x0100);
    LCD_WriteReg(0x0002,0x0700);
    LCD_WriteReg(0x0003,0x1038);//Scan direction up - > down, left - > right
    LCD_WriteReg(0x0004,0x0000);
    LCD_WriteReg(0x000C,0x0000);
    LCD_WriteReg(0x000F,0x0000);
    LCD_WriteReg(0x0020,0x0000);
    LCD_WriteReg(0x0021,0x0000);
    LCD_WriteReg(0x0007,0x0021);
    Delay_ms(20);
    LCD_WriteReg(0x0007,0x0061);
    Delay_ms(20);
    LCD_WriteReg(0x0007,0x0173);
    Delay_ms(20);
  }else if(lcddev.id==0xB505)
  {
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);

    LCD_WriteReg(0x00a4,0x0001);
    Delay_ms(20);
    LCD_WriteReg(0x0060,0x2700);
    LCD_WriteReg(0x0008,0x0202);

    LCD_WriteReg(0x0030,0x0214);
    LCD_WriteReg(0x0031,0x3715);
    LCD_WriteReg(0x0032,0x0604);
    LCD_WriteReg(0x0033,0x0e16);
    LCD_WriteReg(0x0034,0x2211);
    LCD_WriteReg(0x0035,0x1500);
    LCD_WriteReg(0x0036,0x8507);
    LCD_WriteReg(0x0037,0x1407);
    LCD_WriteReg(0x0038,0x1403);
    LCD_WriteReg(0x0039,0x0020);

    LCD_WriteReg(0x0090,0x001a);
    LCD_WriteReg(0x0010,0x0000);
    LCD_WriteReg(0x0011,0x0007);
    LCD_WriteReg(0x0012,0x0000);
    LCD_WriteReg(0x0013,0x0000);
    Delay_ms(20);

    LCD_WriteReg(0x0010,0x0730);
    LCD_WriteReg(0x0011,0x0137);
    Delay_ms(20);

    LCD_WriteReg(0x0012,0x01b8);
    Delay_ms(20);

    LCD_WriteReg(0x0013,0x0f00);
    LCD_WriteReg(0x002a,0x0080);
    LCD_WriteReg(0x0029,0x0048);
    Delay_ms(20);

    LCD_WriteReg(0x0001,0x0100);
    LCD_WriteReg(0x0002,0x0700);
    LCD_WriteReg(0x0003,0x1038);//Scan direction up - > down, left - > right
    LCD_WriteReg(0x0008,0x0202);
    LCD_WriteReg(0x000a,0x0000);
    LCD_WriteReg(0x000c,0x0000);
    LCD_WriteReg(0x000d,0x0000);
    LCD_WriteReg(0x000e,0x0030);
    LCD_WriteReg(0x0050,0x0000);
    LCD_WriteReg(0x0051,0x00ef);
    LCD_WriteReg(0x0052,0x0000);
    LCD_WriteReg(0x0053,0x013f);
    LCD_WriteReg(0x0060,0x2700);
    LCD_WriteReg(0x0061,0x0001);
    LCD_WriteReg(0x006a,0x0000);
    //LCD_WriteReg(0x0080,0x0000);
    //LCD_WriteReg(0x0081,0x0000);
    LCD_WriteReg(0x0090,0X0011);
    LCD_WriteReg(0x0092,0x0600);
    LCD_WriteReg(0x0093,0x0402);
    LCD_WriteReg(0x0094,0x0002);
    Delay_ms(20);

    LCD_WriteReg(0x0007,0x0001);
    Delay_ms(20);
    LCD_WriteReg(0x0007,0x0061);
    LCD_WriteReg(0x0007,0x0173);

    LCD_WriteReg(0x0020,0x0000);
    LCD_WriteReg(0x0021,0x0000);
    LCD_WriteReg(0x00,0x22);
  }else if(lcddev.id==0xC505)
  {
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);
    Delay_ms(20);
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x0000,0x0000);
    LCD_WriteReg(0x00a4,0x0001);
    Delay_ms(20);
    LCD_WriteReg(0x0060,0x2700);
    LCD_WriteReg(0x0008,0x0806);

    LCD_WriteReg(0x0030,0x0703);//gamma setting
    LCD_WriteReg(0x0031,0x0001);
    LCD_WriteReg(0x0032,0x0004);
    LCD_WriteReg(0x0033,0x0102);
    LCD_WriteReg(0x0034,0x0300);
    LCD_WriteReg(0x0035,0x0103);
    LCD_WriteReg(0x0036,0x001F);
    LCD_WriteReg(0x0037,0x0703);
    LCD_WriteReg(0x0038,0x0001);
    LCD_WriteReg(0x0039,0x0004);



    LCD_WriteReg(0x0090, 0x0015);	//80Hz
    LCD_WriteReg(0x0010, 0X0410);	//BT,AP
    LCD_WriteReg(0x0011,0x0247);	//DC1,DC0,VC
    LCD_WriteReg(0x0012, 0x01BC);
    LCD_WriteReg(0x0013, 0x0e00);
    Delay_ms(120);
    LCD_WriteReg(0x0001, 0x0100);
    LCD_WriteReg(0x0002, 0x0200);
    LCD_WriteReg(0x0003, 0x1030);

    LCD_WriteReg(0x000A, 0x0008);
    LCD_WriteReg(0x000C, 0x0000);

    LCD_WriteReg(0x000E, 0x0020);
    LCD_WriteReg(0x000F, 0x0000);
    LCD_WriteReg(0x0020, 0x0000);	//H Start
    LCD_WriteReg(0x0021, 0x0000);	//V Start
    LCD_WriteReg(0x002A,0x003D);	//vcom2
    Delay_ms(20);
    LCD_WriteReg(0x0029, 0x002d);
    LCD_WriteReg(0x0050, 0x0000);
    LCD_WriteReg(0x0051, 0xD0EF);
    LCD_WriteReg(0x0052, 0x0000);
    LCD_WriteReg(0x0053, 0x013F);
    LCD_WriteReg(0x0061, 0x0000);
    LCD_WriteReg(0x006A, 0x0000);
    LCD_WriteReg(0x0092,0x0300);

    LCD_WriteReg(0x0093, 0x0005);
    LCD_WriteReg(0x0007, 0x0100);
  }else if(lcddev.id==0x4531)//OK |/|/|
  {
    LCD_WriteReg(0X00,0X0001);
    Delay_ms(10);
    LCD_WriteReg(0X10,0X1628);
    LCD_WriteReg(0X12,0X000e);//0x0006
    LCD_WriteReg(0X13,0X0A39);
    Delay_ms(10);
    LCD_WriteReg(0X11,0X0040);
    LCD_WriteReg(0X15,0X0050);
    Delay_ms(10);
    LCD_WriteReg(0X12,0X001e);//16
    Delay_ms(10);
    LCD_WriteReg(0X10,0X1620);
    LCD_WriteReg(0X13,0X2A39);
    Delay_ms(10);
    LCD_WriteReg(0X01,0X0100);
    LCD_WriteReg(0X02,0X0300);
    LCD_WriteReg(0X03,0X1038);//Changing direction
    LCD_WriteReg(0X08,0X0202);
    LCD_WriteReg(0X0A,0X0008);
    LCD_WriteReg(0X30,0X0000);
    LCD_WriteReg(0X31,0X0402);
    LCD_WriteReg(0X32,0X0106);
    LCD_WriteReg(0X33,0X0503);
    LCD_WriteReg(0X34,0X0104);
    LCD_WriteReg(0X35,0X0301);
    LCD_WriteReg(0X36,0X0707);
    LCD_WriteReg(0X37,0X0305);
    LCD_WriteReg(0X38,0X0208);
    LCD_WriteReg(0X39,0X0F0B);
    LCD_WriteReg(0X41,0X0002);
    LCD_WriteReg(0X60,0X2700);
    LCD_WriteReg(0X61,0X0001);
    LCD_WriteReg(0X90,0X0210);
    LCD_WriteReg(0X92,0X010A);
    LCD_WriteReg(0X93,0X0004);
    LCD_WriteReg(0XA0,0X0100);
    LCD_WriteReg(0X07,0X0001);
    LCD_WriteReg(0X07,0X0021);
    LCD_WriteReg(0X07,0X0023);
    LCD_WriteReg(0X07,0X0033);
    LCD_WriteReg(0X07,0X0133);
    LCD_WriteReg(0XA0,0X0000);
  }else if(lcddev.id==0x4535)
  {
    LCD_WriteReg(0X15,0X0030);
    LCD_WriteReg(0X9A,0X0010);
    LCD_WriteReg(0X11,0X0020);
    LCD_WriteReg(0X10,0X3428);
    LCD_WriteReg(0X12,0X0002);//16
    LCD_WriteReg(0X13,0X1038);
    Delay_ms(40);
    LCD_WriteReg(0X12,0X0012);//16
    Delay_ms(40);
    LCD_WriteReg(0X10,0X3420);
    LCD_WriteReg(0X13,0X3038);
    Delay_ms(70);
    LCD_WriteReg(0X30,0X0000);
    LCD_WriteReg(0X31,0X0402);
    LCD_WriteReg(0X32,0X0307);
    LCD_WriteReg(0X33,0X0304);
    LCD_WriteReg(0X34,0X0004);
    LCD_WriteReg(0X35,0X0401);
    LCD_WriteReg(0X36,0X0707);
    LCD_WriteReg(0X37,0X0305);
    LCD_WriteReg(0X38,0X0610);
    LCD_WriteReg(0X39,0X0610);

    LCD_WriteReg(0X01,0X0100);
    LCD_WriteReg(0X02,0X0300);
    LCD_WriteReg(0X03,0X1030);//Changing direction
    LCD_WriteReg(0X08,0X0808);
    LCD_WriteReg(0X0A,0X0008);
    LCD_WriteReg(0X60,0X2700);
    LCD_WriteReg(0X61,0X0001);
    LCD_WriteReg(0X90,0X013E);
    LCD_WriteReg(0X92,0X0100);
    LCD_WriteReg(0X93,0X0100);
    LCD_WriteReg(0XA0,0X3000);
    LCD_WriteReg(0XA3,0X0010);
    LCD_WriteReg(0X07,0X0001);
    LCD_WriteReg(0X07,0X0021);
    LCD_WriteReg(0X07,0X0023);
    LCD_WriteReg(0X07,0X0033);
    LCD_WriteReg(0X07,0X0133);
  }else if(lcddev.id==0X1963)
  {
    LCD_WR_REG(0xE2);		//Set PLL with OSC = 10MHz (hardware),	Multiplier N = 35, 250MHz < VCO < 800MHz = OSC*(N+1), VCO = 300MHz
    LCD_WR_DATA(0x1D);		//Parameter 1
    LCD_WR_DATA(0x02);		//Parameter 2: divider M = 2, PLL = 300 / (M + 1) = 100MHz
    LCD_WR_DATA(0x04);		//Valuepoint 3 Validate M and N values
    Delay_us(100);
    LCD_WR_REG(0xE0);		// Start PLL command
    LCD_WR_DATA(0x01);		// enable PLL
    Delay_ms(10);
    LCD_WR_REG(0xE0);		// Start PLL command again
    LCD_WR_DATA(0x03);		// now, use PLL output as system clock
    Delay_ms(12);
    LCD_WR_REG(0x01);		//Soft reset
    Delay_ms(10);

    LCD_WR_REG(0xE6);		//Set pixel frequency, 33Mhz
    LCD_WR_DATA(0x2F);
    LCD_WR_DATA(0xFF);
    LCD_WR_DATA(0xFF);

    LCD_WR_REG(0xB0);		//Set LCD mode
    LCD_WR_DATA(0x20);		//24 bit mode
    LCD_WR_DATA(0x00);		//TFT mode

    LCD_WR_DATA((SSD_HOR_RESOLUTION-1)>>8);//Set LCD horizontal pixels
    LCD_WR_DATA(SSD_HOR_RESOLUTION-1);
    LCD_WR_DATA((SSD_VER_RESOLUTION-1)>>8);//Set LCD vertical pixels
    LCD_WR_DATA(SSD_VER_RESOLUTION-1);
    LCD_WR_DATA(0x00);		//RGB sequence

    LCD_WR_REG(0xB4);		//Set horizontal period
    LCD_WR_DATA((SSD_HT-1)>>8);
    LCD_WR_DATA(SSD_HT-1);
    LCD_WR_DATA(SSD_HPS>>8);
    LCD_WR_DATA(SSD_HPS);
    LCD_WR_DATA(SSD_HOR_PULSE_WIDTH-1);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);
    LCD_WR_REG(0xB6);		//Set vertical period
    LCD_WR_DATA((SSD_VT-1)>>8);
    LCD_WR_DATA(SSD_VT-1);
    LCD_WR_DATA(SSD_VPS>>8);
    LCD_WR_DATA(SSD_VPS);
    LCD_WR_DATA(SSD_VER_FRONT_PORCH-1);
    LCD_WR_DATA(0x00);
    LCD_WR_DATA(0x00);

    LCD_WR_REG(0xF0);	//Set the interface between SSD1963 and CPU to 16bit
    LCD_WR_DATA(0x03);	//16-bit(565 format) data for 16bpp

    LCD_WR_REG(0x29);	//Turn on display
    //Set the PWM output backlight, which is adjustable through the duty cycle
    LCD_WR_REG(0xD0);	//Set automatic white balance DBC
    LCD_WR_DATA(0x00);	//disable

    LCD_WR_REG(0xBE);	//Configure PWM output
    LCD_WR_DATA(0x05);	//1 set PWM frequency
    LCD_WR_DATA(0xFE);	//2 set PWM duty cycle
    LCD_WR_DATA(0x01);	//3 setting C
    LCD_WR_DATA(0x00);	//4 setting D
    LCD_WR_DATA(0x00);	//5 setting E
    LCD_WR_DATA(0x00);	//6 setting F

    LCD_WR_REG(0xB8);	//Set GPIO configuration
    LCD_WR_DATA(0x03);	//Two IO ports are set as output
    LCD_WR_DATA(0x01);	//GPIO uses normal IO functions
    LCD_WR_REG(0xBA);
    LCD_WR_DATA(0X01);	//GPIO[1:0]=01, control LCD direction

    LCD_SSD_BackLightSet(100);//The backlight is set to the brightest
  }
  LCD_Display_Dir(0);		//The default is vertical screen
  LCD_LED_ON();				//Turn on the backlight
  LCD_Clear(WHITE);
}
//Screen clearing function
//Color: the fill color to clear the screen
void LCD_Clear(uint16_t color)
{
  uint32_t index=0;
  uint32_t totalpoint=lcddev.width;
  totalpoint*=lcddev.height; 			//Get total points
  if((lcddev.id==0X6804)&&(lcddev.dir==1))//6804 special treatment for horizontal screen
  {
    lcddev.dir=0;
    lcddev.setxcmd=0X2A;
    lcddev.setycmd=0X2B;
    LCD_SetCursor(0x00,0x0000);		//Set cursor position
    lcddev.dir=1;
    lcddev.setxcmd=0X2B;
    lcddev.setycmd=0X2A;
  }else LCD_SetCursor(0x00,0x0000);	//Set cursor position
  LCD_WriteRAM_Prepare();     		//Start writing GRAM
  for(index=0;index<totalpoint;index++)
  {
    TFTLCD->LCD_RAM=color;
  }
}
//Fills a specified area with a single color
//(sx,sy),(ex,ey): the diagonal coordinates of the filled rectangle. The area size is: (ex SX + 1) * (ey sy + 1)
//Color: the color to fill
void LCD_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t color)
{
  uint16_t i,j;
  uint16_t xlen=0;
  uint16_t temp;
  if((lcddev.id==0X6804)&&(lcddev.dir==1))	//6804 special treatment for horizontal screen
  {
    temp=sx;
    sx=sy;
    sy=lcddev.width-ex-1;
    ex=ey;
    ey=lcddev.width-temp-1;
    lcddev.dir=0;
    lcddev.setxcmd=0X2A;
    lcddev.setycmd=0X2B;
    LCD_Fill(sx,sy,ex,ey,color);
    lcddev.dir=1;
    lcddev.setxcmd=0X2B;
    lcddev.setycmd=0X2A;
  }else
  {
    xlen=ex-sx+1;
    for(i=sy;i<=ey;i++)
    {
      LCD_SetCursor(sx,i);      				//Set cursor position
      LCD_WriteRAM_Prepare();     			//Start writing GRAM
      for(j=0;j<xlen;j++)TFTLCD->LCD_RAM=color;	//Display color
    }
  }
}
//Fills the specified color block in the specified area
//(sx,sy),(ex,ey): the diagonal coordinates of the filled rectangle. The area size is: (ex SX + 1) * (ey sy + 1)
//Color: the color to fill
void LCD_Color_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color)
{
  uint16_t height,width;
  uint16_t i,j;
  width=ex-sx+1; 			//Get the width of the fill
  height=ey-sy+1;			//height
  for(i=0;i<height;i++)
  {
    LCD_SetCursor(sx,sy+i);   	//Set cursor position
    LCD_WriteRAM_Prepare();     //Start writing GRAM
    for(j=0;j<width;j++)TFTLCD->LCD_RAM=color[i*width+j];//Write data
  }
}
//Draw a line
//x1,y1: starting point coordinates
//x2,y2: end coordinates
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
  uint16_t t;
  int xerr=0,yerr=0,delta_x,delta_y,distance;
  int incx,incy,uRow,uCol;
  delta_x=x2-x1; //Calculate coordinate increment
  delta_y=y2-y1;
  uRow=x1;
  uCol=y1;
  if(delta_x>0)incx=1; //Set single step direction
  else if(delta_x==0)incx=0;//Vertical line
  else {incx=-1;delta_x=-delta_x;}
  if(delta_y>0)incy=1;
  else if(delta_y==0)incy=0;//level
  else{incy=-1;delta_y=-delta_y;}
  if( delta_x>delta_y)distance=delta_x; //Select basic incremental axis
  else distance=delta_y;
  for(t=0;t<=distance+1;t++ )//Draw line output
  {
    LCD_DrawPoint(uRow,uCol);//Draw points
    xerr+=delta_x ;
    yerr+=delta_y ;
    if(xerr>distance)
    {
      xerr-=distance;
      uRow+=incx;
    }
    if(yerr>distance)
    {
      yerr-=distance;
      uCol+=incy;
    }
  }
}
//Draw rectangle
//(x1,y1),(x2,y2): diagonal coordinates of rectangle
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
  LCD_DrawLine(x1,y1,x2,y1);
  LCD_DrawLine(x1,y1,x1,y2);
  LCD_DrawLine(x1,y2,x2,y2);
  LCD_DrawLine(x2,y1,x2,y2);
}
//Draw a circle of the specified size at the specified position
//(x,y): center point
//r: radius
void LCD_Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r)
{
  int a,b;
  int di;
  a=0;b=r;
  di=3-(r<<1);             //Mark for judging the position of the next point
  while(a<=b)
  {
    LCD_DrawPoint(x0+a,y0-b);             //5
    LCD_DrawPoint(x0+b,y0-a);             //0
    LCD_DrawPoint(x0+b,y0+a);             //4
    LCD_DrawPoint(x0+a,y0+b);             //6
    LCD_DrawPoint(x0-a,y0+b);             //1
    LCD_DrawPoint(x0-b,y0+a);
    LCD_DrawPoint(x0-a,y0-b);             //2
    LCD_DrawPoint(x0-b,y0-a);             //7
    a++;
    //Draw a circle using Bresenham algorithm
    if(di<0)di +=4*a+6;
    else
    {
      di+=10+4*(a-b);
      b--;
    }
  }
}
//Displays a character in the specified position
//x. Y: starting coordinate
//num: characters to display: '' --- > "~"
//Size: font size 12 / 16 / 24
//Mode: superposition mode (1) or non superposition mode (0)
void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint8_t size,uint8_t mode)
{
  uint8_t temp,t1,t;
  uint16_t y0=y;
  uint8_t csize=(size/8+((size%8)?1:0))*(size/2);		//Get the number of bytes in the lattice set corresponding to one character of the font
  num=num-' ';//Get the offset value (the ASCII font takes the module from the space, so - '' is the font of the corresponding character)
  for(t=0;t<csize;t++)
  {
    if(size==12)temp=asc2_1206[num][t]; 	 	//Call 1206 font
    else if(size==16)temp=asc2_1608[num][t];	//Call 1608 font
    else if(size==24)temp=asc2_2412[num][t];	//Call 2412 font
    else return;								//No font
    for(t1=0;t1<8;t1++)
    {
      if(temp&0x80)LCD_Fast_DrawPoint(x,y,POINT_COLOR);
      else if(mode==0)LCD_Fast_DrawPoint(x,y,BACK_COLOR);
      temp<<=1;
      y++;
      if(y>=lcddev.height)return;		//Super area
      if((y-y0)==size)
      {
        y=y0;
        x++;
        if(x>=lcddev.width)return;	//Super area
        break;
      }
    }
  }
}
//m^n function
//Return value: m^n power
uint32_t LCD_Pow(uint8_t m,uint8_t n)
{
  uint32_t result=1;
  while(n--)result*=m;
  return result;
}
//Displays a number. If the high order is 0, it will not be displayed
//x. Y: starting point coordinates
//len: number of digits
//Size: font size
//Color: color
//num: value (0 ~ 4294967295);
void LCD_ShowNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size)
{
  uint8_t t,temp;
  uint8_t enshow=0;
  for(t=0;t<len;t++)
  {
    temp=(num/LCD_Pow(10,len-t-1))%10;
    if(enshow==0&&t<(len-1))
    {
      if(temp==0)
      {
        LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
        continue;
      }else enshow=1;

    }
    LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0);
  }
}
//Display number, high order is 0, or display
//x. Y: starting point coordinates
//num: value (0 ~ 9999999);
//len: length (i.e. number of digits to be displayed)
//Size: font size
//mode:
//[7] : 0, not filled; 1, fill 0
//[6:1]: reserved
//[0]: 0, non superimposed display; 1. Overlay display
void LCD_ShowxNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size,uint8_t mode)
{
  uint8_t t,temp;
  uint8_t enshow=0;
  for(t=0;t<len;t++)
  {
    temp=(num/LCD_Pow(10,len-t-1))%10;
    if(enshow==0&&t<(len-1))
    {
      if(temp==0)
      {
        if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);
        else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);
        continue;
      }else enshow=1;

    }
    LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01);
  }
}
//display string
//x. Y: starting point coordinates
//width,height: area size
//Size: font size
//*p: String start address
void LCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,uint8_t *p)
{
  uint8_t x0=x;
  width+=x;
  height+=y;
  while((*p<='~')&&(*p>=' '))//Judge whether it is an illegal character!
  {
    if(x>=width){x=x0;y+=size;}
    if(y>=height)break;//sign out
    LCD_ShowChar(x,y,*p,size,0);
    x+=size/2;
    p++;
  }
}
//Set display area
//begine_x,begine_y: Starting point coordinates
//end_x,end_y: End coordinates
void LCD_SetShowArea(uint16_t begine_x,uint16_t end_x,uint16_t begine_y,uint16_t end_y)
{
	//Set display area
	//Set cursor position (0x2A 0x2B)
	LCD_WR_REG(lcddev.setxcmd);//Set page address command
	LCD_WR_DATA(begine_x>>8);//Sets the high byte of the start address of the page
	LCD_WR_DATA(begine_x&0XFF); //Set the low byte of the start address of the page
	LCD_WR_DATA((end_x-1)>>8);//Sets the high byte of the end address of the page
	LCD_WR_DATA((end_x-1)&0xFF);//Sets the low byte of the end address of the page		
	LCD_WR_REG(lcddev.setycmd);//Set column address command
	LCD_WR_DATA(begine_y>>8);//Sets the high byte of the start address of the column
	LCD_WR_DATA(begine_y&0XFF);//Sets the low byte of the start address of the column
	LCD_WR_DATA((end_y-1)>>8);//Sets the high byte of the end address of the column
	LCD_WR_DATA((end_y-1)&0xFF);//Sets the low byte of the end address of the column
}
//display picture
//x. Y: starting point coordinates
//width,height: area size
//*p: Picture start address
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t *p)
{
	uint16_t i=0,j=0;
	uint16_t pcolor = 0;
	//Set display area
	//Set cursor position (0x2A 0x2B)
	LCD_SetShowArea(x,x+width,y,y+height);
	TFTLCD->LCD_REG=lcddev.wramcmd;//Start writing GRAM(0x2C)
	for(j=0;j<height;j++)
	{
		for(i=0;i<width;i++)
		{
			pcolor = (*p<<8)|(*(p+1));
			TFTLCD->LCD_RAM=pcolor;  //Write display color
			p+=2;
		}
	}
}
//display picture
//x. Y: starting point coordinates
//width,height: area size
//b_color -- background color f_color -- foreground color
//*p: Font start address
void LCD_ShowxXx(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint16_t b_color,uint16_t f_color,uint8_t *p)
{
	uint16_t i,j,k;
	//Set display area
	//Set cursor position (0x2A 0x2B)
	LCD_SetShowArea(x,x+width,y,y+height);
	TFTLCD->LCD_REG=lcddev.wramcmd;//Start writing GRAM(0x2C)
	for(j=0;j<height;j++)//Row -- page
	{                              
		for(i=0;i<width/8;i++)//column
		{
				for(k=0;k<8;k++)
				{
					//Draw points
					if(*p & (0x80>>k)) //From the highest drawing point, only one person needs to be judged
						TFTLCD->LCD_RAM=f_color;//Stroke character color
					else
						TFTLCD->LCD_RAM=b_color;//Tracing background color   
				}
				p++;
		}
		
	}
}



bsp_lcd.h

#ifndef __LCD_H
#define __LCD_H
#include "main.h"

//LCD important parameter set
typedef struct
{
  uint16_t width;			//LCD width
  uint16_t height;			//LCD height
  uint16_t id;				//LCD ID
  uint8_t  dir;			//Horizontal screen or vertical screen control: 0, vertical screen; 1. Horizontal screen.
  uint16_t	wramcmd;		//Start writing gram instruction
  uint16_t  setxcmd;		//Set x coordinate command
  uint16_t  setycmd;		//Set y coordinate command
}_lcd_dev;

//LCD parameters
extern _lcd_dev lcddev;	//Manage LCD important parameters
//Brush color and background color of LCD
extern uint16_t  POINT_COLOR;//Default red
extern uint16_t  BACK_COLOR; //Background color The default is white


//
//-----------------LCD port definition----------------
#define	LCD_LED_ON()     GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_SET)
#define 	 LCD_LED_OFF()    GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_RESET)//LCD backlight
//LCD address structure
typedef struct
{
  volatile uint16_t LCD_REG;//HADDR7 = A6 = 0 send command to LCD screen / / 0x6C00 007E
  volatile uint16_t LCD_RAM;//HADDR7 = A6 = 1 send data to LCD screen / / 0x6C00 0080
} LCD_TypeDef;
//Bank1 using NOR/SRAM Sector4, address bit haddr [27,26] = 11, A6 is used as the data command division line
//Note that when setting, STM32 will be shifted to the right by one bit! 0X7E = 0111 1110(HADDR7~HADDR0)(A6~A0)
#define LCD_ Base ((uint32_t) (0x6c00000 | 0x0000007e)) / / structure variable increment
#define TFTLCD          ((LCD_TypeDef *) LCD_BASE)
//

//Scan direction definition
#define L2R_ U2D 0 / / from left to right, from top to bottom
#define L2R_ D2u 1 / / from left to right, from bottom to top
#define R2L_ U2D 2 / / from right to left, from top to bottom
#define R2L_ D2u 3 / / from right to left, from bottom to top

#define U2D_ L2R 4 / / from top to bottom, from left to right
#define U2D_ R2l 5 / / from top to bottom, from right to left
#define D2U_ L2R 6 / / from bottom to top, from left to right
#define D2U_ R2l 7 / / from bottom to top, from right to left

#define DFT_SCAN_DIR  L2R_U2D / / default scanning direction

//stroke color 
#define WHITE         	 0xFFFF
#define BLACK         	 0x0000
#define BLUE         	 0x001F
#define BRED             0XF81F
#define GRED 			      0XFFE0
#define GBLUE			       0X07FF
#define RED           	 0xF800
#define MAGENTA       	 0xF81F
#define GREEN         	 0x07E0
#define CYAN          	 0x7FFF
#define YELLOW        	 0xFFE0
#define BROWN  			  0XBC40 / / Brown
#define BRRED  			  0XFC07 / / brownish red
#define GRAY   			  0X8430 / / gray
//GUI color

#define DARKBLUE       	  0X01CF 	// navy blue
#define LIGHTBLUE       	  0X7D7C 	// wathet
#define GRAYBLUE        	  0X5458 / / grey blue
//The above three colors are the colors of PANEL

#define LIGHTGREEN      	  0X841F / / light green
//#Define lightgray 0xef5b / / light gray
#define LGRAY  			  0XC618 / / light gray (panel), form background color

#Define lgrayblue 0xa651 / / light gray blue (middle layer color)
#Define lbblue 0x2b12 / / light brown blue (select the reverse color of the item)

void TFTLCD_Init(void);													   	//initialization
void LCD_DisplayOn(void);													//On display
void LCD_DisplayOff(void);													//Off display
void LCD_Clear(uint16_t Color);	 												//Clear screen
void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos);										//Set cursor
void LCD_DrawPoint(uint16_t x,uint16_t y);											//Draw points
void LCD_Fast_DrawPoint(uint16_t x,uint16_t y,uint16_t color);								//Quick draw point
uint16_t  LCD_ReadPoint(uint16_t x,uint16_t y); 											//Read point
void LCD_Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r);						 			//Draw a circle
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);							//Draw a line
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);		   				//Draw rectangle
void LCD_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t color);		   				//Fill monochrome
void LCD_Color_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color);				//Fill the specified color
void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint8_t size,uint8_t mode);						//Display one character
void LCD_ShowNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size);  						//Display a number
void LCD_ShowxNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size,uint8_t mode);				//Display number
void LCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,uint8_t *p);		//Display a string, 12 / 16 font
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t *p);
void LCD_ShowxXx(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint16_t b_color,uint16_t f_color,uint8_t *p);
void LCD_WR_REG(volatile uint16_t regval);
void LCD_WR_DATA(volatile uint16_t data);
uint16_t LCD_RD_DATA(void);
void LCD_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue);
uint16_t LCD_ReadReg(uint16_t LCD_Reg);
void LCD_WriteRAM_Prepare(void);
void LCD_WriteRAM(uint16_t RGB_Code);
void LCD_SSD_BackLightSet(uint8_t pwm);							//SSD1963 backlight control
void LCD_Scan_Dir(uint8_t dir);									//Set screen scanning direction
void LCD_Display_Dir(uint8_t dir);								//Set screen display direction
void LCD_Set_Window(uint16_t sx,uint16_t sy,uint16_t width,uint16_t height);	//Setup window
//LCD resolution setting
#define SSD_HOR_RESOLUTION 		 three hundred and twenty 		// LCD horizontal resolution
#define SSD_VER_RESOLUTION 		 four hundred and eighty 		// LCD vertical resolution
//LCD drive parameter setting
#define SSD_HOR_PULSE_WIDTH 		 one 		// Horizontal pulse width
#define SSD_HOR_BACK_PORCH 		 forty-six 		// Horizontal front porch
#define SSD_HOR_FRONT_PORCH 		 two hundred and ten 		// Horizontal back porch

#define SSD_VER_PULSE_WIDTH 		 one 		// Vertical pulse width
#define SSD_VER_BACK_PORCH 		 twenty-three 		// Vertical front porch
#define SSD_VER_FRONT_PORCH 		 twenty-two 		// Vertical front porch
//The following parameters are calculated automatically
#define SSD_HT	(SSD_HOR_RESOLUTION+SSD_HOR_BACK_PORCH+SSD_HOR_FRONT_PORCH)
#define SSD_HPS	(SSD_HOR_BACK_PORCH)
#define SSD_VT 	(SSD_VER_RESOLUTION+SSD_VER_BACK_PORCH+SSD_VER_FRONT_PORCH)
#define SSD_VPS (SSD_VER_BACK_PORCH)

#endif

The above pure personal notes, thank you for your review, welcome to exchange and correct.

Tags: C stm32 LCD

Posted by captain_scarlet87 on Sun, 17 Apr 2022 04:48:58 +0300