51单片机8x9矩阵键盘+数码管+74HC595+Proteus仿真
- Proteus仿真
实现原理
扫键流程:①先检测扫描端口P0,检测自身的8个IO口,是否被拉低,检测完自身;之后,②再对’进行P1口进行扫键,②与8x8扫键流程一样了。
实例代码
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar duanZhi[]= {0,0};
uchar tmp;
uchar keynum;
uchar shi,ge;
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;
void display();
void display2();
void buzzer();
void delay(uint z)
{
uint x,y;
for(x=z; x>>0; x--)
for(y=110; y>>0; y--);
}
void keyscan()
{ P0=0xff;
if(P0 !=0xff){
tmp=P0;
switch(tmp) {
case 0xfe:
keynum=65;
break;
case 0xfd:
keynum=66;
break;
case 0xfb:
keynum=67;
break;
case 0xf7:
keynum=68;
break;
case 0xef:
keynum=69;
break;
case 0xdf:
keynum=70;
break;
case 0xbf:
keynum=71;
break;
case 0x7f:
keynum=72;
break;
}
}
P0=0xfe;
if(P2!=0xff)
{
tmp=P2;
switch(tmp) {
case 0xfe:
keynum=1;
break;
case 0xfd:
keynum=2;
break;
case 0xfb:
keynum=3;
break;
case 0xf7:
keynum=4;
break;
case 0xef:
keynum=5;
break;
case 0xdf:
keynum=6;
break;
case 0xbf:
keynum=7;
break;
case 0x7f:
keynum=8;
break;
}
}
delay(5);
P0=0xfd;
if(P2!=0xff)
{
tmp=P2;
switch(tmp) {
case 0xfe:
keynum=9;
break;
case 0xfd:
keynum=10;
break;
case 0xfb:
keynum=11;
break;
case 0xf7:
keynum=12;
break;
case 0xef:
keynum=13;
break;
case 0xdf:
keynum=14;
break;
case 0xbf:
keynum=15;
break;
case 0x7f:
keynum=16;
break;
}
}
delay(5);
P0=0xfb;
if(P2!=0xff)
{
tmp=P2;
switch(tmp)
{
case 0xfe:
keynum=17;
break;
case 0xfd:
keynum=18;
break;
case 0xfb:
keynum=19;
break;
case 0xf7:
keynum=20;
break;
case 0xef:
keynum=21;
break;
case 0xdf:
keynum=22;
break;
case 0xbf:
keynum=23;
break;
case 0x7f:
keynum=24;
break;
}
}
delay(5);
P0=0xf7;
if(P2!=0xff)
{
tmp=P2;
switch(tmp) {
case 0xfe:
keynum=25;
break;
case 0xfd:
keynum=26;
break;
case 0xfb:
keynum=27;
break;
case 0xf7:
keynum=28;
break;
case 0xef:
keynum=29;
break;
case 0xdf:
keynum=30;
break;
case 0xbf:
keynum=31;
break;
case 0x7f:
keynum=32;
break;
}
}
delay(5);
P0=0xef;
if(P2!=0xff)
{
tmp=P2;
switch(tmp)
{
case 0xfe:
keynum=33;
break;
case 0xfd:
keynum=34;
break;
case 0xfb:
keynum=35;
break;
case 0xf7:
keynum=36;
break;
case 0xef:
keynum=37;
break;
case 0xdf:
keynum=38;
break;
case 0xbf:
keynum=39;
break;
case 0x7f:
keynum=40;
break;
}
}
delay(5);
P0=0xdf;
if(P2!=0xff)
{
tmp=P2;
switch(tmp)
{
case 0xfe:
keynum=41;
break;
case 0xfd:
keynum=42;
break;
case 0xfb:
keynum=43;
break;
case 0xf7:
keynum=44;
break;
case 0xef:
keynum=45;
break;
case 0xdf:
keynum=46;
break;
case 0xbf:
keynum=47;
break;
case 0x7f:
keynum=48;
break;
}
}
delay(5);
P0=0xbf;
if(P2!=0xff)
{
tmp=P2;
switch(tmp)
{
case 0xfe:
keynum=49;
break;
case 0xfd:
keynum=50;
break;
case 0xfb:
keynum=51;
break;
case 0xf7:
keynum=52;
break;
case 0xef:
keynum=53;
break;
case 0xdf:
keynum=54;
break;
case 0xbf:
keynum=55;
break;
case 0x7f:
keynum=56;
break;
}
}
delay(5);
P0=0x7f;
if(P2!=0xff)
{
tmp=P2;
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;
}
tmp=P2;
}
delay(5);
}
void display()
{
ge = keynum%10;
shi = keynum/10;
P37=0;
P1=table[ge];
delay(5);
P37=1;
P36=0;
P1=table[shi];
delay(5);
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 display2()
{
ge = keynum%10;
shi = keynum/10;
duanZhi[0]=table[ge];
duanZhi[1]=table[shi];
P34=0x00;
SendTo595(duanZhi[0]);
delay(5);
P34=0x01;
P33=0x00;
SendTo595(duanZhi[1]);
delay(5);
P33=0x01;
}
void main()
{
while(1)
{
keyscan();
display();
display2();
}
}
void buzzer()
{
if(keynum !=0) {
P35 = 0x01;
delay(1);
P35 = 0x00;
delay(1);
}
}
|