新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 51單片機矩陣鍵盤與左右流水燈控制C程序

51單片機矩陣鍵盤與左右流水燈控制C程序

作者: 時間:2016-12-02 來源:網(wǎng)絡 收藏

//第二次矩陣按鍵檢測
P3 = 0xfd; //給P3口賦初值,使得矩陣按鍵的第二行置0,其余行為高電平;
temp = P3;//將用于檢測的值賦給變量temp
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);//消抖
//重新裝入待于檢測的初值;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
//確定矩陣按鍵有鍵被按下
//將此時的P3口的值賦給temp
FM = 0;
temp = P3;
//開始檢測
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;
}
//檢測按下釋放
while(temp != 0xf0)
{
//重新裝入初值
temp = P3;
temp &= 0xf0;
}
delay(5);//釋放去抖
while(temp != 0xf0)
{
//重新裝入初值
temp = P3;
temp &= 0xf0;
}
//按鍵釋放關閉蜂鳴器
FM = 1;
}
}
//第三次矩陣按鍵檢測
P3 = 0xfb;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);//消抖
//重新裝入檢測初值
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
//將此時P3口的值賦給temp
temp = P3;
FM = 0;//蜂鳴器發(fā)聲
//檢測按下的是哪一個按鍵,行列檢測
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;
}
//檢測按鍵釋放
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
//按鍵釋放關閉蜂鳴器
FM = 1;
}
}
//第四次檢測矩陣按鍵
P3 = 0xf7;//第四行為低電平,其余行為高電平;
temp = P3;//將待檢測的值賦給temp;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5); //消抖
//重新裝入檢測初值
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
//確認有按鍵按下
//將此時的P3口的實際值賦給temp;
temp = P3;
FM = 0;
//開始檢測
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;
}
//檢測按鍵釋放
while(temp != 0xf0)
{ //重新裝入初值
temp = P3;
temp &= 0xf0;
}
delay(5);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
//關閉蜂鳴器
FM = 1;
}
}
}
//定時器0中斷服務子程序
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0X00;
counter++;
if(flag== 0)
{ //流水燈左移流動
if(counter % 1 == 0)
{
P1 = ~(1 << x++);
if(x == 8)
{
x = 0;
}

}
}
else
{ //流水燈右移流動
if(counter % 1 == 0)
{
P1 = ~(0x80 >> x++);
if(x == 8)
{
x = 0;
}
}
}
}

本文引用地址:http://butianyuan.cn/article/201612/324623.htm

上一頁 1 2 下一頁

評論


技術專區(qū)

關閉