不知道怎么在矩阵键盘按键释放后,数码管能够动态显示???、求单片机编程大神帮忙
//按下16个矩阵键盘依次在数码管上显示1-16的平方。如按下第一个显示1,第二个显示4...#include<reg52.h>//52单片机系列#defineuchar...
//按下16个矩阵键盘依次在数码管上显示1-16的平方。如按下第一个显示1,第二个显示4...
#include<reg52.h> //52单片机系列
#define uchar unsigned char
#define uint unsigned int
sbit duan=P2^6; //申明U1锁存器的锁存端
sbit wei=P2^7; //申明U2锁存器的锁存端
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uint x) //延时x毫秒
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar num) // 数码管显示数字
{
uint number;
uchar bai,shi,ge;
number=num*num;
bai=number/100;
shi=number%100/10;
ge=number%10;
duan=1;
P0=table[bai];
duan=0;
P0=0xff;
wei=1;
P0=0xfe;
wei=0;
delay(5);
duan=1;
P0=table[shi];
duan=0;
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
delay(5);
duan=1;
P0=table[ge];
duan=0;
P0=0xff;
wei=1;
P0=0xfb;
wei=0;
delay(5);
}
void yscan() //矩阵键盘检测
{
uchar temp;
uchar key;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=1;
break;
case 0xde:
key=2;
break;
case 0xbe:
key=3;
break;
case 0x7e:
key=4;
break;
}
while(temp!=0xf0) //等待按键释放
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=5;
break;
case 0xdd:
key=6;
break;
case 0xbd:
key=7;
break;
case 0x7d:
key=8;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=9;
break;
case 0xdb:
key=10;
break;
case 0xbb:
key=11;
break;
case 0x7b:
key=12;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=13;
break;
case 0xd7:
key=14;
break;
case 0xb7:
key=15;
break;
case 0x77:
key=16;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
}
void main()
{
while(1)
{
yscan();
}
} 展开
#include<reg52.h> //52单片机系列
#define uchar unsigned char
#define uint unsigned int
sbit duan=P2^6; //申明U1锁存器的锁存端
sbit wei=P2^7; //申明U2锁存器的锁存端
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uint x) //延时x毫秒
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar num) // 数码管显示数字
{
uint number;
uchar bai,shi,ge;
number=num*num;
bai=number/100;
shi=number%100/10;
ge=number%10;
duan=1;
P0=table[bai];
duan=0;
P0=0xff;
wei=1;
P0=0xfe;
wei=0;
delay(5);
duan=1;
P0=table[shi];
duan=0;
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
delay(5);
duan=1;
P0=table[ge];
duan=0;
P0=0xff;
wei=1;
P0=0xfb;
wei=0;
delay(5);
}
void yscan() //矩阵键盘检测
{
uchar temp;
uchar key;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=1;
break;
case 0xde:
key=2;
break;
case 0xbe:
key=3;
break;
case 0x7e:
key=4;
break;
}
while(temp!=0xf0) //等待按键释放
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=5;
break;
case 0xdd:
key=6;
break;
case 0xbd:
key=7;
break;
case 0x7d:
key=8;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=9;
break;
case 0xdb:
key=10;
break;
case 0xbb:
key=11;
break;
case 0x7b:
key=12;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
key=13;
break;
case 0xd7:
key=14;
break;
case 0xb7:
key=15;
break;
case 0x77:
key=16;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
}
void main()
{
while(1)
{
yscan();
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询