2位数码管显示4x4键盘键值,为什么
仿真时P0、P2口一直是高电平?
程序如下:
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar dis[32]={0xc0,0xc0,0xc0,0xf9,0xc0,0xa4,0xc0,0xb0,0xc0,0x99,0xc0,0x92,0xc0,0x82,0xc0,0xf8,
0xc0,0x80,0xc0,0x90,0xf9,0xc0,0xf9,0xf9,0xf9,0xa4,0xf9,0xb0,0xf9,0x99,0xf9,0x92};//0,0,0,1,0,2,……1,0,1,1……1,5
void delay(uint t)//延时
{ uchar i;
while(t--)
for(i=0;i<200;i++){};
}
void delay10ms(void)//消抖
{ uchar i;
for(i=0;i<200;i++){};
}
uchar key_scan(void)//键盘扫描
{
uchar code_h;
uchar code_l;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
delay10ms();
if((P1&0xf0)!=0xf0)
{ code_h=0xfe;
while((code_h&0x10)!=0x00);
{
P1=code_h;
if((P1&0xf0)!=0xf0)
{
code_l=(P1&0xf0|0x0f);
return((~code_h)+(~code_l));
}
};
code_h=_crol_(code_h,1);
}
}
return(0);
}
void xianshi(uchar i)//数码管显示
{ uchar j;
while(1)
{ j=0x01;
P0=dis[i];
P2=j;
delay(150);
j=_crol_(j,1);
P0=dis[i+1];
P2=j;
delay(150);}
}
void main(void)//主程序
{ uchar k=0;
while(1)
{
k=key_scan();
switch(k)
{
case 0x11:xianshi(0);
break;
case 0x21:xianshi(2);
break;
case 0x41:xianshi(4);
break;
case 0x181:xianshi(6);
break;
case 0x12:xianshi(8);
break;
case 0x22:xianshi(10);
break;
case 0x42:xianshi(12);
break;
case 0x82:xianshi(14);
break;
case 0x14:xianshi(16);
break;
case 0x24:xianshi(18);
break;
case 0x44:xianshi(20);
break;
case 0x84:xianshi(22);
break;
case 0x18:xianshi(24);
break;
case 0x28:xianshi(26);
break;
case 0x48:xianshi(28);
break;
case 0x88:xianshi(30);
break;
}
}
}
-
仿真原理图
一周热门 更多>