|
/*4x4軟鍵盤測試
鍵盤布局如下:
1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D
程序中*顯示為E,#顯示為F
數碼管接P0、P2^4-P2^7
鍵盤接P1
*/
#include <reg51.h>
#include <intrins.h>
sbit ge = P2^4;
sbit shi = P2^5;
sbit bai = P2^6;
sbit qian= P2^7;
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={ //共陽極LED數碼管顯示數字0~F
0xf9,0xa4,0xb0,0x88,
0x99,0x92,0x82,0x83,
0xf8,0x80,0x90,0xc6,
0xa1,0xc0,0x86,0x8e
};
uchar Key_Value;
void Delay_1ms(uint x)
{
uchar i, j;
for(i = 0; i < x; i++) for(j = 0; j <= 148; j++);
}
void Getkey()
{
uchar i, j, temp,num,Key_Temp1, Key_Temp2, Buffer[4] = {0xfe, 0xfd, 0xfb, 0xf7};
for(j = 0; j < 4; j++) //循環四次
{
P1 = Buffer[j];
_nop_();
_nop_();
temp = 0x10;
for(i = 0; i < 4; i++) //循環四次
{
if(!(P1 & temp))
{
num=i + j * 4; //返回取得的按鍵值
}
temp <<= 1; //換左邊一位
}
}
P1 = 0xff;
Key_Temp1 = num; //讀入按鍵
if(Key_Temp1 < 16) //有鍵按下
{
Delay_1ms(5); //延時消抖
Key_Temp2 =num; //再讀一次
if (Key_Temp1 == Key_Temp2) //兩次相等
Key_Value = Key_Temp1; //就確認下來
}
}
void Display(uchar k)
{
ge=1;
P0=table[k];
Delay_1ms(5);
ge=0;
shi=1;
P0=table[k];
Delay_1ms(5);
shi=0;
bai=1;
P0=table[k];
Delay_1ms(5);
bai=0;
qian=1;
P0=table[k];
Delay_1ms(5);
qian=0;
/* dula=0;
wela=1;
P0=0x01;
wela=0;
Delay_1ms(5);
wela=1;
P0=0x00; */
}
void Main(void)
{
while(1)
{
Getkey();
Display(Key_Value); //顯示鍵值
}
}
|
|