这是电路图
仿真文件和完好源码下载:http://www.51hei.com/bbs/dpj-22586-1.html
下面是矩阵键盘.c文件:
#include
#include “delay_ms.h”//使用函数delay_ms(unsigned int x)延时 x ms
#include “矩阵键盘.h” //头文件中宏界说#define JPJK P3
void baoj1();
unsigned char keyscan()
{
unsigned char temp,key;
key = 0xff;
JPJK = 0xfe; //将榜首行线置低电平
temp = JPJK;//读JPJK的当时状况到 temp
temp &= 0xf0;// temp = temp & 0xf0 按位与
if(temp != 0xf0)
{
delay_ms(10);//延时去抖
temp = JPJK;
temp &= 0xf0;
if(temp != 0xf0)//榜首行有键被按下
{
temp = JPJK;//读被按下的键
baoj1();
switch(temp)
{
case 0xee: key = 1;break;
case 0xde: key = 2;break;
case 0xbe: key = 3;break;
case 0x7e: key = 0x0a;break;
}
while(temp != 0xf0)//等候按键开释
{
temp = JPJK;
temp &= 0xf0;
}
}
}
JPJK = 0xfd; //将第二行线置低电平
temp = JPJK;//读JPJK的当时状况到 temp
temp &= 0xf0;// temp = temp & 0xf0
if(temp != 0xf0)
{
delay_ms(10);//延时去抖
temp = JPJK;
temp &= 0xf0;
if(temp != 0xf0)//第二行有键被按下
{
temp = JPJK;//读被按下的键
baoj1();
switch(temp)
{
case 0xed: key = 4;break;
case 0xdd: key = 5;break;
case 0xbd: key = 6;break;
case 0x7d: key = 0x0b;break;
}
while(temp != 0xf0)//等候按键开释
{
temp = JPJK;
temp &= 0xf0;
}
}
}
JPJK = 0xfb; //将第 三行线置低电平
temp = JPJK;//读JPJK的当时状况到 temp
temp &= 0xf0;// temp = temp & 0xf0
if(temp != 0xf0)
{
delay_ms(10);//延时去抖
temp = JPJK;
temp &= 0xf0;
if(temp != 0xf0)//第三行有键被按下
{
temp = JPJK;//读被按下的键
baoj1();
switch(temp)
{
case 0xeb: key = 7;break;
case 0xdb: key = 8;break;
case 0xbb: key = 9;break;
case 0x7b: key = 0x0c;break;
}
while(temp != 0xf0)//等候按键开释
{
temp = JPJK;
temp &= 0xf0;
}
}
}
JPJK = 0xf7; //将第四行线置低电平
temp = JPJK;//读JPJK的当时状况到 temp
temp &= 0xf0;// temp = temp & 0xf0
if(temp != 0xf0)
{
delay_ms(10);//延时去抖
temp = JPJK;
temp &= 0xf0;
if(temp != 0xf0)//第四行有键被按下
{
temp = JPJK;//读被按下的键
baoj1();
switch(temp)
{
case 0xe7: key = 0;break;
case 0xd7: key = 0;break;
case 0xb7: key = 0x0f;break;
case 0x77: key = 0x0d;break;
}
while(temp != 0xf0)//等候按键开释
{
temp = JPJK;
temp &= 0xf0;
}
}
}
return key; //回来按下的键
}