51單片機(jī)矩陣鍵盤(pán)與左右流水燈控制C程序
//第二次矩陣按鍵檢測(cè)
P3 = 0xfd; //給P3口賦初值,使得矩陣按鍵的第二行置0,其余行為高電平;
temp = P3;//將用于檢測(cè)的值賦給變量temp
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);//消抖
//重新裝入待于檢測(cè)的初值;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
//確定矩陣按鍵有鍵被按下
//將此時(shí)的P3口的值賦給temp
FM = 0;
temp = P3;
//開(kāi)始檢測(cè)
switch(temp)
{
case 0xed: key1 = 5 * 5;
break;
case 0xdd: key1 = 6 * 6;
break;
case 0xbd: key1 = 7 * 7;
break;
case 0x7d: key1 = 8 * 8;
break;
default:
break;
}
//檢測(cè)按下釋放
while(temp != 0xf0)
{
//重新裝入初值
temp = P3;
temp &= 0xf0;
}
delay(5);//釋放去抖
while(temp != 0xf0)
{
//重新裝入初值
temp = P3;
temp &= 0xf0;
}
//按鍵釋放關(guān)閉蜂鳴器
FM = 1;
}
}
//第三次矩陣按鍵檢測(cè)
P3 = 0xfb;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);//消抖
//重新裝入檢測(cè)初值
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
//將此時(shí)P3口的值賦給temp
temp = P3;
FM = 0;//蜂鳴器發(fā)聲
//檢測(cè)按下的是哪一個(gè)按鍵,行列檢測(cè)
switch(temp)
{
case 0xeb: key1 = 9 * 9;
break;
case 0xdb: key1 = 10 * 10;
break;
case 0xbb: key1 = 11 * 11;
break;
case 0x7b: key1 = 12 * 12;
break;
default:
break;
}
//檢測(cè)按鍵釋放
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
//按鍵釋放關(guān)閉蜂鳴器
FM = 1;
}
}
//第四次檢測(cè)矩陣按鍵
P3 = 0xf7;//第四行為低電平,其余行為高電平;
temp = P3;//將待檢測(cè)的值賦給temp;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5); //消抖
//重新裝入檢測(cè)初值
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
//確認(rèn)有按鍵按下
//將此時(shí)的P3口的實(shí)際值賦給temp;
temp = P3;
FM = 0;
//開(kāi)始檢測(cè)
switch(temp)
{
case 0xe7: key1 = 13 * 13;
break;
case 0xd7: key1 = 14 * 14;
break;
case 0xb7: key1 = 15 * 15;
break;
case 0x77: key1 = 16 * 16;
break;
default:
break;
}
//檢測(cè)按鍵釋放
while(temp != 0xf0)
{ //重新裝入初值
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
//關(guān)閉蜂鳴器
FM = 1;
}
}
}
//定時(shí)器0中斷服務(wù)子程序
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0X00;
counter++;
if(flag== 0)
{ //流水燈左移流動(dòng)
if(counter % 1 == 0)
{
P1 = ~(1 << x++);
if(x == 8)
{
x = 0;
}
}
}
else
{ //流水燈右移流動(dòng)
if(counter % 1 == 0)
{
P1 = ~(0x80 >> x++);
if(x == 8)
{
x = 0;
}
}
}
}本文引用地址:http://m.butianyuan.cn/article/201612/324623.htm
評(píng)論