用51单片机和C语言编程.利用矩阵键盘依次显示1,2,3,4,5.……12的平方,显示在数码管上。

#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uint temp,num,num1;
uchar code table[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xe6,0xee,0x3e,0x9c,0x7a,0x9e,0x8e}; //0~f
void delay(uint z) //延迟
{
uint x,y;
for(x=0;x<110;x++)
for(y=0;y<z;y++);
}
void display(uchar bai,uchar shi,uchar ge)
{
P2=0x7f;
P0=table[bai];
delay(1);
P2=0xbf;
P0=table[shi];
delay(1);
P2=0xdf;
P0=table[ge];
delay(1);
}
uint keyscan()
{
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:num=1;
break;
case 0xdd:num=4;
break;
case 0xbd:num=9;
break;
case 0x7d:num=16;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
P1=0xfb;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:num=25;
break;
case 0xdb:num=36;
break;
case 0xbb:num=49;
break;
case 0x7b:num=64;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
P1=0xf7;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:num=81;
break;
case 0xd7:num=100;
break;
case 0xb7:num=121;
break;
case 0x77:num=144;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
return num;
}
void main()
{
P0=0x00;
P1=0xff;
while(1)
{
num1=keyscan();
display(num1/100,num1%100/10,num1%10);
}
}

程序启动之后,矩阵键盘只有前三个有用可以显示出相应的1,4,9.后面的按键通通没用,不知道问题出在哪
你的while循环写的有问题,既然要先判断行选再判吵歼逗断列选,应该用两级的switch语句,而不是两级wihile语句,你现在的写升卖法只循环了第二级的第一个while循环,所以只有处于第二级while的switch的三按(其实改瞎看代码应该是四个)键个管用。。。

4 x 4 矩阵键盘 仿真实昌告例耐陵明 汪丛供参考


//这是我以前写的 你对照一下吧 字体枯轮太小累眼睛 我的这个 绝对可用 没有错误 只是I/0口不
//一样 主要是你矩阵键盘那块有问题 你对照尘凳我的 很快就找出来了
#include<reg52.h>
#define uchar unsigned char#define uint unsigned int
sbit dula=P2^6;sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};

uint jzjp();
void shumaguan(uint shu);
void delay(uint z);
void init();

void main()
{
init();
while(1)
{

shumaguan(jzjp());
}
}

void init()
{

}

void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void shumaguan(uint shu)
{
uchar bai,shi,ge;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;

dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);

dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);

dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(5);

}
uint jzjp()
{
uint shu,num;
uchar temp;
P3=0xfe;
temp=P3;
temp=P3&0xf0;没兄信
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:num=1*1;
break;
case 0xde:num=2*2;
break;
case 0xbe:num=3*3;
break;
case 0x7e:num=4*4;
break;
}
while(temp!=0xf0)
{

temp=P3;
temp=temp&0xf0;
shumaguan(shu);
}
}

}
P3=0xfd;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:num=5*5;
break;
case 0xdd:num=6*6;
break;
case 0xbd:num=7*7;
break;
case 0x7d:num=8*8;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
shumaguan(shu);
}
}

}
P3=0xfb;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:num=9*9;
break;
case 0xdb:num=10*10;
break;
case 0xbb:num=11*11;
break;
case 0x7b:num=12*12;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
shumaguan(shu);
}
}

}
P3=0xf7;
temp=P3;
temp=P3&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:num=13*13;
break;
case 0xd7:num=14*14;
break;
case 0xb7:num=15*15;
break;
case 0x77:num=16*16;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
shumaguan(shu);
}
}

}
return num;
}