89C52單片機(jī)矩陣鍵盤行列掃描
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^6;
sbit dela=P2^7;
uchar code wela_table[]={
0xFE,0xFD,0xFB,0xF7,
0xEF,0xDF,0xBF,0x7F};
uchar code dela_table[]={
0x3F,0x06,0x5B,0x4F,
0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,
0x39,0x5E,0x79,0x71};
uchar temp,num;
void Init();
void DelayMs(uchar a);
void DelayUs2(uchar b);
void KeyScan();
void Display();
/*---------------------------------------------------------------
主函數(shù)
------------------------------------------------------------------*/
void main()
{
Init();
while(1)
{
KeyScan();
Display();
}
}
/*----------------------------------初始化函數(shù)----------------------------*/
void Init()
{
num=0;
}
/*---------------------------------鍵盤掃描--------------------------------*/
void KeyScan()
{
/*--------------------------第一行檢測(cè)--------------------------*/
P3=0xFE;
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
DelayMs(250);
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0x70: num=1;
break;
case 0xB0: num=2;
break;
case 0xD0: num=3;
break;
case 0xE0: num=4;
break;
}
}
while(temp!=0xF0)
{
temp=P3;
temp=temp&0xF0;
Display();
}
}
/*--------------------------第二行檢測(cè)--------------------------*/
P3=0xFD;
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
DelayMs(50);
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0x70: num=5;break;
case 0xB0: num=6;break;
case 0xD0: num=7;break;
case 0xE0: num=8;break;
}
}
while(temp!=0xF0)
{
temp=P3;
temp=temp&0xF0;
Display();
}
}
/*--------------------------第三行檢測(cè)--------------------------*/
P3=0xFB;
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
DelayMs(50);
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0x70: num=9;break;
case 0xB0: num=10;break;
case 0xD0: num=11;break;
case 0xE0: num=12;break;
}
}
while(temp!=0xF0)
{
temp=P3;
temp=temp&0xF0;
Display();
}
}
/*--------------------------第四行檢測(cè)--------------------------*/
P3=0xF7;
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
DelayMs(50);
temp=P3;
temp=temp&0xF0;
if(temp!=0xF0)
{
switch(temp)
{
case 0x70: num=13;break;
case 0xB0: num=14;break;
case 0xD0: num=15;break;
case 0xE0: num=0;break;
}
}
while(temp!=0xF0)
{
temp=P3;
temp=temp&0xF0;
Display();
}
}
}
/*----------------------------------延時(shí)函數(shù)-----------------------------*/
void DelayMs(uchar a)
{
while(a)
{
DelayUs2(245);
DelayUs2(245);
a--;
}
}
void DelayUs2(uchar b)
{
while(b)
{
b--;
}
}
/*-------------------------------顯示函數(shù)------------------------------------*/
void Display()
{
P0=0xFF;
wela=1;
wela=0;
P0=dela_table[num];
dela=1;
dela=0;
P0=wela_table[5];
wela=1;
wela=0;
DelayUs2(50);
}
評(píng)論