大家帮忙看下这个程序,P1口接数码管,4位动态显示,P0接矩阵键盘,4x3键盘,当我按下键盘时候,没有松开,数码管显示只有最后一位显示(保持的),松开才显示新的数,这是怎么回事呐??
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delayms(uchar);
void display(uchar dat);
uchar keyscan();
sbit LS138A=P2^2;
sbit LS138B=P2^3;
sbit LS138C=P2^4;
uchar code dis[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0XD8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x86,0xc7,0xff};
uchar temp,num,wel[8];
void main()
{
while(1)
{
display(keyscan());
}
}
void delayms(uchar ms)
{
uchar i;
while(ms--)
for(i=0;i<125;i++);
}
uchar keyscan()
{
P0=0xfd;
temp=P0;
temp=temp&0xf1;
while(temp!=0xf1)
{
delayms(5);
temp=P0;
temp=temp&0xf1;
while(temp!=0xf1)
{
temp=P0;
switch(temp)
{
case 0xed:num=1; break;
case 0xdd:num=2; break;
case 0xbd:num=3; break;
case 0x7d:num=4; break;
}
while(temp!=0xf1)
{
temp=P0;
temp=temp&0xf1;
}
}
}
P0=0xfb;
temp=P0;
temp=temp&0xf1;
while(temp!=0xf1)
{
delayms(5);
temp=P0;
temp=temp&0xf1;
while(temp!=0xf1)
{
temp=P0;
switch(temp)
{
case 0xeb:num=5; break;
case 0xdb:num=6; break;
case 0xbb:num=7; break;
case 0x7b:num=8; break;
}
while(temp!=0xf1)
{
temp=P0;
temp=temp&0xf1;
}
}
}
P0=0xf7;
temp=P0;
temp=temp&0xf1;
while(temp!=0xf1)
{
delayms(5);
temp=P0;
temp=temp&0xf1;
while(temp!=0xf1)
{
temp=P0;
switch(temp)
{
case 0xe7:num=9; break;
case 0xd7:num=10; break;
case 0xb7:num=11; break;
case 0x77:num=12; break;
}
while(temp!=0xf1)
{
temp=P0;
temp=temp&0xf1;
}
}
}
return num;
}
void display(uchar dat)
{
uchar j;
wel[0]=dis[dat];
wel[1]=dis[dat];
wel[2]=dis[dat];
wel[3]=dis[dat];
wel[4]=dis[dat];
wel[5]=dis[dat];
wel[6]=dis[dat];
wel[7]=dis[dat];
for(j=0;j<8;j++)
{
P1=wel[j];
switch(j)
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;
}
delayms(1);
}
} |