单片机型号说明:IAP15F2K61S2
新建工程时单片机型号选择STC15F2K60S2
51单片机矩阵键盘代码
void Delay_ms(int xms) //15单片机延时函数
{
int i;
long a;
for (i = xms; i > 0; i --)
for(a = 810; a > 0; a --);
}
unsigned char KeyJZ_16(void)
{
unsigned char KeyNumber;
unsigned char temp;
//第一列
P3 = 0x7F; //P37 = 0,其他是1,检测第一列
temp = P3; //按键检测,输入
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20); //延时20ms来确认是否真正按下按键
temp = P3;
temp &= 0x0F;//检测是第一列哪个按键
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0x7E : KeyNumber = 7; break;
case 0x7D : KeyNumber = 6; break;
case 0x7B : KeyNumber = 5; break;
case 0x77 : KeyNumber = 4; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
//第二列
P3 = 0xBF;
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20);
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0xBE : KeyNumber = 11; break;
case 0xBD : KeyNumber = 10; break;
case 0xBB : KeyNumber = 9; break;
case 0xB7 : KeyNumber = 8; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
//第三列
P3 = 0xDF;
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20);
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0xDE : KeyNumber = 15; break;
case 0xDD : KeyNumber = 14; break;
case 0xDB : KeyNumber = 13; break;
case 0xD7 : KeyNumber = 12; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
//第四列
P3 = 0xEF;
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20);
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0xEE : KeyNumber = 19; break;
case 0xED : KeyNumber = 18; break;
case 0xEB : KeyNumber = 17; break;
case 0xE7 : KeyNumber = 16; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
return KeyNumber;
}
本程序烧写到51单片机应该是正常使用的,手头没有51开发板了,暂时没测试
这个程序烧写到15单片机,前两列按键不能正常使用
只需要在判断第一列按键那部分程序,前面加上P42 = 0;
在判断第二列按键那部分程序,前面加上P44 = 0;
因为这两列接线是P42和P44
矩阵按键部分代码(新写法)
void Delay_ms(int xms) //15单片机延时函数
{
int i;
long a;
for (i = xms; i > 0; i --)
for(a = 810; a > 0; a --);
}
unsigned char KeyJZ_16(void)
{
unsigned char KeyNumber = 0;
unsigned char temp;
//第一列
P3 = 0x7F; //P37 = 0,其他是1,检测第一列
P4 = 0xEF; //P44 = 0; P42 = 1; //15单片机第一列占用的引脚
temp = P3; //按键检测,输入
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20); //延时20ms来确认是否真正按下按键
temp = P3;
temp &= 0x0F;//检测是第一列哪个按键
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0x7E : KeyNumber = 7; break;
case 0x7D : KeyNumber = 6; break;
case 0x7B : KeyNumber = 5; break;
case 0x77 : KeyNumber = 4; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
//第二列
P3 = 0xBF;
P4 = 0xFB; //P42 = 0; P44 = 1; //15单片机第二列占用的引脚
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20);
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0xBE : KeyNumber = 11; break;
case 0xBD : KeyNumber = 10; break;
case 0xBB : KeyNumber = 9; break;
case 0xB7 : KeyNumber = 8; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
//第三列
P3 = 0xDF;
P4 = 0xFF;
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20);
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0xDE : KeyNumber = 15; break;
case 0xDD : KeyNumber = 14; break;
case 0xDB : KeyNumber = 13; break;
case 0xD7 : KeyNumber = 12; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
//第四列
P3 = 0xEF;
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
Delay_ms(20);
temp = P3;
temp &= 0x0F;
if(temp != 0x0F)
{
temp = P3;
switch (temp)
{
case 0xEE : KeyNumber = 19; break;
case 0xED : KeyNumber = 18; break;
case 0xEB : KeyNumber = 17; break;
case 0xE7 : KeyNumber = 16; break;
}
while(temp != 0x0f)
{
temp = P3;
temp &= 0x0F; //因为这条,所以松手检测才写成 temp != 0x0f
}
}
}
return KeyNumber;
}
测试键盘是否好使文章来源:https://www.toymoban.com/news/detail-449516.html
#include <STC15F2K60S2.H>
unsigned char KeyNum;
void main()
{
P2 = 0xA0; P0 = 0x00; P2 = 0x80; P0 = 0xFF; //初始化
while(1)
{
KeyNum = KeyJZ_16();
if(KeyNum != 0)
{
P00 = !P00; //每次按键按下P00状态都会取反
}
}
}
一定要加初始化呀,这次忘了,按键根本不好使文章来源地址https://www.toymoban.com/news/detail-449516.html
到了这里,关于【STC15单片机】简单的矩阵键盘代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!