C51單片機(jī)矩陣鍵盤掃描去抖程序
{
P5 = 0; //行線全數(shù)置為0
EX1 = 1; // 允許外部鐘表秒間斷
IT1 = 1; // 外部鐘表間斷配備布置為邊緣觸發(fā)
}
void Key_Int() interrupt 2
{
Key_Pressed = 1;
EX1 = 0;
}
void Scan_Key()
{
unsigned char temp,rowvalue;
unsigned int key;
int i;
temp = P2;
temp = 0x3C;
if(temp == 0x3C)
{
Key_Released = 0;
Key_Pressed = 0;
key = 滅茬_KEY;
EX1 = 1;
}
else
{
key = temp;
key = key8;
rowvalue = 0x01;
for(i=0;i5;i )
{
P5 = rowvalue
DelayMs⑴;
temp = P2;
temp = 0x3C;
if(temp == 0x3c)
{
rowvalue = rowvalue
key = key | rowvalue;
P5 = 0x00;
break;
}
}
P5 = 0x00;
DelayMs⑴;
}
if(key!=滅茬_KEY) //如果有鍵按下
{ if(key==Key_Value) //如果按下的是不異的鍵
{
if(Keypress_Count>=KEY_DELAY)
{
Key_Down = 1;
}
}
else if(Key_Down != 1)
{
Keypress_Count=0;
Keyrelease_Count = 0;
Key_Value=key;
}
}
else //如果無(wú)鍵按下
{
if(Key_Down) //如果時(shí)下是鍵開(kāi)釋,返回鍵值
{
if(Keyrelease_Count >= KEY_DELAY)
{
Key_Down=0;
Keypress_Count=0;
Keyrelease_Count=0;
Key_Released = 1;
EX1 = 1;
return;
}
}
else
{
Keypress_Count=0;
Keyrelease_Count=0;
Key_Value = 滅茬_KEY;
EX1 = 1;
return;
}
}
}
在main.c中的挪用要領(lǐng)為
if(Key_Pressed == 1)
{
//Key_Pressed = 0;
Scan_Key();
}
if(Key_Released == 1)
{
Key_Released = 0;
Ack_Key();
}
DIY機(jī)械鍵盤相關(guān)社區(qū):機(jī)械鍵盤DIY
評(píng)論