#include<intrins.h>
#include <reg52.h>
#define Uchar unsigned char
#define Uint unsigned int
#define Busy 0x80 // 忙判别位
void LcdWriteData1( char dataW);
void LcdWriteCommand1( Uchar CMD1,Uchar AttribC1);
void LcdReset1( void );
void DispOneChar1(Uchar x,Uchar y,Uchar Wdata);
void ePutstr1(Uchar x,Uchar y, Uchar code *ptr);
void Delay(unsigned int t);
void Checkbusy1();
sbit LcdE1=P3^4;
sbit LcdRS="P3"^5;
sbit LcdWR="P3"^6;
sbit LcdE2=P2^5;
// 检测忙
/*void Checkbusy1()
{
P3=P3|0x40; // RS="0",WR=1
while(1)
{ Uchar tag1;
//P0=0xff;
P3=P3|0x50; //RS=0,WR=1 E1=1读状态
tag1=P0;
P3=P3&0xef; //锁定数据 E1=0
if((tag1&Busy)==0x00) // 检测忙信号?
break;
}
} */
void Checkbusy1()
{
while(1)
{ Uchar tag1;
//P0=0xff;
LcdRS="0"; //RS=0,WR=1 E1=1读状态
LcdWR="1";
LcdE1=1;
tag1=P0;
LcdE1=0; //锁定数据
if((tag1&Busy)==0x00) // 检测忙信号?
break;
}
}
void Checkbusy2()
{
while(1)
{ Uchar tag2;
//P0=0xff;
LcdRS="0"; //RS=0,WR=1 E1=1读状态
LcdWR="1";
LcdE2=1;
tag2=P0;
LcdE2=0; //锁定数据
if((tag2&Busy)==0x00) // 检测忙信号?
break;
}
}
// 写控制字符子程序: E="1" RS="0" RW="0"
/*void LcdWriteCommand1( Uchar CMD1,Uchar AttribC1 )
{
if (AttribC1)
{
Checkbusy1();
P3=P3&0xbf; //RS=0,WR=0 E1=0写命令
P0= CMD1;
P3=P3|0x10; //锁定数据
P3=P3&0xef;
}
else
{
P3=P3&0x8f; //RS=0,WR=0 ,E1=0写命令
P0= CMD1;
P3=P3|0x10; //锁定数据 E1=1后 E1=0
P3=P3&0xef;
}
} */
// 写控制字符子程序: E="1" RS="0" RW="0"
void LcdWriteCommand1( Uchar CMD1,Uchar AttribC1 )
{
if (AttribC1)
{
Checkbusy1();
//P0=0xff;
LcdRS="0";
LcdWR="0"; //写命令
P0= CMD1;
LcdE1=1;
LcdE1=0; //锁定数据
}
else
{
LcdRS="0";
LcdWR="0";
P0= CMD1;
LcdE1=1;
LcdE1=0; //锁定数据
}
}
// 写控制字符子程序: E="1" RS="0" RW="0"
void LcdWriteCommand2( Uchar CMD2,Uchar AttribC2 )
{
if (AttribC2)
{
Checkbusy2();
//P0=0xff;
LcdRS="0";
LcdWR="0"; //写命令
P0= CMD2;
LcdE2=1;
LcdE2=0; //锁定数据
}
else
{
LcdRS="0";
LcdWR="0";
P0= CMD2;
LcdE2=1;
LcdE2=0; //锁定数据
}
}
void Delay(unsigned int t)
{
int k,l,m;
for(k=0;k<t;k++)
{
for(l=0;l<200;l++)
{
m="0";
}
}
}
// 初始化程序, 必须按照产品资料介绍的初始化过程进行
void LcdReset1( void )
{
LcdWriteCommand1(0x38,0); // 显示模式设置(不检测忙信号)
Delay(50);
LcdWriteCommand1(0x38,0); // 共三次
Delay(50);
LcdWriteCommand1(0x38,0);
Delay(50);
LcdWriteCommand1(0x38,1); //6功能设置, 显示模式设置(以后均检测忙信号)
// 8位数据,两行显示, 5*10点阵
// Delay(100);
LcdWriteCommand1(0x08,1); //4 显示关闭
// Delay(100);
LcdWriteCommand1(0x01,1); //1清显示, 显示清屏
//Delay(100);
LcdWriteCommand1(0x02,1); //3输入方式, 光标由左向右移,AC加一,禁止流动
//Delay(100);
LcdWriteCommand1(0x06,1); //5移位, 光标右移,AC加一,显示不动
// Delay(100);
LcdWriteCommand1(0x0f,1); //4显示开关,整体显示打开,光标显示开,光标闪烁
}
// 初始化程序, 必须按照产品资料介绍的初始化过程进行
void LcdReset2( void )
{
LcdWriteCommand2(0x38,0); // 显示模式设置(不检测忙信号)
Delay(50);
LcdWriteCommand2(0x38,0); // 共三次
Delay(50);
LcdWriteCommand2(0x38,0);
Delay(50);
LcdWriteCommand2(0x38,1); //6功能设置, 显示模式设置(以后均检测忙信号)
// 8位数据,两行显示, 5*10点阵
LcdWriteCommand2(0x08,1); //4 显示关闭
LcdWriteCommand2(0x01,1); //1清显示, 显示清屏
LcdWriteCommand2(0x02,1); //3输入方式, 光标由左向右移,AC加一,禁止流动
LcdWriteCommand2(0x06,1); //5移位, 光标右移,AC加一,显示不动
LcdWriteCommand2(0x0f,1); //4显示开关,整体显示打开,光标显示开,光标闪烁
}
// 当前位置写字符子程序: E =1 RS="1" RW="0"
/*void LcdWriteData1( char dataW1 )
{
Checkbusy1();
P3=P3|0x20; //RS=1, RW="0", E1 =0
P3=P3&0xbf;
P0= dataW1;
P3=P3|0x10; //锁定数据
P3=P3&0xef;
}*/
void LcdWriteData1( char dataW1 )
{
Checkbusy1();
LcdRS="1"; //RS=1, RW="0", E2 =0
LcdWR="0";
P0= dataW1;
LcdE1=1; //锁定数据
LcdE1=0;
}
void LcdWriteData2( char dataW2 )
{
Checkbusy2();
LcdRS="1"; //RS=1, RW="0", E2 =0
LcdWR="0";
P0= dataW2;
LcdE2=1; //锁定数据
LcdE2=0;
}
// 显示光标定位
void LocateXY1( char posx,char posy)
{
Uchar temp;
temp = posx & 0xf;
posy &= 0x1;
if ( posy )temp |= 0x40;
temp |= 0x80;
LcdWriteCommand1(temp,0);
}
// 按指定位置显示数出一个字符
void DispOneChar1(Uchar x,Uchar y,Uchar Wdata)
{
LocateXY1( x, y ); // 定位显示地址
LcdWriteData1( Wdata ); // 写字符
}
void ePutstr1(Uchar x,Uchar y, Uchar code *ptr)
{
Uchar i,l=0;
while (ptr[l] >31){l++;};
for (i=0;i<l;i++) {
DispOneChar1(x++,y,ptr);
if ( x == 16 )
{
x = 0; y ^= 1;
}
}
}
main()
{ Uchar g;
Uchar j="0x30";
LcdRS="0";
LcdWR="1";
LcdE1=0;
LcdE2=0;
P0=0xff;
/* LcdReset1();
Delay(2000);
LcdWriteCommand1(0x80,1);
for(g=0;g<80;g++)
{
LcdWriteData1(j);
j++;
Delay(50);
}*/ //这也是必需的....初始化
LcdReset2();
Delay(2000);
LcdWriteCommand2(0x80,1);
j="0x30";
for(g=0;g<80;g++)
{
LcdWriteData2(j);
j++;
Delay(50);
}
/*LcdWriteCommand2(0x80,1);
LcdWriteData2(0x39);
// LcdWriteCommand1(0x81,1);
LcdWriteData2(0x38);
// LcdWriteCommand1(0x82,1);
LcdWriteData2(0x37);
//LcdWriteCommand1(0x83,1);
LcdWriteData2(0x36);
// LcdWriteCommand1(0x84,1);
LcdWriteData2(0x35);
//LcdWriteCommand1(0x85,1);
LcdWriteData2(0x34);
// LcdWriteCommand1(0x86,1);
LcdWriteData2(0x33);
// LcdWriteCommand1(0x87,1);
LcdWriteData2(0x32);
// LcdWriteCommand1(0x88,1);
LcdWriteData2(0x31);
//LcdWriteCommand1(0x89,1);
LcdWriteData2(0x30); */
while(1);
}
如上程序中定义,数据口为P0,可是读忙标志的时候就循环在那不动了,如下:
void Checkbusy1()
{
while(1)
{ Uchar tag1;
//P0=0xff;
LcdRS="0"; //RS=0,WR=1 E1=1读状态
LcdWR="1";
LcdE1=1;
tag1=P0;
LcdE1=0; //锁定数据
if((tag1&Busy)==0x00) // 检测忙信号?
break;
}
}
void Checkbusy2()
{
while(1)
{ Uchar tag2;
//P0=0xff;
LcdRS="0"; //RS=0,WR=1 E1=1读状态
LcdWR="1";
LcdE2=1;
tag2=P0;
LcdE2=0; //锁定数据
if((tag2&Busy)==0x00) // 检测忙信号?
break;
}
}
用的是WAVE仿真器,CPU为89S52,通讯板上已接上拉电阻,用仿真器时,P0写1后就成死循环,问题到底出在哪里,仿真器我已经试过了,(P0口写1后,再给P0.7接低电平,是可以读出的,为0x7f),请高手指教。。。
文章评论(0条评论)
登录后参与讨论