本文通过存储控制实现按键控制数码管显示0~f
硬件方面注意51单片机signal应与gnd短接
代码思路为:
扫描程序代码
需要先将行列引脚命名
逐行扫描设置行值列值
按键改变列值由此检测出哪个按键被按下
#include <reg52.h>
#include <absacc.h>
unsigned char code SMGduanm[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
sbit R1=P3^0;//将键盘行列IO口先设出以便单独控制
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C1=P3^7;
sbit C2=P3^6;
sbit C3=P3^5;
sbit C4=P3^4;
void delay(unsigned char t)
{
while(t--);
while(t--);
}
void SMGdisplay(unsigned char pos,unsigned char dat)
{
XBYTE[0xc000]=0x01<<pos;
XBYTE[0xe000]=SMGduanm[dat];
delay(500);
}
void keyscan(unsigned char i)
{
switch(i)
{
case 1:
R1=0;R2=R3=R4=1;
if(C1==0)
{ while(C1==0);
SMGdisplay(0,0);
}
else if(C2==0)
{ while(C2==0);
SMGdisplay(0,1);
}
else if(C3==0)
{ while(C3==0);
SMGdisplay(0,2);
}
else if(C4==0)
{ while(C4==0);
SMGdisplay(0,3);
}
break;
//===========
case 2:
R2=0;R1=R3=R4=1;
if(C1==0)
{ while(C1==0);
SMGdisplay(0,4);
}
if(C2==0)
{ while(C2==0);
SMGdisplay(0,5);
}
else if(C3==0)
{ while(C3==0);
SMGdisplay(0,6);
}
else if(C4==0)
{ while(C4==0);
SMGdisplay(0,7);
}
break;
//==============
case 3:
R3=0;R1=R2=R4=1;
if(C1==0)
{ while(C1==0);
SMGdisplay(0,8);
}
else if(C2==0)
{ while(C2==0);
SMGdisplay(0,9);
}
else if(C3==0)
{ while(C3==0);
SMGdisplay(0,10);
}
else if(C4==0)
{ while(C4==0);
SMGdisplay(0,11);
}
break;
//===========
case 4:
R4=0;R1=R3=R2=1;//扫描哪一行,哪一行为IO口0
if(C1==0)
{ while(C1==0);
SMGdisplay(0,12);
}
else if(C2==0)
{ while(C2==0);
SMGdisplay(0,13);
}
else if(C3==0)
{ while(C3==0);
SMGdisplay(0,14);
}
else if(C4==0)
{ while(C4==0);
SMGdisplay(0,15);
}
break;
}
}
unsigned char a;
void main()
{
while(1)
{ for(a=1;a<5;a++)
{C1=C2=C3=C4=1;//每次要软件将按键制成1
keyscan(a);
}
}
}
如果对你有帮助的话,你的支持是对我最大的鼓励哦