📄 yejing.c
字号:
#include<reg51.h>
#include<intrins.h>// 可以调用_nop_()函数
#define uchar unsigned char
#define uint unsigned int
const uchar NoDisp=0;
const uchar NoCur=1;
const uchar CurNoFlash=2;
const uchar CurFlash=3;
sbit E=P3^4;
sbit RS=P3^5;
sbit RW=P3^6;//与前两个列子相比,增加了读写控制信号,仿真前要修改电路图
sbit dula=P2^6;
sbit wela=P2^7;
void mdelay(uint z);//延时子程序
//正常读写操作之前检测LCD控制器状态
void WaitIdle();
void LcdWcn(uchar c);//送控制字子程序(不检测忙信号)
void LcdWc(uchar c);//送控制字子程序(检测忙信号)
void LcdWd(uchar c);//写数据
void ClrLcd();//清屏幕
void RstLcd();//复位LCD控制器
void SetCur(uchar Para);//设置光标
//设置第(xPos,yPos)个字符的位置
void LcdPos(uchar xPos,uchar yPos);
//在指定的行列位置(xPos,yPos)写字符c
void WriteChar(uchar c,uchar xPos,uchar yPos);
//在指定的行列位置(xPos,yPos)写字符串s,如果字符串的长度超过了一行的
//长度,其后的字符被截断,
//并不在下一行显示出来
void WriteString(uchar *s,uchar xPos,uchar yPos);
void main()
{
uchar xPos0,yPos0,xPos1,yPos1;
uchar * s0, *s1;
RstLcd();
ClrLcd();
SetCur(CurFlash);
xPos0=0;yPos0=0;
s0="I Like MCU!";
WriteString(s0,xPos0,yPos0);
xPos1=0;yPos1=1;
s1="WWW.AUFE.EDU.CN";
WriteString(s1,xPos1,yPos1);
while(1);
}
void mdelay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void WaitIdle()//检测忙,1时禁止,0时允许
{
RS=0;//读写数据
RW=1;
E=1;
mdelay(5);
while((P0&0x80)==0x80)//最高位是控制字:1是禁止,0是允许。80H 1000 0000
{
E=0; //这两句protues仿真必须加;仿真器仿真也通过。
E=1;
};
E=0;
}
void LcdWcn(uchar c)//送控制字子程序(不检测忙信号)
{
RS=0;
RW=0;
P0=c;
mdelay(5);
E=1;
mdelay(5);
E=0;
}
void LcdWc(uchar c)//送控制字子程序(检测忙信号)
{
WaitIdle();
LcdWcn(c);
}
void LcdWd(uchar c)//写数据
{
WaitIdle();
RS=1;
RW=0;
P0=c;
mdelay(5);
E=1;
mdelay(5);
mdelay(5);
E=0;
}
void ClrLcd()
{
LcdWc(0x01);
}
void RstLcd()
{
dula=0;//关闭数码管锁存端,避免电流不够
wela=0;
E=0;
LcdWc(0x38);
LcdWc(0x0f);
LcdWc(0x06);
LcdWc(0x01);
}
void SetCur(uchar Para)
{
mdelay(2);
switch(Para)
{
case 0:LcdWc(0x08);break;
case 1:LcdWc(0x0c);break;
case 2:LcdWc(0x0e);break;
case 3:LcdWc(0x0f);break;
default:break;
}
}
void LcdPos(uchar xPos,uchar yPos)
{
uchar tmp;
xPos&=0x0f;
yPos&=0x01;
if(yPos==0)
tmp=xPos;
else
tmp=xPos+0x40;
tmp|=0x80;
LcdWc(tmp);
}
void WriteChar(uchar c,uchar xPos,uchar yPos)
{
LcdPos(xPos,yPos);
LcdWd(c);
}
void WriteString(uchar *s,uchar xPos,uchar yPos)
{
uchar i;
if(*s==0)
return;
for(i=0;;i++)
{
if(*(s+i)==0)
break;
WriteChar(*(s+i),xPos,yPos);
xPos++;
if(xPos>=15)
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -