#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
sfr KeyPort=0x90;//#define KeyPort P1 -----行在P1口低四位
sbit Key_C1=P1^4; //第1列接P1.4
sbit Key_C2=P1^5; //第2列接P1.5
sbit Key_C3=P1^6; //第3列接P1.6
sfr Led=0x80; //#define Led P0--------P0口LED
void delay(uchar ms); 函数的声明
void main()
{
Led=0xfe; 定义一个LED点亮
while(1)
{
KeyPort=0x6f; //0110 1111
if(KeyPort!=0x6f) //是否有键按下
{
delay(10); //延时10ms去抖
if(KeyPort!=0x6f) //再判断一下
{
switch(KeyPort) //进行矩阵键盘按键判断
{
case 0x6e://0110 1110
Led=_crol_(Led,1);
break;
case 0x6d://0110 1101
Led=_crol_(Led,4);
break;
case 0x6b://0110 1011
Led=_crol_(Led,7);
break;
case 0x67://0110 0111
Led=_crol_(Led,10);
break;
}
while(KeyPort!=0x6f); //松手检测
}
}
KeyPort=0x5f;//0101 1111
if(KeyPort!=0x5f)
{
delay(10);
if(KeyPort!=0x5f)
{
switch(KeyPort)
{
case 0x5e://0101 1110
Led=_crol_(Led,2);
break;
case 0x5d://0101 1101
Led=_crol_(Led,5);
break;
case 0x5b://0101 1011
Led=_crol_(Led,8);
break;
case 0x57://0101 0111
Led=_crol_(Led,11);
break;
}
while(KeyPort!=0x5f);
}
}
KeyPort=0x3f;//0011 1111
if(KeyPort!=0x3f)
{
delay(10);
if(KeyPort!=0x3f)
{
switch(KeyPort)
{
case 0x3e://0011 1110
Led=_crol_(Led,3);
break;
case 0x3d://0011 1101
Led=_crol_(Led,6);
break;
case 0x3b://0011 1011
Led=_crol_(Led,9);
break;
case 0x37://0011 0111
Led=_crol_(Led,12);
break;
}
while(KeyPort!=0x3f);
}
}
}
}
void delay(uchar ms)
{
uchar i;
for(;ms>0;ms--)
for(i=0;i<244;i++);
}