//2014年6月1日23:20:17
#include <reg52.h>
sbit KEY_IN_4 = P2^7;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_1 = P2^4;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[] = { 0xc0, 0xf9, 0xa4, 0xb0,
0x99, 0x92, 0x82, 0xf8,
0x80, 0x90, 0x88, 0x83,
0xa7, 0xa1, 0x86, 0x8e };
unsigned char KeyState[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
void main()
{
unsigned char i, j;
unsigned char backup[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
ADDR0 = 0;
ADDR1 = 0;
ADDR2 = 0;
ADDR3 = 1;
ENLED = 0;
TMOD = 0X01;
TH1 = 0XFC;
TL1 = 0X67;
EA = 1;
ET1 = 1;
TR1 = 1;
P0 = LedChar[0];
while(1)
{
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(backup[i][j] != KeyState[i][j])
{
if(backup[i][j] == 0 )
{
P0 = LedChar[4*i + j];
}
backup[i][j] = KeyState[i][j];
}
}
}
}
}
void key() interrupt 3
{
sta
tic unsigned char keybuff[4][4] = {{0xff, 0xff, 0xff, 0xff},{0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff},{0xff, 0xff, 0xff, 0xff}};
static unsigned char keyout = 0;
unsigned char i;
TH1 = 0XFC;
TL1 = 0X67;
keybuff[keyout][0] = (keybuff[keyout][0] << 1) | KEY_IN_1;
keybuff[keyout][1] = (keybuff[keyout][1] << 1) | KEY_IN_2;
keybuff[keyout][2] = (keybuff[keyout][2] << 1) | KEY_IN_3;
keybuff[keyout][3] = (keybuff[keyout][3] << 1) | KEY_IN_4;
for(i=0; i<4; i++)
{
if(keybuff[keyout][i] & 0x0f == 0x0f)
{
KeyState[keyout][i] = 1;
}
else if((keybuff[keyout][i] & 0x0F) == 0x00) //注意这里(keybuff[keyout][i] & 0x0f)一定要加()
{
KeyState[keyout][i] = 0;
}
else
{
}
}
keyout++; //请注意这里
keyout = keyout & 0x03; //请注意这里
switch (keyout)
{
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
/* keyout++;
keyout = keyout & 0x03; //若上面的两行移到下面 键盘显示值 就错位了 为什么 不解
*/
}
//程序问题不大 ,但是有问题不明白 ,在上面两行语句的位置变化。希望大家能帮我解决一下 ,万分谢谢。有兴趣可以叫我qq1012421396
keyout = keyout & 0x03; //若上面的两行移到下面 键盘显示值 就错位了 为什么 不解
*/
keyout可以简单的理解为列值
一周热门 更多>