8个IO口检测64个按键,数码管显示
实例代码
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar duanZhi[]= {0,0};
sbit P36=P3^6;
sbit P37=P3^7;
sbit ST=P3^0;
sbit SH=P3^2;
sbit DS=P3^1;
sbit P33=P3^3;
sbit P34=P3^4;
sbit P35=P3^5;
uchar shi,ge;
uchar tmp;
static uchar keynum=0;
void delay(uint z)
{
uint x,y;
for(x=z; x>>0; x--)
for(y=110; y>>0; y--);
}
void SendTo595(uchar byteData);
void display()
{
ge = keynum%10;
shi = keynum/10;
duanZhi[0]=table[ge];
duanZhi[1]=table[shi];
P37=0;
SendTo595(duanZhi[0]);
delay(2);
P37=1;
P36=0;
SendTo595(duanZhi[1]);
delay(2);
P36=1;
}
void SendTo595(uchar byteData)
{
uchar i=0;
ST = 0;
for(i; i<8; i++)
{
SH = 0;
if(byteData&0x80)DS=1;
else DS=0;
byteData = byteData <<1;
SH = 1;
_nop_();
_nop_();
SH = 0;
}
ST =1;
_nop_();
_nop_();
ST = 0;
}
void key_scan()
{
P0=0xff;
delay(6);
if(P0!=0xff)
{
tmp=P0;
switch(tmp)
{
case 0xfe:
keynum=57;
break;
case 0xfd:
keynum=58;
break;
case 0xfb:
keynum=59;
break;
case 0xf7:
keynum=60;
break;
case 0xef:
keynum=61;
break;
case 0xdf:
keynum=62;
break;
case 0xbf:
keynum=63;
break;
case 0x7f:
keynum=64;
break;
}
}
P0=0xef;
delay(6);
if(P0!=0xef)
{
tmp=P0;
switch(tmp)
{
case 0x7e:
keynum=50;
break;
case 0x7d:
keynum=51;
break;
case 0x7b:
keynum=52;
break;
case 0x77:
keynum=53;
break;
case 0x6f:
keynum=54;
break;
case 0x5f:
keynum=55;
break;
case 0x3f:
keynum=56;
break;
}
}
delay(6);
P0=0xbf;
if(P0!=0xbf)
{
tmp=P0;
switch(tmp)
{
case 0x7e:
keynum=43;
break;
case 0x7d:
keynum=44;
break;
case 0x7b:
keynum=45;
break;
case 0x77:
keynum=46;
break;
case 0x6f:
keynum=47;
break;
case 0x5f:
keynum=48;
break;
case 0x3f:
keynum=49;
break;
}
}
P0=0xdf;
delay(6);
if(P0!=0xdf)
{
tmp=P0;
switch(tmp)
{
case 0xde:
keynum=36;
break;
case 0xdd:
keynum=37;
break;
case 0xdb:
keynum=38;
break;
case 0xd7:
keynum=39;
break;
case 0xcf:
keynum=40;
break;
case 0x9f:
keynum=41;
break;
case 0x5f:
keynum=42;
break;
}
}
P0=0xef;
delay(6);
if(P0!=0xef)
{
tmp=P0;
switch(tmp)
{
case 0xee:
keynum=29;
break;
case 0xed:
keynum=30;
break;
case 0xeb:
keynum=31;
break;
case 0xe7:
keynum=32;
break;
case 0xcf:
keynum=33;
break;
case 0xaf:
keynum=34;
break;
case 0x6f:
keynum=35;
break;
}
}
P0=0xf7;
delay(6);
if(P0!=0xf7)
{
tmp=P0;
switch(tmp)
{
case 0xf6:
keynum=22;
break;
case 0xf5:
keynum=23;
break;
case 0xf3:
keynum=24;
break;
case 0xe7:
keynum=25;
break;
case 0xd7:
keynum=26;
break;
case 0xb7:
keynum=27;
break;
case 0x77:
keynum=28;
break;
}
}
P0=0xfb;
delay(6);
if(P0!=0xfb)
{
tmp=P0;
switch(tmp)
{
case 0xfa:
keynum=15;
break;
case 0xf9:
keynum=16;
break;
case 0xf3:
keynum=17;
break;
case 0xeb:
keynum=18;
break;
case 0xdb:
keynum=19;
break;
case 0xbb:
keynum=20;
break;
case 0x7b:
keynum=21;
break;
}
display();
}
P0=0xfb;
delay(6);
if(P0!=0xfb)
{
tmp=P0;
switch(tmp)
{
case 0xfc:
keynum=8;
break;
case 0xf9:
keynum=9;
break;
case 0xf5:
keynum=10;
break;
case 0xed:
keynum=11;
break;
case 0xdd:
keynum=12;
break;
case 0xbd:
keynum=13;
break;
case 0x7d:
keynum=14;
break;
}
}
P0=0xfe;
delay(6);
if(P0!=0xfe)
{
tmp=P0;
switch(tmp)
{
case 0xfc:
keynum=1;
break;
case 0xfa:
keynum=2;
break;
case 0xf6:
keynum=3;
break;
case 0xee:
keynum=4;
break;
case 0xde:
keynum=5;
break;
case 0xbe:
keynum=6;
break;
case 0x7e:
keynum=7;
break;
}
}
display();
}
void main()
{
keynum=0;
P0=0xff;
while(1)
{
key_scan();
}
}
proteus仿真软件里面有些bug
- 注意:二极管选型,不能选择发光二极管,有些其他的二极管也不好使,我使用的是
10A01 - 注意走线,线路连接中最好不要出现重复线或者点,有多余的线或者连接点都要删除,不然也会影响仿真效果,电阻选择不能过大。
|