//延时
lcd12864delay()
{
unsigned int i=500;
while(i--) {;}
}
/*----------------------------------------------------------------------------------------------------*/
//状态检查
void checkstate()
{
unsigned char dat;
di=0;
rw=1;
do{
lcd12864dataport=0xff;
en=1; dat=lcd12864dataport; en=0;
dat=0x90 & dat; //仅当第4,7位为0时才可操作
}while(!(dat==0x00));
}
/*----------------------------------------------------------------------------------------------------*/
//写显示数据
//dat:显示数据
void writebyte(unsigned char dat)
{
checkstate();
di=1; rw=0;
lcd12864dataport=dat;
en=1; en=0;
}
/*-----------------------------------------------------------------------------------------------------*/
//向lcd发送命令
//command :命令
sendcommandtolcd(unsigned char command)
{
checkstate();
rw=0;
lcd12864dataport=command;
en=1; en=0;
}
/*----------------------------------------------------------------------------------------------------*/
//设定行地址(页)--x 0-7
void setline(unsigned char line)
{
line=line & 0x07; // 0<=line<=7
line=line|0xb8; //1011 1xxx
sendcommandtolcd(line);
}
//设定列地址--y 0-63
void setcolumn(unsigned char column)
{
column=column &0x3f; // 0=<column<=63
column=column | 0x40; //01xx xxxx
sendcommandtolcd(column);
}
//设定显示开始行--xx
void setstartline(unsigned char startline) //0--63
{
startline=startline & 0x07;
startline=startline|0xc0; //1100 0000
sendcommandtolcd(startline);
}
//开关显示
void setonoff(unsigned char onoff)
{
onoff=0x3e | onoff; //0011 111x
sendcommandtolcd(onoff);
}
/*---------------------------------------------------------------------------------------------------*/
//选择屏幕
//screen: 0-全屏,1-左屏,2-右屏
void selectscreen(unsigned char screen)
{ //北京显示器:负有效 cs1: 0--右; cs2: 0--左
switch(screen)
{ case 0: cs1=0;//全屏
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
cs2=0;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
break;
case 1: cs1=1;//左屏
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
cs2=0;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
break;
case 2: cs1=0;//右屏
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
cs2=1;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
break;
}
}
/*---------------------------------------------------------------------------------------------------*/
//清屏
//screen: 0-全屏,1-左屏,2-右
void clearscreen(unsigned char screen)
{ unsigned char i,j;
selectscreen(screen);
for(i=0;i<8;i++)
{ setline(i);
for(j=0;j<64;j++)
{
writebyte(0x00);
}
}
}
/*--------------------------------------------------------------------------------------------------*/
//显示8*8点阵
//旋转90度:字模被竖着切分
//lin:行(0-7), column: 列(0-15)
//address : 字模区首地址
void show88(unsigned char lin,unsigned char column,unsigned int address)
{ unsigned char i;
if(column>16) {return;}
if(column<8) selectscreen(1); //如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上
else {
selectscreen(2); //否则 (8,9,10,11,12,13,14,15)写在第二屏上
column=column & 0x07; //防止越界
}
setline(lin);
setcolumn(column<<3);
for(i=0;i<8;i++) writebyte( cbyte[address+i] );
}
/*------------------------------------------------------------------------------------------------*/
//显示8*16字符
//旋转90度:字模被竖着切分
//lin:行(0-3), column: 列(0-15)
//character:字符代码(标准ascii码)
void showchar(unsigned char lin,unsigned char column,unsigned char character)
{ lin=lin<<1;
show88(lin ,column,ascii816[character-0x20] );
show88(lin+1,column,ascii816[character-0x20]+8 );
}
/*-----------------------------------------------------------------------------------------------*/
/*
//显示8*16字符串
//!!!只能显示在一行上即: 串长+column <=15
//旋转90度:字模被竖着切分
//lin:行(0-3), column: 列(0-15)
//address : 字模区首地址
void showstring(unsigned char lin,unsigned char column, unsigned char *string)
{ unsigned char ch;
unsigned char i=0;
while(*string!='\0')
{
ch=*string;
if(i+column >15) break; //(只能显示在一行上即: 串长+column <=15)
showchar(lin,i+column,ch);
string++; i++;
}
}
*/
/*----------------------------------------------------------------------------------------------*/
//显示一个汉字
//旋转90度:字模被竖着切分
//lin:行(0-3), column: 列(0-7)
//hzcode: 汉字代码(自定义的)
//uchar code hztable
void showhz(unsigned char lin,unsigned char column,unsigned int hzcode)
{
lin=lin<<1; //lin*2
show88(lin,column,hztable[hzcode]);
show88(lin,column+1,hztable[hzcode]+8);
show88(lin+1,column,hztable[hzcode]+16);
show88(lin+1,column+1,hztable[hzcode]+24);
}
/*----------------------------------------------------------------------------------------------*/
//显示图片
//旋转90度 :字模被竖着切分
//startline :开始行
//startcolumn:开始列
//linechar :图片行点除8(图片以8*8点阵为单位)
//columnchar :图片列点除8(图片以8*8点阵为单位)
//address : 字模区首地址
void showpicture(unsigned char startline,unsigned char startcolumn,unsigned char linechar,unsigned char columnchar,unsigned int address)
{ unsigned char i,j;
for(i=0;i<columnchar;i++)
for(j=0;j<linechar;j++)
show88(startline+i , startcolumn+j , address+(i*linechar+j)*8 );
}
/*----------------------------------------------------------------------------------------------*/
//显示一行字符串(汉字,字母混排,一行16字节)
//lin:行
//lineheadaddr: 此行汉字代码区首地址
void showline(unsigned char lin,unsigned char linehead[])
{ unsigned char i;
unsigned char byte;
unsigned int hzcode;
for( i=0; i<16; )
{ byte=linehead[i];
if(byte < 0x80) //字母
{
if(i>15) return; //!!编译器有问题,须强制退出
showchar(lin, i , byte);
i=i+1;
}
else // byte >= 0x80(汉字)
{
if(i>15) return; //!!编译器有问题,须强制退出
byte=byte & 0x7f; //最高位置0,即:减去0x8000
hzcode=(unsigned int)byte<<8; //?? //高8位
hzcode=hzcode+linehead[i+1]; //加低8位,组合成整型数地址
showhz( lin,i,hzcode);
i=i+2;
}
}
}
/*----------------------------------------------------------------------------------------------------*/
//显示一屏汉字
//pageheadaddr:此屏汉字代码地址区首地址
void showpage(unsigned char pagehead[][16])
{ unsigned char i;
for(i=0;i<4;i++) showline(i,pagehead[i]); //1行8个汉字,16字节
}
/*----------------------------------------------------------------------------------------------------*/
//读显示数据
unsigned char readbyte()
{
unsigned char dat;
checkstate();
di=1; rw=1;
lcd12864dataport=0xff;
en=1; dat=lcd12864dataport; en=0;
return(dat);
}
/*-----------------------------------------