課程設(shè)計:矩陣鍵盤掃描顯示(c51反轉(zhuǎn)法)
//矩陣鍵盤掃描程序
//采用翻轉(zhuǎn)識別法(相對代碼少一些)
#include
#define uchar unsigned char
#define uint unsigned int
uchar key=0;
//共陽數(shù)碼管編碼如下(0-15)
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
//函數(shù)聲明
void display(uchar Duan_value);
void delay(uchar t);
void key_scan();
void main()
{
while(1)
{
display(key);//顯示按鍵值
key_scan();//按鍵掃描
}
}
//延時函數(shù)
void delay(uchar t)
{
uchar x,y;
for(x=t;t>0;t--)
for(y=111;y>0;y--);
}
//數(shù)碼管掃描函數(shù)
void display(uchar value)
{
P2=table[value];
}
//按鍵掃描函數(shù)
void key_scan()
{
uchar m0,m1;
uchar temp;
P1=0xf0;//這樣設(shè)置是為了能使低四位將高四位拉低,釋放時自動拉高高四位
temp=P1;
if(temp!=0xf0)
{
delay(10);//延時,去除抖動
if(temp!=0xf0)
{
m0=temp;//獲得按鍵的列號
P1=0x0f;
temp=P1;
if(temp!=0x0f)
{
m1=temp;//獲得按鍵的行號號
temp=m0|m1;//獲得最終的號
}
switch(temp)
{
case 0xee:key=0;break;
case 0xde:key=1;break;
case 0xbe:key=2;break;
case 0x7e:key=3;break;
case 0xed:key=4;break;
case 0xdd:key=5;break;
case 0xbd:key=6;break;
case 0x7d:key=7;break;
case 0xeb:key=8;break;
case 0xdb:key=9;break;
case 0xbb:key=10;break;
case 0x7b:key=11;break;
case 0xe7:key=12;break;
case 0xd7:key=13;break;
case 0xb7:key=14;break;
case 0x77:key=15;break;
}
do{
temp=P1;//消除按起抖動
temp&=0X0f;
}while(temp!=0x0f);
}
}
}
評論