/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 -----------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序        */
/*---------------------------------------------------------------------*/


/*************  功能说明    **************

LCM接口+BMM驱动液晶屏程序

8bit I8080模式, P2口接D0~D7

sbit LCD_RS = P4^1;      //数据/命令切换
sbit LCD_WR = P3^6;      //写控制
sbit LCD_RD = P3^7;      //读控制
sbit LCD_CS = P3^4;      //片选	
sbit LCD_RESET = P3^5;   //复位 

LCM指令通过中断方式等待发送完成

BMM设置长度256字节，通过中断方式判断传输完成

下载时, 选择时钟 24MHz (5寸屏, RA8875驱动).

******************************************/


#include    "reg51.h"
#include    "intrins.h"
#include    "LCD_IMG.h"

#define     MAIN_Fosc       24000000L   //定义主时钟

typedef     unsigned char   u8;
typedef     unsigned int    u16;
typedef     unsigned long   u32;

sfr TH2  = 0xD6;
sfr TL2  = 0xD7;
sfr IE2   = 0xAF;
sfr INT_CLKO = 0x8F;
sfr AUXR = 0x8E;
sfr AUXR1 = 0xA2;
sfr P_SW1 = 0xA2;
sfr P_SW2 = 0xBA;
sfr S2CON = 0x9A;
sfr S2BUF = 0x9B;

sfr P4   = 0xC0;
sfr P5   = 0xC8;
sfr P6   = 0xE8;
sfr P7   = 0xF8;
sfr P1M1 = 0x91;    //PxM1.n,PxM0.n     =00--->Standard,    01--->push-pull
sfr P1M0 = 0x92;    //                  =10--->pure input,  11--->open drain
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xB1;
sfr P3M0 = 0xB2;
sfr P4M1 = 0xB3;
sfr P4M0 = 0xB4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;

sbit P00 = P0^0;
sbit P01 = P0^1;
sbit P02 = P0^2;
sbit P03 = P0^3;
sbit P04 = P0^4;
sbit P05 = P0^5;
sbit P06 = P0^6;
sbit P07 = P0^7;
sbit P10 = P1^0;
sbit P11 = P1^1;
sbit P12 = P1^2;
sbit P13 = P1^3;
sbit P14 = P1^4;
sbit P15 = P1^5;
sbit P16 = P1^6;
sbit P17 = P1^7;
sbit P20 = P2^0;
sbit P21 = P2^1;
sbit P22 = P2^2;
sbit P23 = P2^3;
sbit P24 = P2^4;
sbit P25 = P2^5;
sbit P26 = P2^6;
sbit P27 = P2^7;
sbit P30 = P3^0;
sbit P31 = P3^1;
sbit P32 = P3^2;
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P35 = P3^5;
sbit P36 = P3^6;
sbit P37 = P3^7;
sbit P40 = P4^0;
sbit P41 = P4^1;
sbit P42 = P4^2;
sbit P43 = P4^3;
sbit P44 = P4^4;
sbit P45 = P4^5;
sbit P46 = P4^6;
sbit P47 = P4^7;
sbit P50 = P5^0;
sbit P51 = P5^1;
sbit P52 = P5^2;
sbit P53 = P5^3;
sbit P54 = P5^4;
sbit P55 = P5^5;
sbit P56 = P5^6;
sbit P57 = P5^7;

//LCM
#define LCMIFCFG          (*(unsigned char volatile xdata *)0xfe50)
#define LCMCFG2           (*(unsigned char volatile xdata *)0xfe51)
#define LCMIFSTA          (*(unsigned char volatile xdata *)0xfe53)
#define LCMIFCR           (*(unsigned char volatile xdata *)0xfe52)
#define LCMIFDATL         (*(unsigned char volatile xdata *)0xfe54)
#define LCMIFDATH         (*(unsigned char volatile xdata *)0xfe55)

//BMM
#define LCMIF_BMM_CFG     (*(unsigned char volatile xdata *)0xfa70)
#define LCMIF_BMM_CR      (*(unsigned char volatile xdata *)0xfa71)
#define LCMIF_BMM_STA     (*(unsigned char volatile xdata *)0xfa72)
#define LCMIF_BMM_AMT     (*(unsigned char volatile xdata *)0xfa73)
#define LCMIF_BMM_DONE_N  (*(unsigned char volatile xdata *)0xfa74)
#define LCMIF_BMM_TXA     (*(unsigned int volatile xdata *)0xfa75)
#define LCMIF_BMM_TXAH    (*(unsigned char volatile xdata *)0xfa75)
#define LCMIF_BMM_TXAL    (*(unsigned char volatile xdata *)0xfa76)
#define LCMIF_BMM_RXA     (*(unsigned int volatile xdata *)0xfa77)
#define LCMIF_BMM_RXAH    (*(unsigned char volatile xdata *)0xfa77)
#define LCMIF_BMM_RXAL    (*(unsigned char volatile xdata *)0xfa78)

#define CLKDIV (*(unsigned char volatile xdata *)0xfe01)
#define MCLKOCR (*(unsigned char volatile xdata *)0xfe05)
	
sbit LCD_RS = P4^1;      //数据/命令切换
sbit LCD_WR = P3^6;      //写控制
sbit LCD_RD = P3^7;      //读控制
sbit LCD_CS = P3^4;      //片选	
sbit LCD_RESET = P3^5;   //复位 

//IO连接
#define  LCD_DataPort P2     //8位数据口

#define  USR_LCM_IF     1			//1: use LCM Interface

//画笔颜色
#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 //棕色
#define BRRED            0XFC07 //棕红色
#define GRAY             0X8430 //灰色

#define  LCD_W     799			//LCD 宽度
#define  LCD_H     479			//LCD 高度

u16 index;
u16 xdata Color[256];
bit BmmFlag;
bit LcmFlag;

void delay_ms(u16 ms);
void GPIO_Init(void);
void LCM_Config(void);
void LCD_Init(void);
void BMM_Config(void);
void LCD_SetWindows(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd);
void Test_Background(void);
void Test_Text(void);
void Test_Pic(void);
void Test_Color(void);

void main(void)
{
	P0M1 = 0;   P0M0 = 0;   //设置为准双向口
	P1M1 = 0;   P1M0 = 0;   //设置为准双向口
	P2M1 = 0;   P2M0 = 0;   //设置为准双向口
	P3M1 = 0;   P3M0 = 0;   //设置为准双向口
	P4M1 = 0;   P4M0 = 0;   //设置为准双向口
	P5M1 = 0;   P5M0 = 0;   //设置为准双向口
	P6M1 = 0;   P6M0 = 0;   //设置为准双向口
	P7M1 = 0;   P7M0 = 0;   //设置为准双向口

	GPIO_Init();
#if USR_LCM_IF == 1	
	LCM_Config();
	BMM_Config();
	EA = 1;
#endif
	
	LCD_Init(); //LCM初始化

	while(1)
	{
//		Test_Background();
		Test_Color();
		Test_Text();
		Test_Pic();
	}
}

//========================================================================
// 函数: void delay_ms(u16 ms)
// 描述: 延时函数。
// 参数: ms,要延时的ms数, 自动适应主时钟.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注: 
//========================================================================
void delay_ms(u16 ms)
{
	 u16 i;
	 do{
			i = MAIN_Fosc / 13000;
			while(--i);   //14T per loop
	 }while(--ms);
}

void LCD_DataWrite(unsigned char cmd)  	//RA8875 RS与标准相反, 0: data; 1: cmd
{
#if USR_LCM_IF == 1
	
	LCMIFDATL = cmd;
	LCD_CS = 0;
	LcmFlag = 1;
	LCMIFCR = 0x84;		//Enable interface, write command out, RS=0
	while(LcmFlag);
	LCD_CS = 1 ;
	
#else

	LCD_RD = 1;       
	LCD_CS = 0;
	LCD_RS = 0;
	LCD_DataPort = cmd;
	LCD_WR = 0;
	_nop_();
	_nop_();
	_nop_();
	LCD_WR = 1;
	LCD_CS = 1;
	LCD_DataPort = 0xff;
	
#endif
}

void LCD_CmdWrite(unsigned char dat)  	//RA8875 RS与标准相反, 0: data; 1: cmd
{
#if USR_LCM_IF == 1
	
	LCMIFDATL = dat;
	LCD_CS = 0;
	LcmFlag = 1;
	LCMIFCR = 0x85;		//Enable interface, write data out, RS=1
	while(LcmFlag);
	LCD_CS = 1 ;
	
#else

	LCD_RD = 1;
	LCD_CS = 0;
	LCD_RS = 1;
	LCD_DataPort = dat;
	LCD_WR = 0;
	_nop_();
	_nop_();
	_nop_();
	LCD_WR = 1;
	LCD_CS = 1;
	LCD_DataPort = 0xff;
	
#endif
}

u8 LCD_DataRead(void)  	//RA8875 RS与标准相反, 0: data; 1: cmd
{
	u8 d;
#if USR_LCM_IF == 1
	
	//LCM Read
	LCD_CS = 0;
	LcmFlag = 1;
	LCMIFCR = 0x86;		//Enable interface, Read command, RS=1
	while(LcmFlag);
	LCD_CS = 1 ;
	d = LCMIFDATL;
	return (d);
	
#else

	//P2口设置成高阻输入
	P2M0=0x00;
	P2M1=0xff;

	LCD_WR = 1;
	LCD_RS = 0;
	LCD_RD = 0;  
	LCD_CS = 0;
	_nop_();
	_nop_();
	_nop_();
	d = LCD_DataPort;  
	LCD_RD = 1;
	LCD_CS = 1;

  //P2口设置成推挽输出
	P2M0=0xff;
	P2M1=0x00;
	return d;

#endif
}

u8 LCD_StatusRead(void) 	//RA8875 RS与标准相反, 0: data; 1: cmd
{
	u16 d;
#if USR_LCM_IF == 1
	
	//LCM Read
	LCD_CS = 0;
	LcmFlag = 1;
	LCMIFCR = 0x87;		//Enable interface, Read data, RS=1
	while(LcmFlag);
	LCD_CS = 1 ;
	d = LCMIFDATL;
	return (d);
	
#else

  //P2口设置成高阻输入
	P2M0=0x00;
	P2M1=0xff;

	LCD_WR = 1;
	LCD_RS = 1;
	LCD_RD = 0;
	LCD_CS = 0;
	_nop_();
	_nop_();
	_nop_();
	d = LCD_DataPort;  
	LCD_RD = 1;
	LCD_CS = 1;
	LCD_DataPort = 0xff;

  //P2口设置成推挽输出
	P2M0=0xff;
	P2M1=0x00;
	return d;
	
#endif
}

void Chk_Busy(void)
{
	u8 temp; 	
	do
	{
		temp=LCD_StatusRead();
	}while((temp&0x80)==0x80);
}

///////////////check bte busy
void Chk_BTE_Busy(void)
{
	u8 temp; 	
	do
	{
		temp=LCD_StatusRead();
	}while((temp&0x40)==0x40);		   
}

void LCD_WR_REG(u8 Reg, u8 dat)
{
	LCD_CmdWrite(Reg);
	LCD_DataWrite(dat);
}

void LCD_WR_DATA(u16 dat)
{
	LCD_DataWrite(dat>>8);
	LCD_DataWrite(dat);
}

/*****************************************************************************
 * @name       :void GPIO_Init(void)
 * @date       :2018-11-13 
 * @function   :Set the gpio to push-pull mode
 * @parameters :None
 * @retvalue   :None
******************************************************************************/	
void GPIO_Init(void)
{
  //P0口设置成推挽输出
//	P0M0=0xff;
//	P0M1=0x00;

	//P1口设置成准双向口
//	P1M0=0x00;
//	P1M1=0x00;

  //P2口设置成推挽输出
	P2M0=0xff;
	P2M1=0x00;

	//P3.4~P3.7口设置成推挽输出
	P3M0=0xf0;
	P3M1=0x00;

	//P4.1口设置成推挽输出
	P4M0=0x02;
	P4M1=0x00;
}

/*****************************************************************************
 * @name       :void BMM_Config(void)
 * @date       :2020-12-09 
 * @function   :Config BMM
 * @parameters :None
 * @retvalue   :None
******************************************************************************/	
void BMM_Config(void)
{
	P_SW2 = 0x80;
	LCMIF_BMM_AMT = 0xff;		//Exe 256(n+1) bytes
	LCMIF_BMM_TXA = &Color;
//	LCMIF_BMM_RXA = &Buffer;
	LCMIF_BMM_STA = 0x00;
	LCMIF_BMM_CFG = 0x82;		//Enable Interrupt
	LCMIF_BMM_CR = 0x00;
}

/*****************************************************************************
 * @name       :void LCM_Config(void)
 * @date       :2018-11-13 
 * @function   :Config LCM
 * @parameters :None
 * @retvalue   :None
******************************************************************************/	
void LCM_Config(void)
{
	P_SW2 = 0x80;
	LCMIFCFG = 0x80;	//bit7 1:Enable Interrupt, bit1 0:8bit mode; bit0 0:8080,1:6800
	LCMCFG2 = 0x29;		//RS:P41,RD:P37,WR:P36; Setup Time=2,HOLD Time=1
	LCMIFSTA = 0x00;
}

/*****************************************************************************
 * @name       :void LCDReset(void)
 * @date       :2018-08-09 
 * @function   :Reset LCD screen
 * @parameters :None
 * @retvalue   :None
******************************************************************************/	
void LCDReset(void)
{
	LCD_CS=1;
	delay_ms(50);	
	LCD_RESET=0;
	delay_ms(150);
	LCD_RESET=1;
	delay_ms(50);
}

void PLL_ini(void)
{
	LCD_CmdWrite(0x88);
	LCD_DataWrite(0x0c);
	delay_ms(1);
	LCD_CmdWrite(0x89);
	LCD_DataWrite(0x02);
	delay_ms(1);
}

void LCD_SetWindows(u16 XL, u16 XR, u16 YT, u16 YB)
{	
	u8 temp;
	//setting active window X
	temp=XL;   
	LCD_CmdWrite(0x30);//HSAW0
	LCD_DataWrite(temp);
	temp=XL>>8;   
	LCD_CmdWrite(0x31);//HSAW1	   
	LCD_DataWrite(temp);

	temp=XR;   
	LCD_CmdWrite(0x34);//HEAW0
	LCD_DataWrite(temp);
	temp=XR>>8;   
	LCD_CmdWrite(0x35);//HEAW1	   
	LCD_DataWrite(temp);

	//setting active window Y
	temp=YT;   
	LCD_CmdWrite(0x32);//VSAW0
	LCD_DataWrite(temp);
	temp=YT>>8;   
	LCD_CmdWrite(0x33);//VSAW1	   
	LCD_DataWrite(temp);

	temp=YB;   
	LCD_CmdWrite(0x36);//VEAW0
	LCD_DataWrite(temp);
	temp=YB>>8;   
	LCD_CmdWrite(0x37);//VEAW1	   
	LCD_DataWrite(temp);
}   

/*****************************************************************************
 * @name       :void LCD_Init(void)
 * @date       :2018-08-09 
 * @function   :Initialization LCD screen
 * @parameters :None
 * @retvalue   :None
******************************************************************************/	 	 
void LCD_Init(void)
{
	LCDReset(); //初始化之前复位
	
	PLL_ini();
//*************LCD 初始化**********//	
	LCD_CmdWrite(0x10);	 //SYSR   bit[4:3] color  bit[2:1]= MPU interface
	LCD_DataWrite(0x0C);   //   8BIT    65K           					 
	
	LCD_CmdWrite(0x04);    //PCLK
	LCD_DataWrite(0x81);   //
	delay_ms(1);

	//Horizontal set
	LCD_CmdWrite(0x14);	 //HDWR//Horizontal Display Width Setting Bit[6:0]  
	LCD_DataWrite(0x63); //Horizontal display width(pixels) = (HDWR + 1)*8       0x27
	LCD_CmdWrite(0x15);	 //HNDFCR//Horizontal Non-Display Period fine tune Bit[3:0] 
	LCD_DataWrite(0x00); //(HNDR + 1)*8 +HNDFCR
	LCD_CmdWrite(0x16);	 //HNDR//Horizontal Non-Display Period Bit[4:0] 
	LCD_DataWrite(0x03); //Horizontal Non-Display Period (pixels) = (HNDR + 1)*8 
	LCD_CmdWrite(0x17);	 //HSTR//HSYNC Start Position[4:0] 
	LCD_DataWrite(0x03); //HSYNC Start Position(PCLK) = (HSTR + 1)*8 
	LCD_CmdWrite(0x18);	 //HPWR//HSYNC Polarity ,The period width of HSYNC. 
	LCD_DataWrite(0x0b); //HSYNC Width [4:0]   HSYNC Pulse width(PCLK) = (HPWR + 1)*8 

	//Vertical set
	LCD_CmdWrite(0x19); //VDHR0 //Vertical Display Height Bit [7:0] 
	LCD_DataWrite(0xdf); //Vertical pixels = VDHR + 1	0xef
	LCD_CmdWrite(0x1a); //VDHR1 //Vertical Display Height Bit [8] 
	LCD_DataWrite(0x01); //Vertical pixels = VDHR + 1 	0x00
	LCD_CmdWrite(0x1b); //VNDR0 //Vertical Non-Display Period Bit [7:0]
	LCD_DataWrite(0x20); //Vertical Non-Display area = (VNDR + 1) 
	LCD_CmdWrite(0x1c); //VNDR1 //Vertical Non-Display Period Bit [8] 
	LCD_DataWrite(0x00); //Vertical Non-Display area = (VNDR + 1) 
	LCD_CmdWrite(0x1d); //VSTR0 //VSYNC Start Position[7:0]
	LCD_DataWrite(0x16); //VSYNC Start Position(PCLK) = (VSTR + 1) 
	LCD_CmdWrite(0x1e); //VSTR1 //VSYNC Start Position[8] 
	LCD_DataWrite(0x00); //VSYNC Start Position(PCLK) = (VSTR + 1) 
	LCD_CmdWrite(0x1f);	 //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
	LCD_DataWrite(0x01); //VSYNC Pulse Width(PCLK) = (VPWR + 1) 

	LCD_CmdWrite(0x13);   //GPIOX setting
	LCD_DataWrite(0x0d);  //UD=0  LR=1  DITHB=1  MODE=1  

	LCD_CmdWrite(0xC7);   //GPIOX setting
	LCD_DataWrite(0xF1); //	 DISP=1 

	LCD_SetWindows(0,799,0,479);//Set the work window size

	LCD_CmdWrite(0x8a);//PWM setting
	LCD_DataWrite(0x80);
	LCD_CmdWrite(0x8a);//PWM setting
	LCD_DataWrite(0x81);//open PWM
	LCD_CmdWrite(0x8b);//Backlight brightness setting
	LCD_DataWrite(0xff);//Brightness parameter 0xff-0x00
	
	LCD_WR_REG(0X01,0X80);//display on
}

void Text_Background_Color1(u16 b_color)
{
	LCD_CmdWrite(0x60);//BGCR0
	LCD_DataWrite((u8)(b_color>>11));
	
	LCD_CmdWrite(0x61);//BGCR0
	LCD_DataWrite((u8)(b_color>>5));
	
	LCD_CmdWrite(0x62);//BGCR0
	LCD_DataWrite((u8)(b_color));
} 

//full display test
void Test_Background(void)
{	///display red
	Text_Background_Color1(WHITE);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display green
	Text_Background_Color1(BLACK);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display blue
	Text_Background_Color1(BLUE);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display white
	Text_Background_Color1(RED);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display cyan
	Text_Background_Color1(GREEN);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display yellow
	Text_Background_Color1(MAGENTA);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display purple
	Text_Background_Color1(CYAN);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
	///display black
	Text_Background_Color1(YELLOW);//Background color setting
	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
	delay_ms(300);
}

////////////////Foreground color settings
void Text_Foreground_Color1(u16 b_color)
{
	LCD_CmdWrite(0x63);//BGCR0
	LCD_DataWrite((u8)(b_color>>11));
	
	LCD_CmdWrite(0x64);//BGCR0
	LCD_DataWrite((u8)(b_color>>5));
	
	LCD_CmdWrite(0x65);//BGCR0
	LCD_DataWrite((u8)(b_color));
} 

////////////////Text write position
void FontWrite_Position(u16 X, u16 Y)
{
	u8 temp;
	temp=X;   
	LCD_CmdWrite(0x2A);
	LCD_DataWrite(temp);
	temp=X>>8;   
	LCD_CmdWrite(0x2B);	   
	LCD_DataWrite(temp);

	temp=Y;   
	LCD_CmdWrite(0x2C);
	LCD_DataWrite(temp);
	temp=Y>>8;   
	LCD_CmdWrite(0x2D);	   
	LCD_DataWrite(temp);
}

void MemoryWrite_Position(u16 X,u16 Y)
{
	u8 temp;

	temp=X;   
	LCD_CmdWrite(0x46);
	LCD_DataWrite(temp);
	temp=X>>8;   
	LCD_CmdWrite(0x47);	   
	LCD_DataWrite(temp);

	temp=Y;   
	LCD_CmdWrite(0x48);
	LCD_DataWrite(temp);
	temp=Y>>8;   
	LCD_CmdWrite(0x49);	   
	LCD_DataWrite(temp);
}

////////////////////BTE starting position
void BTE_Source(u16 SX,u16 DX ,u16 SY ,u16 DY)
{
	u8 temp,temp1;
    
	temp=SX;
	LCD_CmdWrite(0x54);//BTE horizontal position of read/write data
	LCD_DataWrite(temp);
	temp=SX>>8;
	LCD_CmdWrite(0x55);//BTE horizontal position of read/write data   
	LCD_DataWrite(temp);

	temp=DX;
	LCD_CmdWrite(0x58);//BET written to the target horizontal position
	LCD_DataWrite(temp);
	temp=DX>>8;   
	LCD_CmdWrite(0x59);//BET written to the target horizontal position	   
	LCD_DataWrite(temp); 
    
	temp=SY;   
	LCD_CmdWrite(0x56);//BTE vertical position of read/write data
	LCD_DataWrite(temp);
	temp=SY>>8;   
	LCD_CmdWrite(0x57);
	temp1 = LCD_DataRead();
	temp1 &= 0x80;
	temp=temp|temp1; 
	LCD_CmdWrite(0x57);//BTE vertical position of read/write data  
	LCD_DataWrite(temp);

	temp=DY;   
	LCD_CmdWrite(0x5a);//BET written to the target  vertical  position
	LCD_DataWrite(temp);
	temp=DY>>8;   
	LCD_CmdWrite(0x5b);
	temp1 = LCD_DataRead();
	temp1 &= 0x80;
	temp=temp|temp1;
	LCD_CmdWrite(0x5b);//BET written to the target  vertical  position 
	LCD_DataWrite(temp);
}

//////////////////BTE area size settings
void BTE_Size(u8 width, u8 height)
{
	u8 temp;
	temp=width;   
	LCD_CmdWrite(0x5c);//BET area width literacy
	LCD_DataWrite(temp);
	temp=width>>8;   
	LCD_CmdWrite(0x5d);//BET area width literacy	   
	LCD_DataWrite(temp);

	temp=height;   
	LCD_CmdWrite(0x5e);//BET area height literacy
	LCD_DataWrite(temp);
	temp=height>>8;   
	LCD_CmdWrite(0x5f);//BET area height literacy	   
	LCD_DataWrite(temp);
}		

void String(u8 *str)
{   
	LCD_WR_REG(0x40,0x80);//Set the character mode
	LCD_CmdWrite(0x02);
	while(*str != '\0')
	{
	 LCD_DataWrite(*str);
	 ++str;	 	
	 Chk_Busy();		
	}
}

void Test_Text(void)
{
	/////External characters of the functional test
	Text_Foreground_Color1(WHITE);//Set the foreground color
	Text_Background_Color1(BLACK);//Set the background color		
	LCD_SetWindows(0,799,0,479);//Set the work window size
	LCD_WR_REG(0X8E,0X80);//Start screen clearing (display window)
	Chk_Busy();
	LCD_WR_REG(0x21,0x20);//Select the external character
	LCD_WR_REG(0x06,0x03);//Set the frequency
	LCD_WR_REG(0x2E,0x80);//Font Write Type Setting Register Set up 24 x24 character mode     spacing   0 
	LCD_WR_REG(0x2F,0x81);//Serial Font ROM Setting GT23L32S4W
	LCD_WR_REG(0x05,0x28);// The waveform 3   2 byte dummy Cycle) 
	LCD_WR_REG(0x22,0x80);//Full alignment is enable.The text background color . Text don't rotation. 0x zoom		
	LCD_WR_REG(0x29,0x05);//Font Line Distance Setting
	
	FontWrite_Position(208,45);//Text written to the position
	LCD_WR_REG(0x40,0x80);//Set the character mode
	LCD_CmdWrite(0x02);//start write data
	String("STC / 江苏国芯科技有限公司");

	Text_Foreground_Color1(RED);//Set the foreground color
	LCD_WR_REG(0x2E,0x01);//Set the characters mode 16 x16 / spacing 1
	FontWrite_Position(100,90);//Text written to the position
	String("TEL:0513-55012928  FAX:0513-55012956");
	FontWrite_Position(100,120);//Text written to the position
	String("www.STCMCUDATA.com");
	FontWrite_Position(100,150);//Text written to the position
	String("QQ:  800003751");
	FontWrite_Position(100,180);//Text written to the position
	String("STC 1T Series MCU Demo Programme");
	LCD_WR_REG(0x29,0x00);//Font Line Distance Setting
	LCD_WR_REG(0x22,0x05);//Full alignment is disable.The text background color . Text don't rotation. 2x zoom		

	Text_Foreground_Color1(GREEN);//Set the foreground color
	LCD_WR_REG(0x2E,0x00);//Set the characters mode 16 x16 / spacing 0
	FontWrite_Position(0x00,250);//Text written to the position
	String("STC / 江苏国芯科技有限公司是国产单片微型计算机的领航者，是1T 8051单片机的领航者，STC是SysTem Chip(系统芯片)的缩写，因性能出众，引导着行业的发展方向，被用户评为51中的战斗机,是ISP/IAP技术全球领航者。"
	"STC江苏国芯科技现具备 0.35um/0.18um/0.11um/55nm/40nm/10nm 的高阶数模混合集成电路设计能力，江苏国芯完成最核心的设计和功能测试。");
	LCD_WR_REG(0x21,0x00);//Recovery of register
	LCD_WR_REG(0x2F,0x00);//Recovery of register

	////////////RA8875 internal input character test
	Text_Foreground_Color1(YELLOW);//Set the foreground color
	LCD_WR_REG(0x2E,0x01);//Set the characters mode 16 x16 / spacing 1
	LCD_WR_REG(0x40,0x80);//Set the character mode
	LCD_WR_REG(0x21,0x10);//Select the internal CGROM  ISO/IEC 8859-1.
	FontWrite_Position(80,5);//Text written to the position
//	LCD_WR_REG(0x22,0x00);//Full alignment is disable.The text background color . Text don't rotation. Text don't zoom

	delay_ms(800);
}

void Test_Pic(void)
{
	u16 i;

	//////////BTE Color Expansion
	Text_Background_Color1(GRAY);//Set the background color 
	Text_Foreground_Color1(YELLOW);//Set the foreground color
	BTE_Source(0,650,0,0);//BTE starting position
	BTE_Size(120,100);//BTE size setting
	LCD_WR_REG(0x51,0x78);//Raster setting
	LCD_WR_REG(0x50,0x80);//BET open
	Chk_Busy();
	LCD_CmdWrite(0x02);//start write data

//#if USR_LCM_IF == 1
//	
//	LCD_CS=0;
//	for(i=0;i<6;i++)
//	{
//		LCMIF_BMM_TXA = &Pic1[i*256];
//		BmmFlag = 1;
//		LCMIF_BMM_CR = 0xc0;	//Write dat(RA8875 RS与标准相反, 0: data; 1: cmd)
//		while(BmmFlag);
//	}
//	LCD_CS=1;
//	
//#else
	
	for(i=0;i<1500;i++)
	{
		LCD_DataWrite(Pic1[i]);
		Chk_Busy();
	}
	Chk_BTE_Busy();
	
//#endif

	//clear screen test:   part of the window 
//	LCD_WR_REG(0X8E,0X80);//Began to clear the screen (display window)
//	Chk_Busy();		
//	LCD_SetWindows(40,150,100,200);//Set the work window size
//	Text_Background_Color1(GREEN);//Set the background color
//	LCD_WR_REG(0X8E,0X40);//Set the screen clearing properties window (work window)
//	LCD_WR_REG(0X8E,0XC0);//Began to clear the screen
//	Chk_Busy();
//	 
//	LCD_SetWindows(150,479,180,271);//Set the work window size
//	Text_Background_Color1(CYAN);//Set the background color
//	LCD_WR_REG(0X8E,0X40);//Set the screen clearing properties window (work window)
//	LCD_WR_REG(0X8E,0XC0);//Began to clear the screen
//	Chk_Busy();

//	/////////Memory write test
//	LCD_WR_REG(0x40,0x00);
//	LCD_SetWindows(0,111,0,139);//Set the work window size	
//	MemoryWrite_Position(0,0);//Memory write position
//	LCD_CmdWrite(0x02);//start data write
//	//112X140 dot
//	for(i=0;i<31360;i++)
//	{
//		LCD_DataWrite(pic[i]);
//		Chk_Busy();
//	}
	delay_ms(800);
	delay_ms(800);
}

void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 color)
{  	
	u16 i,j;			
	u16 width=ex-sx+1; 		//得到填充的宽度
	u16 height=ey-sy+1;		//高度

	LCD_WR_REG(0x40,0x00);
	LCD_SetWindows(sx,ex,sy,ey);//设置显示窗口
	MemoryWrite_Position(0,0);//Memory write position
	LCD_CmdWrite(0x02);//start data write

#if USR_LCM_IF == 1
	
	LCMIF_BMM_TXA = &Color;
	for(j=0,i=0;i<256;i++)
	{
		Color[i] = color;
	}
	index = 3000;		//8bit
	LCD_CS=0;
	LCMIF_BMM_CR = 0xc0;	//Write dat(RA8875 RS与标准相反, 0: data; 1: cmd)
	P40 = 0;
	
#else
	
	P40 = 0;
	for(i=0;i<height;i++)
	{
		for(j=0;j<width;j++)
		LCD_WR_DATA(color);	//写入数据 	 
	}
	P40 = 1;
	
#endif
}

void Test_Color(void)
{
	LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
	delay_ms(800);
	LCD_Fill(0,0,LCD_W,LCD_H,RED);
	delay_ms(800);
	LCD_Fill(0,0,LCD_W,LCD_H,GREEN);
	delay_ms(800);
	LCD_Fill(0,0,LCD_W,LCD_H,BLUE);
	delay_ms(800);
}

/*****************************************************************************
 * @name       :void LCM_Interrupt(void)
 * @date       :2018-11-13 
 * @function   :None
 * @parameters :None
 * @retvalue   :
******************************************************************************/ 
void LCMIF_BMM_Interrupt(void) interrupt 13
{
	if(LCMIFSTA & 0x01)
	{
		LCMIFSTA = 0x00;
		LcmFlag = 0;
	}
	
	if(LCMIF_BMM_STA & 0x01)
	{
		if(BmmFlag)
		{
			BmmFlag = 0;
			LCMIF_BMM_CR = 0;
		}
		else
		{
			index--;
			if(index == 0)
			{
				LCMIF_BMM_CR = 0;
				LCD_CS=1;
				P40 = 1;
			}
			else
			{
				LCMIF_BMM_CR = 0xc0;	//Write dat(RA8875 RS与标准相反, 0: data; 1: cmd)
			}
		}
		LCMIF_BMM_STA = 0;
	}
}
