单片机C语言LCD1602程序怎么只能显示一个字符?求高手帮我看看,谢谢!
#include<reg52.h>#defineucharunsignedchar#defineuintunsignedintsbitRS=P2^4;sbitRW=P2^...
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^4;
sbit RW=P2^3;
sbit E=P2^2;
void delay(uchar z) //延时
{
uchar x,t;
for(x=z;x>0;x--)
for(t=0;t>0;t--);
}
void read_busy(void) //读状态
{
RS=0;
RW=1;
P0=0xff;
E=0;
E=1;
while(P0^7==1);
E=0;
}
/*void write_data(uchar Databuf) //写数据
{
RS=1;
RW=0;
P0=Databuf;
E=1;
E=0;
} */
void write_busy_data(uchar Databuf) //读状态写数据
{
read_busy();
RS=1;
RW=0;
P0=Databuf;
E=0;
E=1;
E=0;
}
void write_com(uchar Combuf) //写指令
{
RS=0;
RW=0;
P0=Combuf;
E=0;
E=1;
E=0;
}
void write_busy_com(uchar Combuf) //读状态写指令
{
read_busy();
RS=0;
RW=0;
P0=Combuf;
E=0;
E=1;
E=0;
}
void init(void) //初始化
{
delay(15);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
write_busy_com(0x38); //显示模式设置
write_busy_com(0x08); //显示关闭
write_busy_com(0x01); //显示清屏
write_busy_com(0x06); //显示光标移动设置
write_busy_com(0x0c); //显示开及光标设置
}
void write_address(uchar x,uchar y) //写地址位
{
if(y==0)
write_busy_com(0x80+x);
else
write_busy_com(0xc0+x);
}
void Display(uchar x,uchar y,uchar Databuf) //显示数据
{
read_busy();
write_address(x,y);
write_busy_data(Databuf);
}
void DisplayStr(uchar x,uchar y,uchar *Strbuf) //显示字符串
{
read_busy();
write_address(x,y);
while(*Strbuf!= '\0')
{
write_busy_data(*Strbuf);
Strbuf++;
}
}
void main() //主函数
{
init();
Display(15,0,'!');
Display(5,2,'u');
DisplayStr( 4,0,"welcome");
while(1);
}
运行只显示 ! 字符 展开
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^4;
sbit RW=P2^3;
sbit E=P2^2;
void delay(uchar z) //延时
{
uchar x,t;
for(x=z;x>0;x--)
for(t=0;t>0;t--);
}
void read_busy(void) //读状态
{
RS=0;
RW=1;
P0=0xff;
E=0;
E=1;
while(P0^7==1);
E=0;
}
/*void write_data(uchar Databuf) //写数据
{
RS=1;
RW=0;
P0=Databuf;
E=1;
E=0;
} */
void write_busy_data(uchar Databuf) //读状态写数据
{
read_busy();
RS=1;
RW=0;
P0=Databuf;
E=0;
E=1;
E=0;
}
void write_com(uchar Combuf) //写指令
{
RS=0;
RW=0;
P0=Combuf;
E=0;
E=1;
E=0;
}
void write_busy_com(uchar Combuf) //读状态写指令
{
read_busy();
RS=0;
RW=0;
P0=Combuf;
E=0;
E=1;
E=0;
}
void init(void) //初始化
{
delay(15);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
write_busy_com(0x38); //显示模式设置
write_busy_com(0x08); //显示关闭
write_busy_com(0x01); //显示清屏
write_busy_com(0x06); //显示光标移动设置
write_busy_com(0x0c); //显示开及光标设置
}
void write_address(uchar x,uchar y) //写地址位
{
if(y==0)
write_busy_com(0x80+x);
else
write_busy_com(0xc0+x);
}
void Display(uchar x,uchar y,uchar Databuf) //显示数据
{
read_busy();
write_address(x,y);
write_busy_data(Databuf);
}
void DisplayStr(uchar x,uchar y,uchar *Strbuf) //显示字符串
{
read_busy();
write_address(x,y);
while(*Strbuf!= '\0')
{
write_busy_data(*Strbuf);
Strbuf++;
}
}
void main() //主函数
{
init();
Display(15,0,'!');
Display(5,2,'u');
DisplayStr( 4,0,"welcome");
while(1);
}
运行只显示 ! 字符 展开
2个回答
展开全部
将read_busy()函数中的while(P0^7==1)改为while(P0&0x20!=0)试试。在程序中P0^7并不代表是P0寄存器的第7位,而是P0寄存器的值与7的按位异或运算。形如P0^7只有在加上"sbit"关键词后才代表P0的最高位,这是C51的扩展语法;在程序代码中符号"^"就只是按位异或运算符,这点不要搞混了。
追问
现在改好了
uchar read_busy() //读状态
{
uchar state;
RS=0;
RW=1;
P0=0xff;
E=1;
delay(1);
state=P0;
E=0;
return state;
}
然后把写数据写命令之前加 while((read_busy()&0x80)==0x80);就行了
还是谢谢你 其实关键性错误就是这一句
追答
是应该while(P0^0x80)的,当时手误写错了。反正就是这一句了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询