想要在LCD1602上显示矩阵键盘的键值,但程序改了很久,在LCD上只显示“Plsease_input”,然后就没有其他的了,按动键盘1602也没反应,求
哪位好心人帮忙看看,小弟刚刚入门
**************************************************************************************************************
#include<stc12c5a.h>
#include <intrins.h>
sbit rs = P2^5;
sbit rw = P2^6;
sbit en = P2^7;
unsigned char key;
unsigned char code key_code[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};
//unsigned char dis[]="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,Z";
void delay1ms(unsigned char d) //误差 -0.018084490741us
{
unsigned char a,b,c,z;
for(c=8;c>0;c--)
for(b=197;b>0;b--)
for(a=2;a>0;a--)
for(z=d;z>0;a--);
}
void delay()
{
//for(x=z;x>0;x--)
//for(y=1000;y>0;y--);
}
void lcd_busy() //判断是否忙状态
{
while(1)
{
rs=0;
rw=1;
delay();
en=0;
P0=0xff;
delay();
en=1;
if((P0&0x80)==0)
break;
}
}
void write_com(unsigned char com)
{
lcd_busy();
rs=0;
rw=0;
P0=com;
delay();
en=1;
delay();
en=0;
}
void write_data(unsigned char date)
{
lcd_busy();
rs=1;
rw=0;
P0=date;
delay();
en=1;
delay();
en=0;
}
void init()
{
en=0;
write_com(0x01);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
}
void display() //显示给定电压、实际电压子程序
{
en=0;
write_com(0x80+0x00);
write_data('P');
write_com(0x80+0x01);
write_data('l');
write_com(0x80+0x02);
write_data('s');
write_com(0x80+0x03);
write_data('e');
write_com(0x80+0x04);
write_data('a');
write_com(0x80+0x05);
write_data('s');
write_com(0x80+0x06);
write_data('e');
write_com(0x80+0x07);
write_data('_');
write_com(0x80+0x08);
write_data('i');
write_com(0x80+0x09);
write_data('n');
write_com(0x80+0x0A);
write_data('p');
write_com(0x80+0x0B);
write_data('u');
write_com(0x80+0x0C);
write_data('t');
write_com(0x80+0x0D);
write_data(':');
// write_com(0x80+0x0E);
// write_data('Z');
}
void display_key(unsigned char cm)
{
write_com(0x80+0x0e);
write_data(cm);
}
unsigned char keyscan()
{
unsigned char scan1,scan2,keycode;//,j;
P1=0xf0;
scan1=P1;
if((scan1&0xf0)!=0xf0) //判键是否按下
{
delay1ms(20); //延时20ms
scan1=P1;
if((scan1&0xf0)!=0xf0) //二次判键是否按下
{
P1=0x0f; //线反转法的核心
scan2=P1;
keycode=scan1|scan2; //组合成键编码
/* for(j=0;j<=15;j++)
{
if(keycode==key_code[j]) //查表得键值
{
key=j;
write_com(0x80+0x0e);
write_data(dis[key]);
}
} */
switch(keycode)
{ case 0xee:key=1;break;
case 0xde:key=2;break;
case 0xbe:key=3;break;
case 0x7e:key=4;break;
case 0xed:key=5;break;
case 0xdd:key=6;break;
case 0xbd:key=7;break;
case 0x7d:key=8;break;
case 0xeb:key=9;break;
case 0xdb:key=10;break;
case 0xbb:key=11;break;
case 0x7b:key=12;break;
case 0xe7:key=13;break;
case 0xd7:key=14;break;
case 0xb7:key=15;break;
case 0x77:key=16;break; }
return(key);
}
}
else
{ P1=0xff; //P1口写1 输入状态
write_com(0x80+0x0e);
write_data('Z');
}
}
void main()
{
init();
P1=0xff;
display();
while(1)
{
P1=0xf0;
if((P1&0xf0)!=0xf0)
{keyscan();
display_key(key);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
write_com(0x80+0x0e);
write_data(cm);
}
这个子程序就不可以这个写,write_data(cm);不能这样写,应改为 write_data(dis[cm]);其他的没有怎么看
一周热门 更多>