51矩阵键盘

//按键码表
uchar code KeyCodeTable[]=
{
  	0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88
};
//延迟小程序,一般20ms
void Delay(void)
{
 	uchar i;
	for(i=0;i<200;i++);
}
//按键检测程序
uchar Keys_Scan(void)
{
 	uchar sCode,kCode,i,k;
	P1 = 0xf0;
	//是否有键被按下
	if((P1&0xf0)!=0xf0)
	{
	 	Delay();
		if((P1&0xf0)!=0xf0)
		{
		 	sCode = 0xfe;
			for(k=0;k<4;k++)
			{
			 	P1 = sCode;
				if((P1&0xf0)!=0xf0)
				{
				 	kCode = ~P1;
					for(i=0;i<16;i++)
					{
					 	if(kCode == KeyCodeTable[i])
							return i;
					}
				}
				else 
				{
					sCode = _crol_(sCode,1);
				}
			}
		}
	}
	return -1;
}

你可能感兴趣的:(51矩阵键盘)