永发信息网

单片机矩阵按键,求大神解答

答案:2  悬赏:60  手机版
解决时间 2021-02-03 00:33
  • 提问者网友:精神病院里
  • 2021-02-02 15:29
#include

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

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;

unsigned char KeySta[4][4]={ {1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
};

unsigned char code LedChar[]={0xC0, 0xF9, 0xA4, 0xB0, 0x99,
0x92, 0x82, 0xF8, 0x80, 0x90,
0x83, 0xC6, 0xA1, 0x86, 0x8E};

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},
};
EA=1;
ADDR3 = 1;
ENLED = 0;
ADDR0 = 0;
ADDR1 = 0;
ADDR2 = 0;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0=1;
TR0=1;
P0 = LedChar[0];

while(1)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if (backup[i][j] != KeySta[i][j])
{
if (backup[i][j] == 0)
{
P0 = LedChar[i*4+j];
}
backup[i][j] = KeySta[i][j];
}
}
}
}
}

void InterruptTimer0() interrupt 1
{
unsigned char i;
static unsigned keyout = 0;
unsigned char keychec[4][4] = {
{0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF}
};
TH0 = 0xfc;
TL0 = 0x67;

keychec[keyout][0] = (keychec[keyout][0] << 1) | KEY_IN_1;
keychec[keyout][1] = (keychec[keyout][1] << 1) | KEY_IN_2;
keychec[keyout][2] = (keychec[keyout][2] << 1) | KEY_IN_3;
keychec[keyout][3] = (keychec[keyout][3] << 1) | KEY_IN_4;

for(i=0;i<4;i++)
{
if((keychec[keyout][i] & 0x0F) == 0x00)
{
KeySta[keyout][i] = 0;
}
else if((keychec[keyout][i] & 0x0F) == 0x0F)
{
KeySta[keyout][i] = 1;
}
}
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;
}

}

中断为扫描按键并去抖,主函数刷新数码管
最佳答案
  • 五星知识达人网友:野慌
  • 2021-02-02 16:43
不可能。即便程序OK,没有在硬件连接的基础上说事,也是空谈。
全部回答
  • 1楼网友:夜余生
  • 2021-02-02 17:27
.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯