为什么这样子的55矩阵键盘无法运行啊
时间:10-02
整理:3721RD
点击:
5乘5的矩阵键盘,行线接P0^0到P0^4,列线接P2^0到P2^4。但是按键程序不能正确运行,问题如图,没能正确扫描。
#include<reg52.h>
void delay(int i)
{
int x,y;
for(x=i;x>0;x--)
for(y=112;y>0;y--);
}
unsigned char KeyA()
{
unsigned int a,H,L;
P0 = 0x1f; //行 0001 1111
P2 = 0x00; //列 0000 0000
if((P0 & 0x1f) != 0x1f)
{
delay(10); //按键消抖
if((P0 & 0x1f) != 0x1f)
{
H = P0;
P0 = 0x00;
P2 = 0x1f;
L = P2;
while((P2 & 0x1f) != 0x1f);
a = (10 * H) + L;
return(a);
}
}
return -1;
}
char KeyB()
{
char r;
switch( KeyA() )
{
//1
case 330: r = 0x11; break;
case 329: r = 0x12; break;
case 327: r = 0x13; break;
case 323: r = 0x14; break;
case 315: r = 0x15; break;
//2
case 320: r = 0x21; break;
case 319: r = 0x22; break;
case 317: r = 0x23; break;
case 313: r = 0x24; break;
case 305: r = 0x25; break;
//3
case 300: r = 0x11; break;
case 299: r = 0x12; break;
case 297: r = 0x13; break;
case 293: r = 0x14; break;
case 285: r = 0x15; break;
//4
case 260: r = 0x11; break;
case 259: r = 0x12; break;
case 257: r = 0x13; break;
case 253: r = 0x14; break;
case 245: r = 0x15; break;
//5
case 180: r = 0x11; break;
case 179: r = 0x12; break;
case 177: r = 0x13; break;
case 173: r = 0x14; break;
case 165: r = 0x15; break;
}
return (r);
}
void init()
{
TMOD = 0x02;
TH0 = 0xf4;
TL0 = 0xf4;
TR0 = 1;
SM0 = 0;
SM1 = 1;
}
void main()
{
init();
while(2)
{
SBUF = KeyB();
while(!TI);
TI = 0;
}
}
#include<reg52.h>
void delay(int i)
{
int x,y;
for(x=i;x>0;x--)
for(y=112;y>0;y--);
}
unsigned char KeyA()
{
unsigned int a,H,L;
P0 = 0x1f; //行 0001 1111
P2 = 0x00; //列 0000 0000
if((P0 & 0x1f) != 0x1f)
{
delay(10); //按键消抖
if((P0 & 0x1f) != 0x1f)
{
H = P0;
P0 = 0x00;
P2 = 0x1f;
L = P2;
while((P2 & 0x1f) != 0x1f);
a = (10 * H) + L;
return(a);
}
}
return -1;
}
char KeyB()
{
char r;
switch( KeyA() )
{
//1
case 330: r = 0x11; break;
case 329: r = 0x12; break;
case 327: r = 0x13; break;
case 323: r = 0x14; break;
case 315: r = 0x15; break;
//2
case 320: r = 0x21; break;
case 319: r = 0x22; break;
case 317: r = 0x23; break;
case 313: r = 0x24; break;
case 305: r = 0x25; break;
//3
case 300: r = 0x11; break;
case 299: r = 0x12; break;
case 297: r = 0x13; break;
case 293: r = 0x14; break;
case 285: r = 0x15; break;
//4
case 260: r = 0x11; break;
case 259: r = 0x12; break;
case 257: r = 0x13; break;
case 253: r = 0x14; break;
case 245: r = 0x15; break;
//5
case 180: r = 0x11; break;
case 179: r = 0x12; break;
case 177: r = 0x13; break;
case 173: r = 0x14; break;
case 165: r = 0x15; break;
}
return (r);
}
void init()
{
TMOD = 0x02;
TH0 = 0xf4;
TL0 = 0xf4;
TR0 = 1;
SM0 = 0;
SM1 = 1;
}
void main()
{
init();
while(2)
{
SBUF = KeyB();
while(!TI);
TI = 0;
}
}
case真多
这个case是什么意思呀
是while(2)吧?一般都是while(1)啊
我说的是程序里的case
问题解决了买有?我看子程序KeyB()中的变量r在定义后没有被初始化为一个特定值,只是在switch语句的不同case中被赋值,如果没有任何按键的话,KeyA()会返回-1,这时KeyB()中的r会是什么值呢?
乍一看,也没发现程序哪里有问题,你可以再加几个led灯,让它们根据KeyB()返回的值来显示,根据led的点亮情况来看是扫描按键这一块儿出的问题,还是串口发送出了问题。
不成熟的建议供你参考。
哦 知道了 看来要学的太多了
大牛啊,写个垃圾代码