51单片机怎么用独立按键控制lcd1602,摁key1显示“up”,摁key2显示“down”为什么我的不成功呢?
#include<reg52.h>#defineucharunsignedchar#defineuintunsignedintsbitrs=P2^6;sbitrw=P2^...
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
sbit key1=P3^0;
sbit key2=P3^1;
uchar sz1[]="up";
uchar sz2[]="down";
uchar num;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //д???
{
e=0;
rs=0;
rw=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void write_data(uchar dat) //д????
{
e=0;
rs=1;
rw=0;
P0=dat;
delay(5);
e=1;
delay(5);
e=0;
}
void init(void)
{
e=0;
write_com(0x38); //????1602??????5*7????
delay(5);
write_com(0x01); //????
delay(5);
write_com(0x0c); //????? ???????? ??????
delay(5);
write_com(0x06); //?????????д????????????????????????
delay(5);
}
void main()
{
uchar k;
init ();
if(key1==0)
{
if(key1==0)
delay(5);
{
write_com(0x80);
for(k=0;k<2;k++)
{
write_data(sz1[k]);
delay(5);
}
}
}
while(!key1);
else (key2==0)
{
else (key2==0)
delay(5);
{
write_com(0x80 + 0x40);
for(k=0;k<4;k++)
{
write_data(sz2[k]);
delay(5);
}
}
}
while(!key2);
while(1);
}
我现在的问题就是摁着key1和key2的同时摁复位键才会显现。
不知道怎么办 展开
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
sbit key1=P3^0;
sbit key2=P3^1;
uchar sz1[]="up";
uchar sz2[]="down";
uchar num;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //д???
{
e=0;
rs=0;
rw=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void write_data(uchar dat) //д????
{
e=0;
rs=1;
rw=0;
P0=dat;
delay(5);
e=1;
delay(5);
e=0;
}
void init(void)
{
e=0;
write_com(0x38); //????1602??????5*7????
delay(5);
write_com(0x01); //????
delay(5);
write_com(0x0c); //????? ???????? ??????
delay(5);
write_com(0x06); //?????????д????????????????????????
delay(5);
}
void main()
{
uchar k;
init ();
if(key1==0)
{
if(key1==0)
delay(5);
{
write_com(0x80);
for(k=0;k<2;k++)
{
write_data(sz1[k]);
delay(5);
}
}
}
while(!key1);
else (key2==0)
{
else (key2==0)
delay(5);
{
write_com(0x80 + 0x40);
for(k=0;k<4;k++)
{
write_data(sz2[k]);
delay(5);
}
}
}
while(!key2);
while(1);
}
我现在的问题就是摁着key1和key2的同时摁复位键才会显现。
不知道怎么办 展开
2个回答
2020-08-03
展开全部
我把我写的发出来了,你可以参考下
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define uchar unsigned char
#define GPIO_LCD P0
sbit E=P2^7;
sbit RW=P2^5;
sbit RS=P2^6;
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
sbit K4=P1^3;
sbit K5=P1^4;
uchar code table[]="Pechin Science";
uchar code table1[]="Key_Value:";
uchar code table2[]="K1";
uchar code table3[]="K2";
void yanshi (uchar xms)//延时
{
uchar i,y;
for(i=xms;i>0;i--)
for(y=122;y>0;y++);
}
void LcdWriteCom(unsigned char com) //写入命令: 向LCD写入一个字节的命令
{
RS=0;
RW=0;
GPIO_LCD=com;
yanshi(10);
E=1;
yanshi(10);
E=0;
}
void LcdWriteData(unsigned char dat) //写入数据: 向LCD马入一个字节的数据
{
RS=1;
RW=0;
GPIO_LCD =dat;
yanshi(10);
E=1;
yanshi(10);
E=0;
}
void LcdInit() //初始化1602
{
LcdWriteCom(0x80);
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
yanshi(1);
}
void main(void)
{
uchar x,y,z,u;
LcdInit();
LcdWriteCom(0x80);
for(x=0;x<14;x++)
{
LcdWriteData(table[x]);
yanshi(50);
}
LcdWriteCom(0xC0);
for(y=0;y<10;y++)
{
LcdWriteData(table1[y]);
yanshi(50);
}
while(1)
{
LcdWriteCom(0xCb);
if( K1 == 0)
{
for(z=0;z<2;z++)
{
LcdWriteData(table2[z]);
yanshi(50);
}
}
if( K2 == 0)
{
for(u=0;u<2;u++)
{
LcdWriteData(table3[u]);
yanshi(50);
}
}
}
}
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define uchar unsigned char
#define GPIO_LCD P0
sbit E=P2^7;
sbit RW=P2^5;
sbit RS=P2^6;
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
sbit K4=P1^3;
sbit K5=P1^4;
uchar code table[]="Pechin Science";
uchar code table1[]="Key_Value:";
uchar code table2[]="K1";
uchar code table3[]="K2";
void yanshi (uchar xms)//延时
{
uchar i,y;
for(i=xms;i>0;i--)
for(y=122;y>0;y++);
}
void LcdWriteCom(unsigned char com) //写入命令: 向LCD写入一个字节的命令
{
RS=0;
RW=0;
GPIO_LCD=com;
yanshi(10);
E=1;
yanshi(10);
E=0;
}
void LcdWriteData(unsigned char dat) //写入数据: 向LCD马入一个字节的数据
{
RS=1;
RW=0;
GPIO_LCD =dat;
yanshi(10);
E=1;
yanshi(10);
E=0;
}
void LcdInit() //初始化1602
{
LcdWriteCom(0x80);
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
yanshi(1);
}
void main(void)
{
uchar x,y,z,u;
LcdInit();
LcdWriteCom(0x80);
for(x=0;x<14;x++)
{
LcdWriteData(table[x]);
yanshi(50);
}
LcdWriteCom(0xC0);
for(y=0;y<10;y++)
{
LcdWriteData(table1[y]);
yanshi(50);
}
while(1)
{
LcdWriteCom(0xCb);
if( K1 == 0)
{
for(z=0;z<2;z++)
{
LcdWriteData(table2[z]);
yanshi(50);
}
}
if( K2 == 0)
{
for(u=0;u<2;u++)
{
LcdWriteData(table3[u]);
yanshi(50);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |