|
发表于 2006-9-9 19:19:00
|
显示全部楼层
下面是摘录:
http://file.21ic.com/offline/2003/11/508073.htm
TRULY 12864A8-3 LCD读写程序,已经测试过。
#define LCDDATA XBYTE[0x8000] /* 液晶显示器数据端口 */ #define BPORT1 XBYTE[0x8400] /*控制端口;D0=LCM_RS, D1=LCM_EN, D2=LCM_CS1, D3=LCM_CS2, D4=LCM_RW*/ unsigned char bdata Mport1; /*BPORT1内存映射*/ sbit LCD_RS=Mport1^0; sbit LCD_E =Mport1^1; sbit LCD_C1=Mport1^2; sbit LCD_C2=Mport1^3; sbit LCD_RW=Mport1^4; #define DCS1_ON 0 /* 选择CS1 */ #define DCS1_OFF 1 /* 释放CS1 */ #define DCS2_ON 0 #define DCS2_OFF 1 #define CONTRAST_DISP 1 /* 显示属性-反白显示 */ #define NORMAL_DISP 0 /* 显示属性-正常显示 */ #define FILL_LINE 1 /* 行填充*/ #define CLEAR_LINE 0 /* 行填充*/ /* 向TRULY MCG12864A8-3写命令 */ void SendCommand(unsigned char b) { unsigned char i; LCD_RS = 0; BPORT1=Mport1; LCD_RW = 0; BPORT1=Mport1; LCDDATA = b; LCD_E = 0; BPORT1=Mport1; for(i=0;i<2;i++); LCD_E = 1; BPORT1=Mport1; for(i=0;i<2;i++); LCD_E = 0; BPORT1=Mport1; for(i=0;i<2;i++); } /* 向TRULY MCG12864A8-3写显示数据 */ void SendData(unsigned char b) { unsigned char i; LCD_RS = 1; BPORT1=Mport1; LCD_RW = 0; BPORT1=Mport1; LCDDATA = b; LCD_E = 0; BPORT1=Mport1; for(i=0;i<2;i++); LCD_E = 1; BPORT1=Mport1; for(i=0;i<2;i++); LCD_E = 0; BPORT1=Mport1; for(i=0;i<2;i++); } /* TRULY MCG12864A8-3片选 */ void lcdcs(bit cs1, bit cs2) { LCD_C1=cs1; LCD_C2=cs2; BPORT1=Mport1; } /*TRULY MCG12864A8-3锁定*/ void lcdlock(void) { lcdcs( DCS1_OFF , DCS2_OFF ); } /* TRULY MCG12864A8-3清屏初始化 */ void lcdscreenclear(void) { unsigned char i,m,n; lcdcs( DCS1_ON , DCS2_OFF ); SendCommand(0x3f); /*打开液晶*/ SendCommand(0xc0); /*从0行开始*/ m = 0xb8; for( n=0 ; n<8 ; n++ ){ SendCommand(m); /*X坐标(对应屏幕位置为第X行)*/ SendCommand(0x40); /*Y坐标*/ for( i=0 ; i<64 ; i++ ) SendData(0x00); m++; } lcdcs( DCS1_OFF , DCS2_ON ); SendCommand(0x3f); SendCommand(0xc0); m=0xb8; for( n=0 ; n<8 ; n++ ){ SendCommand(m); SendCommand(0x40); for( i=0 ; i<64 ; i++ ) SendData(0x00); m++; } lcdlock(); } /* TRULY MCG12864A8-3行处理 */ /*x=字符行坐标[0x00,0x07];FILL=1 行填充;FILL=0行清除*/ void lcdline(unsigned char x , bit fill) { unsigned char i; lcdcs( DCS1_ON , DCS2_OFF ); SendCommand(0x3f); SendCommand(0xc0); SendCommand( 0xb8|x ); for( i=0 ; i<64 ; i++ ) if(fill) SendData(0xff); else SendData(0x00); lcdcs( DCS1_OFF , DCS2_ON ); SendCommand(0x3f); SendCommand(0xc0); SendCommand( 0xb8|x ); for( i=0 ; i<64 ; i++ ) if(fill) SendData(0xff); else SendData(0x00); lcdlock(); } /*字节倒序*/ unsigned char inva(unsigned char b) { char i; /* 有符合数 */ unsigned char v1 = 0; unsigned char v2 = 0; unsigned char j1 = 0x80; unsigned char j2 = 0x01; for(i = 7; i >= 1 ;i = i-2){ v1 = ((b <>i)&j2)|v2; j1 = j1 >> 1; j2 = j2 << 1; } return(v1|v2); } /******************************************************* ** TRULY MCG12864A8-3同行显示汉字和ASCII码 ** 入口:x--显示起始行坐标[0x00,0x07],以8×8点阵的字符为度量单位 ** y--显示起始列坐标[0x00,0x15],以8×8点阵的字符为度量单位 ** p--显示字符串的指针 ** contrast--显示字符串的属性,=1反白显示 *******************************************************/ void dispbuf(unsigned char x, unsigned char y, unsigned char *p, bit contrast) { unsigned char i,b; unsigned char t; if( y>7 ){ lcdcs( DCS1_OFF , DCS2_ON ); y = y-8; b = 2; } else{ lcdcs( DCS1_ON , DCS2_OFF ); b = 1; } SendCommand(0x3f); SendCommand(0xc0); while( *p ) { t=(*(p++)-1); if(t > 127){ /*汉字*/ t -=128; SendCommand( 0x40 | (y*8) ); SendCommand( 0xb8 | x ); for( i=0 ; i<8 ; i++ ) if(contrast) SendData( ~inva(chinese[t]) ); else SendData( inva(chinese[t]) ); SendCommand( 0x40 | (y*8) ); SendCommand( 0xb8 |(x+1) ); for( i=0 ; i<8 ; i++ ) if(contrast) SendData( ~inva(chinese[t][i+16]) ); else SendData( inva(chinese[t][i+16]) ); y++; if(y==8){ y=0; if( b==1 ){ lcdcs( DCS1_OFF , DCS2_ON ); b = 2; } else{ /*已到行尾,自动换到下一行*/ lcdcs( DCS1_ON , DCS2_OFF ); b = 1; x+= 2; } } SendCommand( 0x40 | (y*8) ); SendCommand( 0xb8 | x ); for( i=0 ; i<8 ; i++ ) if(contrast) SendData( ~inva(chinese[t][i+8]) ); else SendData( inva(chinese[t][i+8]) ); SendCommand( 0x40 |(y*8) ); SendCommand( 0xb8 |(x+1) ); for(i = 0;i < 8;i++) if(contrast) SendData( ~inva(chinese[t][i+24]) ); else SendData( inva(chinese[t][i+24]) ); y++; if( y==8 ){ y=0; if( b==1 ){ lcdcs( DCS1_OFF , DCS2_ON ); b = 2; } else{ lcdcs( DCS1_ON , DCS2_OFF ); b = 1; x += 2; } } } else{ /*ASCII*/ SendCommand( 0x40 | y*8 ); SendCommand( 0xb8 | x ); for( i=0 ; i<8 ; i++ ) if(contrast) SendData( ~inva(ascii[t+1]) ); else SendData( inva(ascii[t+1]) ); SendCommand( 0x40 | y*8 ); SendCommand( 0xb8 | (x+1) ); for( i=0 ; i<8 ; i++ ) if(contrast) SendData( ~inva(ascii[t+1][i+8]) ); else SendData( inva(ascii[t+1][i+8]) ); y++; if(y==8){ y = 0; if( b==1 ){ lcdcs( DCS1_OFF , DCS2_ON ); b=2; } else{ lcdcs( DCS1_ON , DCS2_OFF ); b=1; x+=2; } } } } lcdlock(); } |
|