12864液晶4位并行接口驱动程序.docx
《12864液晶4位并行接口驱动程序.docx》由会员分享,可在线阅读,更多相关《12864液晶4位并行接口驱动程序.docx(16页珍藏版)》请在冰豆网上搜索。
12864液晶4位并行接口驱动程序
#include
#include
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#defineDATA1
#defineCOMMAND0
sbitrs=P2^4;
sbitrw=P2^5;
sbiten=P2^6;
sbitpsb=P2^1;
sbitrst=P2^3;
sbitkey=P1^0;
ucharcodeAC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,//第一行汉字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,//第二行汉字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,//第三行汉字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,//第四行汉字位置
};
ucharcodestr1[]="黄河远上白云间,一片孤城万仞山。
羌笛何须怨柳,春风不度玉门关。
";
ucharcodebmp1[]={
//********************************************************//
//位图点阵占用1024字节
//********************************************************//
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x80,0x00,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x3F,0xC0,0x00,0x07,0x07,0x00,0x04,0x01,0x00,0x00,0x38,0x60,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x78,0xE0,0x00,0x3F,0x83,0x80,0x06,0x03,0x80,0x00,0x1C,0x70,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x02,0x38,0x70,0x00,0xFF,0x07,0x80,0x06,0x39,0x80,0x00,0x1F,0x70,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x02,0x1C,0x30,0x00,0x7C,0x0E,0x00,0x06,0x19,0xC0,0x00,0x1F,0x80,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x02,0x1C,0x30,0x00,0x3E,0x1C,0x00,0x06,0x1F,0xC0,0x00,0x7F,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x03,0x3E,0x30,0x00,0x62,0x1A,0x00,0x0F,0x79,0x00,0x03,0xFE,0x00,0x00,0x01,
0x80,0x00,0x00,0x60,0x0F,0x00,0x3F,0x00,0x00,0x00,0x3C,0x00,0xC0,0x00,0x00,0x01,
0x80,0x07,0x76,0x18,0x00,0xC3,0xC1,0x00,0x1F,0xFB,0x00,0x03,0xFE,0x00,0x00,0x01,
0x80,0x00,0x01,0xE0,0x3F,0xC0,0xF1,0xC0,0x00,0x00,0xF0,0x01,0xC0,0x00,0x00,0x01,
0x80,0x07,0x6E,0x18,0x00,0xCF,0xC1,0x80,0x7E,0x3F,0x80,0x01,0xE2,0x00,0x00,0x01,
0x80,0x00,0x07,0xE0,0x7F,0xE0,0xE0,0xE0,0x00,0x03,0xC0,0x03,0xC0,0x00,0x00,0x01,
0x80,0x07,0x7F,0x98,0x00,0xFE,0x83,0x81,0xFC,0x1E,0xC0,0x00,0x83,0x00,0x00,0x01,
0x80,0x00,0x0F,0xE0,0x63,0xE1,0xE0,0xE0,0x00,0x07,0x00,0x03,0xC0,0x00,0x00,0x01,
0x80,0x06,0xFF,0xD8,0x01,0xFA,0x87,0x00,0xFD,0xF8,0xC0,0x01,0x73,0x00,0x00,0x01,
0x80,0x00,0x01,0xE0,0xC1,0xE1,0xE0,0xE0,0x00,0x0F,0x00,0x07,0xC0,0x00,0x00,0x01,
0x80,0x0F,0xD9,0xD8,0x03,0xE3,0x0E,0x80,0xFE,0x7E,0xC0,0x03,0xF9,0x80,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0xE1,0xF0,0xE0,0x00,0x1E,0x00,0x0B,0xC0,0x00,0x00,0x01,
0x80,0x0D,0xB8,0x18,0x1F,0xC3,0x08,0xC0,0x0E,0x6D,0x80,0x03,0xF1,0x80,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0xE0,0xFD,0xC7,0xEF,0x9E,0x00,0x13,0xC0,0x00,0x00,0x01,
0x80,0x0D,0xFE,0x18,0x0F,0xC2,0x10,0xE0,0x1C,0x3F,0x00,0x03,0xE0,0xC0,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0xC0,0xFF,0x83,0xC6,0x1F,0xE0,0x13,0xC0,0x00,0x00,0x01,
0x80,0x0D,0xCE,0x18,0x06,0xC2,0x01,0xC0,0x38,0x3E,0x00,0x00,0xE8,0xC0,0x00,0x01,
0x80,0x00,0x01,0xE0,0x01,0x80,0x3F,0x81,0xE4,0x3C,0x78,0x23,0xC0,0x00,0x00,0x01,
0x80,0x0C,0x4C,0x18,0x00,0xC2,0x03,0x80,0x78,0x1F,0xF0,0x00,0xFC,0x60,0x00,0x01,
0x80,0x00,0x01,0xE0,0x03,0x80,0x3F,0xC1,0xE8,0x3C,0x78,0x43,0xC0,0x00,0x00,0x01,
0x80,0x0C,0x5E,0x18,0x00,0xC2,0x07,0x00,0x68,0x3F,0xF8,0x03,0xFC,0x22,0x00,0x01,
0x80,0x00,0x01,0xE0,0x03,0x00,0x6F,0xC0,0xF0,0x3C,0x3C,0x83,0xC0,0x00,0x00,0x01,
0x80,0x06,0x3F,0x38,0x01,0x82,0x0E,0x00,0x09,0xF8,0x00,0x03,0xC0,0x32,0x00,0x01,
0x80,0x00,0x01,0xE0,0x06,0x00,0xC3,0xE0,0x78,0x3C,0x3C,0xFF,0xF0,0x00,0x00,0x01,
0x80,0x07,0x10,0x38,0x00,0x82,0x1C,0x00,0x0B,0x99,0xC0,0x03,0x00,0x1A,0x00,0x01,
0x80,0x00,0x01,0xE0,0x0C,0x11,0xC3,0xE0,0x78,0x3C,0x3C,0xFF,0xF0,0x00,0x00,0x01,
0x80,0x03,0xFF,0xF0,0x00,0x02,0x38,0x00,0x08,0x30,0xE0,0x02,0x00,0x1E,0x00,0x01,
0x80,0x00,0x01,0xE0,0x08,0x11,0xC1,0xE0,0x7C,0x3C,0x3C,0xFF,0xF0,0x00,0x00,0x01,
0x80,0x01,0xEF,0xF0,0x00,0x00,0xE0,0x00,0x08,0xE0,0x60,0x00,0x00,0x0E,0x00,0x01,
0x80,0x00,0x01,0xE0,0x1F,0xF1,0xC1,0xE0,0xBC,0x1C,0x3C,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x03,0xE0,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x07,0x00,0x01,
0x80,0x00,0x01,0xE0,0x3F,0xE1,0xC1,0xC0,0x9E,0x1E,0x38,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
0x80,0x00,0x01,0xF0,0x7F,0xE0,0xE3,0x83,0x1F,0x0E,0x70,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x0F,0xFC,0xFF,0xE0,0x3F,0x07,0xDF,0x83,0xE0,0x03,0xC0,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x01,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
voidwriteByte(ucharflag,ucharcontent);
voiddelayUs(uintz)
{
while(z--);
}
voiddelay1MS(ucharx)
{
uchark,j;
x=x<<1;
for(k=0;kfor(j=0;j<255;j++)_nop_();
}
/*------------------------------------------------
检测忙位
------------------------------------------------*/
voidCheckBusy()
{
uchartemp1,temp2;
do
{
en=0;
delayUs(5);
rs=0;
rw=1;
en=1;
temp1=P0;//读高字节
delayUs(10);
en=0;
delayUs(5);
rs=0;
rw=1;
en=1;
temp2=P0;//读低字节
en=0;
/*数据处理*/
temp1=temp1&0xf0;
temp2=(temp2&0xf0)>>4;
temp1=(temp1+temp2);
}
while((temp1&0x80)==0x80);
}
voidinit()
{
rst=0;
delayUs(40);
rst=1;
delayUs(40);
psb=1;
delayUs(8);
writeByte(COMMAND,0x20);//基本指令集4bit数据
delayUs(8);
writeByte(COMMAND,0x01);
delayUs(8);
writeByte(COMMAND,0x06);
delayUs(8);
writeByte(COMMAND,0x0c);
delayUs(8);
}
/******************************************************************************************
*函数名称:
SendByte
*功能描述:
串口发送一个字节
*flag=1,写入数据。
flag=0,写入命令。
******************************************************************************************/
voidwriteByte(ucharflag,ucharcontent)
{
CheckBusy();
//delayUs(10);
en=0;
delayUs(5);
if(flag)
rs=1;
else
rs=0;
rw=0;
en=1;
P0=content&0xf0;//数据高4位放到P0口高四位上
delayUs(10);
en=0;
if(flag)
rs=1;
else
rs=0;
delayUs(5);
rw=0;
en=1;
P0=(content<<4)&0xf0;//数据低4位放到P0口高四位上
en=0;
}
/******************************************************************************************
*函数名称eceiveByte
*功能描述:
从串口接收一个字?
*flag=1,读出数据。
flag=0,读出命令
******************************************************************************************/
ucharreceiveByte(ucharflag)
{
uchartemp1,temp2;
CheckBusy();
//delayUs(10);
en=0;
delayUs(5);
if(flag)
rs=1;
else
rs=0;
rw=1;
en=1;
temp1=P0;//读高字节
delayUs(10);
en=0;
if(flag)
rs=1;
else
rs=0;
delayUs(5);
rw=1;
en=1;
temp2=P0;//读低字节
en=0;
/*数据处理*/
temp1=temp1&0xf0;
temp2=(temp2&0xf0)>>4;
return(temp1+temp2);
}
/******************************************************************************************
*函数名称:
LcmClearTXT
*功能描述:
文本区清RAM函数
******************************************************************************************/
voidLcmClearTXT(void)
{
uchari;
writeByte(0,0x20);//4BitMCU,基本指令集合
writeByte(0,0x80);//AC归起始位
for(i=0;i<64;i++)
writeByte(1,0x20);
}
/******************************************************************************************
*函数名称:
LcmClearBMP
*功能描述:
图形区清RAM函数
******************************************************************************************/
voidLcmClearBMP(void)
{
uchari,j;
writeByte(0,0x24);//4Bit扩充指令集
writeByte(0,0x26);//绘图ON
for(i=0;i<32;i++)//12864实际为256x32
{
writeByte(0,0x80|i);//行位置
writeByte(0,0x80);//列位置
for(j=0;j<32;j++)//256/8=32byte
writeByte(1,0x00);
}
}
/******************************************************************************************
*函数名称:
PutStr
******************************************************************************************/
voidPutStr(ucharrow,ucharcol,uchar*puts)
{
writeByte(0,0x20);//4BitMCU,基本指令集合
writeByte(0,AC_TABLE[8*row+col]);//起始位置
while(*puts!
='\0')//判断字符串是否显示完毕
{
if(col==8)//判断换行
{//若不判断,则自动从第一行到第三行
col=0;
row++;
}
if(row==4)row=0;//一屏显示完,回到屏左上角
writeByte(0,AC_TABLE[8*row+col]);
writeByte(1,*puts);//一个汉字要写两次
puts++;
writeByte(1,*puts);
puts++;
col++;
}
}
/******************************************************************************************
*函数名称:
PutBMP
******************************************************************************************/
voidPutBMP(uchar*puts)
{
uintx=0;
uchari,j;
writeByte(0,0x24);//4Bit扩充指令集
writeByte(0,0x26);//绘图ON
for(i=0;i<32;i++)//12864实际为256x32
{
writeByte(0,0x80|i);//行位置
writeByte(0,0x80);//列位置
for(j=0;j<16;j++)//256/8=3