#define KEYIO PT1AD0
#define KEYIODDR DDR0AD0
//=================== 按键扫描 ======================
uchar KeyScan(void)
{
uchar keyValue = 0;
KEYIODDR = 0xFF; //初始化 AD[15:8]输出
KEYIO = 0xF0; //列=高电平 行=低电平
delay_ms(1);
KEYIODDR = 0xF0; //AD[15:12]输出 AD[11:8]输入 扫描行值
delay_ms(1);
if(KEYIO != 0xF0)
{
KEYIO = 0xF0; //列=高电平 行=低电平
delay_ms(10);
if(KEYIO != 0xF0)
{
keyValue = KEYIO & 0x0F; //保存行值
delay_ms(1);
KEYIODDR = 0xFF; //初始化 AD[15:8 ]输出
KEYIO = 0x0F; //列=低电平 行=高电平
delay_ms(1);
KEYIODDR = 0x0F; //AD[15:12]输入 AD[11:8]输出 扫描列值
delay_ms(1);
keyValue |= (KEYIO & 0xF0); //合并行列值
KEYIO = 0x0F; //列=低电平 行=高电平
delay_ms(1);
while(KEYIO != 0x0F); //等待按键释放
switch(keyValue)
{
case 0x88: keyValue = 0x01;break;
case 0x84: keyValue = 0x02;break;
case 0x82: keyValue = 0x03;break;
case 0x81: keyValue = 0x0A;break;
case 0x48: keyValue = 0x04;break;
case 0x44: keyValue = 0x05;break;
case 0x42: keyValue = 0x06;break;
case 0x41: keyValue = 0x0B;break;
case 0x28: keyValue = 0x07;break;
case 0x24: keyValue = 0x08;break;
case 0x22: keyValue = 0x09;break;
case 0x21: keyValue = 0x0C;break;
case 0x18: keyValue = 0x0E;break;
case 0x14: keyValue = 0x00;break;
case 0x12: keyValue = 0x0F;break;
case 0x11: keyValue = 0x0D;break;
default: keyValue=0;break;
}
return (keyValue + 1);
}
}
return keyValue;
}
4x4 矩阵键盘
最新推荐文章于 2022-12-08 22:30:20 发布