//------------------------------------------------- // Shenzhen TOPWAY Technology Co.,Ltd. // System: AT89S52(12MHz) // LCD Module: LMT056DIDFWD-xAx // Display Size: 640(x3)x480 (16bit color) // Driver/Controller: RA8875 // Interface: 80 mode (8bit) // by: dong // rev 01: 2016-12-15 //------------------------------------------------- #include #include #include #include #define uchar unsigned char // 8bit, 0 ~ 255 #define uint unsigned int // 16bit, 0 ~ 65,535 #define ulong unsigned long // 32bit, 0 ~ 4,294,967,295 #define RED 0xf800 #define GREEN 0x07e0 #define BLUE 0x001f #define YELLOW 0xffe0 #define CYAN 0x07ff #define MAGENTA 0xf81f #define BLACK 0x0000 #define WHITE 0xffff #define mid_re 0x8000 #define mid_or 0x8200 #define mid_ye 0x8400 #define mid_yg 0x4400 #define mid_gr 0x0400 #define mid_gc 0x0408 #define mid_cy 0x0410 #define mid_cb 0x0210 #define mid_bl 0x0010 #define mid_bm 0x4010 #define mid_ma 0x8010 #define mid_mr 0x8010 #define GRAY00 0x0000 #define GRAY01 0x1082 #define GRAY02 0x2104 #define GRAY03 0x3186 #define GRAY04 0x4208 #define GRAY05 0x528a #define GRAY06 0x630c #define GRAY07 0x738e #define GRAY08 0x8410 #define GRAY09 0x9492 #define GRAY10 0xa514 #define GRAY11 0xb596 #define GRAY12 0xc618 #define GRAY13 0xd69a #define GRAY14 0xe71c #define GRAY15 0xffff uchar xdata CmdPort _at_ 0x8000;//0x8000 uchar xdata DataPort _at_ 0x0000; // using 8051 RAM bus as // LCD.RS connect 8051.A15 // LCD.CS connect 8051.P3.2 // LCD.WR connect 8051.WR // LCD.RD connect 8051.RD // i.e. // pointer 0x0xxx for RS=0 // pointer 0x8xxx for RS=1 sbit _CS = P3^2; // TP.CS sbit RST = P3^4; uchar bdata btemp; // create a bit accessable byte sbit btemp_b0 = btemp^0; sbit btemp_b3 = btemp^3; sbit btemp_b7 = btemp^7; uchar BL_Brightness; // 0~255 value (0%~100%) uchar PAG_NO; sbit _KeySel = P3^0; // _KeySel=0, key scanning active sbit _KeyAuto = P1^0; // _KeyAuto=0, key pressed sbit _KeyDarker = P1^1; // _KeyDarker=0, key pressed sbit _KeyLighter = P1^2; // _KeyLighter=0,key pressed sbit _KeyStep = P1^3; // _KeyStep=0, key pressed bit AutoRun; //----------------------------------- // delayms routine //----------------------------------- void delayms(uint m) // 12MHz Xtal, close to ms value { uint j; uint i; for(i=0; i>8; DataPort = DData; // write the low byte (toggle /WR) _CS=1; } uchar RdCmd() { uchar DData; _CS=0; DData=DataPort; // write the low byte (toggle /WR) _CS=1; return(DData); } //============================================================= // 以下是标准初始化 //============================================================= void initLCM() { BL_Brightness=0x4C; //------------------------------------------------------------------------------- //系统频率设定为30MHz,应用于640x480,71Hz屏幕 SdReg(0x88);SdCmd(0x05);delayms(5);//倍频PLL=20*(5+1) = 120MHz SdReg(0x89);SdCmd(0x02);delayms(5);//分频得到系统频率=120/4=30MHz SdReg(0x04);SdCmd(0x80);//面板驱动频率30MHz,时钟下降沿 //------------------------------------------------------------------------------- //System Configuration Register (SYSR) //7~4 3~2 1~0 //N/A Color Depth Setting MCUIF SdReg(0x10);SdCmd(0x0c); //[3:2]=00: 256, 1x:65K, [1~0]=00: 8bit, 1x:16bit //------------------------------------------------------------------------------- //LCD Horizontal Display Width Register (HDWR) SdReg(0x14);SdCmd(0x4F); //(HDWR + 1)*8 ==> 640 SdReg(0x15);SdCmd(0x06); //Horizontal Non-Display Period Fine Tuning Option Register (HNDFTR) ===>default SdReg(0x16);SdCmd(0x12); //LCD Horizontal Non-Display Period Register (HNDR) SdReg(0x17);SdCmd(0x01); //HSYNC Start Position Register (HSTR+1)*8 ===>LCM Parameter 16 SdReg(0x18);SdCmd(0x00); //HSYNC Pulse Width Register (HPW+1)*8 ==> LCM Parameter 8 //------------------------------------------------------------------------------- //Vertical Display Height Register (VDHR0) SdReg(0x19);SdCmd(0xdf); //VDHR0 //Vertical Display Height Bit [7:0],7~0bit SdReg(0x1a);SdCmd(0x01); //8bit ===>8~0bit+1 ==>480 SdReg(0x1b);SdCmd(0x2c); //VNDR0 //Vertical Non-Display Period Bit [7:0],Vertical Non-Display area=(VNDR+1) 44 SdReg(0x1c);SdCmd(0x00); SdReg(0x1d);SdCmd(0x00); //VSTR0 //VSYNC Start Position[7:0],/VSYNC Start Position(PCLK) = (VSTR+1)6 SdReg(0x1f);SdCmd(0x01); //VPWR:VSYNC Polarity ,VSYNC Pulse Width[6:0],VSYNC Pulse Width(PCLK)=(VPWR+1) //------------------------------------------------------------------------------- //be in a poor light set SdReg(0x8a);SdCmd(0x85); //3.6kHz SdReg(0x8b);SdCmd(BL_Brightness); //------------------------------------------------------------------------------- //font selection==>CGRAM_Font + External_Font, GT23L24T3Y SdReg(0x20);SdCmd(0x00); // signal layer SdReg(0x21);SdCmd(0x20); //external font SdReg(0x29);SdCmd(0x01); //font line distance SdReg(0x05);SdCmd(0x08); //1 byte none wait SdReg(0x2e);SdCmd(0x00); //default font_type==>16X16 SdReg(0x2f);SdCmd(0x41); //font-lib SdReg(0x44);SdCmd(0x1a); //文字闪烁时间 SdReg(0x4e);SdCmd(0x01); //光标水平大小 SdReg(0x4f);SdCmd(0x0f); //光标垂直大小 SdReg(0x01);SdCmd(0x80); } //----------------------------------- // Backlight Brightness //----------------------------------- void BL_INC() { uchar i; for(i=0;i<0x10;i++) { if(BL_Brightness>0x00) { BL_Brightness=BL_Brightness-1; } } SdReg(0x8b); SdCmd(BL_Brightness); } void BL_DEC() { uchar i; for(i=0;i<0x10;i++) { if(BL_Brightness<0xFF) { BL_Brightness=BL_Brightness+1; } } SdReg(0x8b); SdCmd(BL_Brightness); } //------------------------------------------------------------- //=============================================== // 打字函数 // 输入参数包括坐标x,y,颜色值,放大系数1~4,字符组 //=============================================== void PrintGB2(uint x, uint y, uint GBcolor, uchar size, uchar *GBcode) { uint temp; switch(size) { case 1: size=0x60; break; case 2: size=0x65; break; case 3: size=0x6a; break; case 4: size=0x6f; break; default: break; } SdReg(0x40);SdCmd(0x80); //光标设定 SdReg(0x21);SdCmd(0x20); //字库选择 SdReg(0x06);SdCmd(0x00); //字库工作频率 SdReg(0x05);SdCmd(0x00); //字库工作模式配置 SdReg(0x22);SdCmd(size); //字体背景透明,放大倍数设定 SdReg(0x2e);SdCmd(0x00); //字型点阵数大小16x16 SdReg(0x2f);SdCmd(0x41); //字库型号,字码类型选择 41 SdReg(0x30);SdCmd(0);//窗口水平起始点 SdReg(0x31);SdCmd(0); SdReg(0x32);SdCmd(0);//窗口垂直起始点 SdReg(0x33);SdCmd(0); SdReg(0x34);SdCmd(0x1f);//窗口水平结束点 SdReg(0x35);SdCmd(0x03); SdReg(0x36);SdCmd(0xdf);//窗口垂直结束点 SdReg(0x37);SdCmd(0x01); SdReg(0x63);SdCmd(GBcolor>>11);//设定颜色,红段 SdReg(0x64);SdCmd(GBcolor>>5);//设定颜色,绿段 SdReg(0x65);SdCmd(GBcolor);//设定颜色,蓝段 SdReg(0x2a);SdCmd(x);//水平光标位置 SdReg(0x2b);SdCmd(x>>8); SdReg(0x2c);SdCmd(y);//垂直光标设定 SdReg(0x2d);SdCmd(y>>8); SdReg(0x02); while(*GBcode>0) { temp=*GBcode++; // temp=temp+*pstr++; SdCmd(temp);delayms(1); } } //=============================================== // 设定图形写入区域(窗口区域) //=============================================== void Window_adr_set(uint x1,x2,y1,y2) { SdReg(0x30);SdCmd(x1);// 水平起始点 SdReg(0x31);SdCmd(x1>>8); SdReg(0x32);SdCmd(y1); //垂直起始点 SdReg(0x33);SdCmd(y1>>8); SdReg(0x34);SdCmd(x2); //水平结束点 SdReg(0x35);SdCmd(x2>>8); SdReg(0x36);SdCmd(y2); //垂直结束点 SdReg(0x37);SdCmd(y2>>8); SdReg(0x46);SdCmd(x1);// 光标起始位置 SdReg(0x47);SdCmd(x1>>8); SdReg(0x48);SdCmd(y1); SdReg(0x49);SdCmd(y1>>8); SdReg(0x02); //开始写入 } void ShowBMP(uint x,uint y,uint width,uint high,uint *pstr) { uint i,j; SdReg(0x40);SdCmd(0x00); SdReg(0x30);SdCmd(x);// SdReg(0x31);SdCmd(x>>8);// SdReg(0x32);SdCmd(y);// SdReg(0x33);SdCmd(y>>8);// SdReg(0x34);SdCmd(x+width-1);// SdReg(0x35);SdCmd((x+width-1)>>8);// SdReg(0x36);SdCmd(y+high-1);// SdReg(0x37);SdCmd((y+high-1)>>8);// SdReg(0x46);SdCmd(x);// SdReg(0x47);SdCmd(x>>8);// SdReg(0x48);SdCmd(y);// SdReg(0x49);SdCmd(y>>8);// SdReg(0x02); for(i=0;i>8); SdReg(0x93);SdCmd(y1); //垂直起始点 SdReg(0x94);SdCmd(y1>>8); SdReg(0x95);SdCmd(x2); //水平结束点 SdReg(0x96);SdCmd(x2>>8); SdReg(0x97);SdCmd(y2); //垂直结束点 SdReg(0x98);SdCmd(y2>>8); SdReg(0x63);SdCmd(Color>>11);//设定颜色,红段 SdReg(0x64);SdCmd(Color>>5);//设定颜色,绿段 SdReg(0x65);SdCmd(Color);//设定颜色,蓝段 SdReg(0x90);SdCmd(0x80);//开始画线 0x80 delayms(5); SdReg(0x90);SdCmd(0x00);//结束画线 } //======================================================= // 画矩形 //======================================================= void Draw_box(uint x1,x2,y1,y2,Color) { //设定绘图区为全屏区域内 SdReg(0x30);SdCmd(0);// 水平起始点 SdReg(0x31);SdCmd(0); SdReg(0x32);SdCmd(0); //垂直起始点 SdReg(0x33);SdCmd(0); SdReg(0x34);SdCmd(0xff); //水平结束点 SdReg(0x35);SdCmd(0xff); SdReg(0x36);SdCmd(0xff); //垂直结束点 SdReg(0x37);SdCmd(0xff); //设定坐标 SdReg(0x91);SdCmd(x1);// 水平起始点 SdReg(0x92);SdCmd(x1>>8); SdReg(0x93);SdCmd(y1); //垂直起始点 SdReg(0x94);SdCmd(y1>>8); SdReg(0x95);SdCmd(x2); //水平结束点 SdReg(0x96);SdCmd(x2>>8); SdReg(0x97);SdCmd(y2); //垂直结束点 SdReg(0x98);SdCmd(y2>>8); SdReg(0x63);SdCmd(Color>>11);//设定颜色,红段 SdReg(0x64);SdCmd(Color>>5);//设定颜色,绿段 SdReg(0x65);SdCmd(Color);//设定颜色,蓝段 SdReg(0x90);SdCmd(0xb0);//开始画矩形 delayms(100); SdReg(0x90);SdCmd(0x00);//结束画矩形 } //----------------------------------- // wait... //----------------------------------- void Touch() { uint TP_x,TP_y; uchar TP_xy; SdReg(0x71);SdCmd(0x41); SdReg(0x74);TP_xy=RdCmd(); if((TP_xy&0x80)==0x00) { delayms(50); SdReg(0x74);TP_xy=RdCmd(); if((TP_xy&0x80)==0x00) { SdReg(0x71);SdCmd(0x42);//X轴获取 delayms(100); SdReg(0x71);SdCmd(0x43);//Y轴获取 delayms(100); SdReg(0x71);SdCmd(0x40); SdReg(0x72);TP_x=RdCmd();//得到x轴高位 SdReg(0x73);TP_y=RdCmd();//得到y轴高位 SdReg(0x74);TP_xy=RdCmd();//得到x,y轴低位 TP_xy = TP_xy & 0x0f; TP_x = ((TP_x<<2)|(TP_xy&0x03)); TP_y = ((TP_y<<2)|((TP_xy>>2)&0x03)); if((TP_y<200)&&(TP_x>150)&&(TP_x<900)) { BL_INC();delayms(50); } else if((TP_y>800)&&(TP_x>150)&&(TP_x<900)) { BL_DEC();delayms(50); } } } } //----------------------------------- // Main Program主程序 //----------------------------------- void main() { SP=0x60; EA=0; // no interrupt _CS=1; RST=1; RST=0; delayms(10); RST=1; delayms(50); initLCM(); delayms(800); SdReg(0x8c);SdCmd(0x40); //开背光 // SdReg(0x8c);SdCmd(0x00); //关背光 Draw_box(0,639,0,479,RED); delayms(1000); Draw_box(0,639,0,479,GREEN); delayms(1000); Draw_box(0,639,0,479,BLUE); delayms(1000); Draw_box(0,639,0,479,WHITE); delayms(1000); Draw_box(0,639,0,479,BLACK); delayms(1000); Draw_box(0,639,0,479,GRAY10); PrintGB2(50,10,BLUE,3,"新葡萄京官网科技开发有限企业"); PrintGB2(50,80,BLUE,1,"SHENZHEN TOPWAY TECHNOLOGY CO.,LTD"); PrintGB2(150,130,YELLOW,2,"型号:LMT056DIFWD-xGx"); PrintGB2(150,170,YELLOW,2,"分辨率:640x480"); PrintGB2(150,210,YELLOW,2,"工作电压:5.0V"); PrintGB2(150,250,YELLOW,2,"接口模式:8080"); PrintGB2(150,290,YELLOW,2,"内建字库:GB2312汉字库"); PrintGB2(50,340,BLACK,1,"总部地址:中国深圳市南山区松白公路百旺信工业区5区20栋"); PrintGB2(50,360,BLACK,1,"深圳华强展台:中国深圳市福田区华强北路,华强电子世界,3号楼,1层A431"); PrintGB2(50,380,BLACK,1,"深圳都会展厅:中国深圳市福田区中航路,都会电子城,3楼C509室"); PrintGB2(50,400,BLACK,1,"上海办事处:中国上海市松江区新飞路1500号上海松江创业中心51栋"); PrintGB2(50,420,BLACK,1,"销售热线:+86(755)81795700 +86(755)81795988"); PrintGB2(50,440,RED,2,"详情请登陆www.topwaydisplay.com"); while(1); }