📄 lcd.c
字号:
#include "lcd.h"
/*************************************************************************************************/
//内部函数
void CheckBusy (void)
{
P1=0xff;
LcdRs=0;
LcdRw=1;
LcdE=1;
while(Busy==1);
LcdE =0;
}
unsigned char ReadLcd () //写操作
{
unsigned char Rddata;
CheckBusy ();
LcdRs=1; //数据
LcdRw=1; //读
LcdE=1;
Rddata=LCD_DATA;
LcdE=0;
return Rddata;
}
void WriteLcd (unsigned char dat_type,unsigned char content) //写操作
{
CheckBusy ();
if(dat_type)
{
LcdRs=1; //数据
LcdRw=0; //写
}
else
{
LcdRs=0; //指令
LcdRw=0; //写
}
LCD_DATA=content;
LcdE=1;
LcdE=0;
}
//用户函数
void Initlcd (void) //初始化LCD
{
WriteLcd (LCDCOMM,0x30); //基本指令动作
WriteLcd (LCDCOMM,0x01); //清屏,地址指针指向00
WriteLcd (LCDCOMM,0x06); //光标的移动方向
WriteLcd (LCDCOMM,0x0c); //开显示,关游标
}
void ClearRam (void) //清RAM
{
WriteLcd (LCDCOMM,0x30);
WriteLcd (LCDCOMM,0x01);
}
void ChnPrintf (unsigned char x,unsigned char y,unsigned char code *chn) //显示汉字或字符
{
xdata unsigned char i,len;
WriteLcd (LCDCOMM,0x30);
for(len=0;*chn!='\0';chn++,len++);
chn-=len;
switch(x)
{
case 1:
WriteLcd (LCDCOMM,0x80+y-1);
for (i=0;i<16-2*(y-1)&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
WriteLcd (LCDCOMM,0x90);
for (;i<16-2*(y-1)+16&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
WriteLcd (LCDCOMM,0x88);
for (;i<16-2*(y-1)+32&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
WriteLcd (LCDCOMM,0x98);
for (;i<16-2*(y-1)+48&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
break;
case 2:
WriteLcd (LCDCOMM,0x90+y-1);
for (i=0;i<16-2*(y-1)&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
WriteLcd (LCDCOMM,0x88);
for (;i<16-2*(y-1)+16&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
WriteLcd (LCDCOMM,0x98);
for (;i<16-2*(y-1)+32&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
break;
case 3:
WriteLcd (LCDCOMM,0x88+y-1);
for (i=0;i<16-2*(y-1)&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
WriteLcd (LCDCOMM,0x98);
for (;i<16-2*(y-1)+16&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
break;
case 4:
WriteLcd (LCDCOMM,0x98+y-1);
for (i=0;i<16-2*(y-1)&&i<len;i++)
WriteLcd (LCDDAT,chn[i]);
break;
}
}
void ImgPrintf (unsigned char x,unsigned char y,unsigned char Row,unsigned char Lis,unsigned char code img[][2]) //显示图形
{
xdata unsigned char i,j;
for(i=0;i<Row;i++)
for(j=0;j<Lis;j++)
{
if(y+i>32)
{
WriteLcd (LCDCOMM,0x34);
WriteLcd (LCDCOMM,Y+y+i-33);
WriteLcd (LCDCOMM,X+x+j+7);
}
else
{
WriteLcd (LCDCOMM,0x34);
WriteLcd (LCDCOMM,Y+y+i-1);
WriteLcd (LCDCOMM,X+x+j-1);
}
WriteLcd (LCDCOMM,0x30);
WriteLcd (LCDDAT,img[i*Lis+j][0]);
WriteLcd (LCDDAT,img[i*Lis+j][1]);
}
WriteLcd (LCDCOMM,0x36);
}
void DotPrintf (unsigned char data1,unsigned char data2) //显示点阵
{
xdata unsigned char i,j,k,x;
x=X;
for(k=0;k<2;k++)
{
for(j=0;j<16;j++)
{
for(i=0;i<8;i++)
{
WriteLcd (LCDCOMM,0x34);
WriteLcd (LCDCOMM,Y+j*2);
WriteLcd (LCDCOMM,x+i);
WriteLcd (LCDCOMM,0x30);
WriteLcd (LCDDAT,data1);
WriteLcd (LCDDAT,data1);
}
for(i=0;i<8;i++)
{
WriteLcd (LCDCOMM,0x34);
WriteLcd (LCDCOMM,Y+j*2+1);
WriteLcd (LCDCOMM,x+i);
WriteLcd (LCDCOMM,0x30);
WriteLcd (LCDDAT,data2);
WriteLcd (LCDDAT,data2);
}
}
x=XX;
}
WriteLcd (LCDCOMM,0x36);
}
void DisPrintf (unsigned char data1,unsigned char data2,unsigned char x0,unsigned char y0,unsigned char x1,unsigned char yl) //反白显示
{
xdata unsigned char i,j;
for(j=0;j<yl;j++)
{
for(i=0;i<x1;i++)
{
WriteLcd (LCDCOMM,0x34); //扩充指令集,绘图关
WriteLcd (LCDCOMM,y0+j);
WriteLcd (LCDCOMM,x0+i);
WriteLcd (LCDCOMM,0x30); //基本指令集
WriteLcd (LCDDAT,data1);
WriteLcd (LCDDAT,data2);
}
}
WriteLcd (LCDCOMM,0x36); //扩充指令集,绘图开
}
void StdIntPrintf(unsigned char x,unsigned char y,signed int i)//将一个整数直接在屏幕上打印出来,可以显示的数据的范围:-32767到+32767
{
unsigned char code StdNumber[11][3]={"0","1","2","3","4","5","6","7","8","9","10"};
xdata unsigned char m[5];
xdata unsigned char n=0,fushu=0;
if (i<0)
{
i=-i;
fushu=1;
}
while(i!=0)
{
m[n]=i%10;
i/=10;
n++;
}
if (fushu)
{
ChnPrintf(x,y,"-");
y++;
}
for(i=0;i<n;i++)
ChnPrintf(x,y+i,StdNumber[m[n-i-1]]);
}
void PixelPrintf (unsigned char x,unsigned char y,unsigned char real) //显示图形
{
xdata unsigned char LcdData0,LcdData1,LcdData2=0x80,LcdData3=0x80;
LcdData2=0x80;
LcdData3=0x80;
if(x%16==0)
{
LcdData3>>=7;
LcdData2=0x00;
}
else if(x%16<8)
{
LcdData2>>=x%16-1;
LcdData3=0x00;
}
else if(x%8==0)
{
LcdData2>>=7;
LcdData3=0x00;
}
else
{
LcdData3>>=(x%16-9);
LcdData2=0x00;
}
if(y>32)
{
y-=32;
x+=128;
}
WriteLcd (LCDCOMM,0x34);
WriteLcd (LCDCOMM,Y+y-1);
WriteLcd (LCDCOMM,X+(x-1)/16);
WriteLcd (LCDCOMM,0x30);
LcdData0=ReadLcd();
LcdData0=ReadLcd();
LcdData1=ReadLcd();
WriteLcd (LCDCOMM,0x34);
WriteLcd (LCDCOMM,Y+y-1);
WriteLcd (LCDCOMM,X+(x-1)/16);
WriteLcd (LCDCOMM,0x30);
if(real)
{
WriteLcd (LCDDAT,LcdData0|LcdData2);
WriteLcd (LCDDAT,LcdData1|LcdData3);
}
else
{
LcdData2^=0xff;
LcdData3^=0xff;
WriteLcd (LCDDAT,LcdData0&(LcdData2));
WriteLcd (LCDDAT,LcdData1&(LcdData3));
}
WriteLcd (LCDCOMM,0x36);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -