2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
提示
A button is a button or switch used to control the connection and disconnection of an electronic device or circuit. They usually have two states: on and off. Here are some common switch buttons:
Some application scenarios of mechanical buttons:
The key circuit schematic on the development board is shown below. J11 is used to control whether the 0/4/8/ buttons are in the matrix keyboard or independent keys. The 16 keys are controlled by a set of P3X ports.
The actual buttons of the development board are shown in the figure
First, we implement independent buttons. Use a jumper cap on J11 to short-circuit pins 2 and 3. This way, we can use buttons 0/4/8/C independently.
1. Software engineering, we create two new files, c51_key.c and c51_key.h, apply includes.h in c51_key.c, include c51_key.h in includes.h, and add the two files to the project as shown in the figure.
Because we need to display the key value, we use the digital tube in front to display the data.
The display function is shown below.
/********************************************************
函数名称:sys_keynum_ledon
函数功能:显示按键数值
入口参数:按键数值
出口参数:
修 改:
内 容:
********************************************************/
void sys_keynum_ledon(unsigned char num)
{
//根据原理图,将P0口全部输出高电平,P2选择0号数码管
P0=EL[num];//取显示数据,段码
P2=0; //取位码
}
In c51_key.c, key detection is implemented
bit key1=0; //定义按键位置
bit key2=0;
bit key3=0;
bit key4=0;
/********************************************************
函数名称:sys_key_single
函数功能:按键检测
入口参数:
出口参数:按键数值
修 改:
内 容:
********************************************************/
static unsigned char key1_history = 0;//缓存上一次按键的结果
unsigned char sys_key_single(void)
{
key1=P30; //定义按键位置
key2=P31;
key3=P32;
key4=P33;
if(!key1) //按下相应的按键,数码管显示相应的码值
{
key1_history = 1;
return 1;
}
else if(!key2)
{
key1_history = 2;
return 2;
}
else if(!key3)
{
key1_history = 3;
return 3;
}
else if(!key4)
{
key1_history = 4;
return 4;
}
else
{
return key1_history;
}
}
Referenced in the main program:
#include "includes.h"
/*------------------------------------------------
延时子程序
------------------------------------------------*/
void delay(unsigned int cnt)
{
while(--cnt);
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main (void)
{
//8个指示灯的操作
sys_led();
sys_led_test();
sys_led_test1();
sys_ledtube_on1();
sys_ledtube_on2();
//主循环中添加其他需要一直工作的程序
while (1)
{
sys_keynum_ledon(sys_key_single());
}
}
/********************************************************
函数名称:sys_key_single
函数功能:按键检测,带有消抖策略
入口参数:
出口参数:按键数值
修 改:
内 容:
********************************************************/
static unsigned char key1_history = 0;//缓存上一次按键的结果
unsigned char sys_key_single(void)
{
key1=P30; //定义按键位置
key2=P31;
key3=P32;
key4=P33;
if(!key1) //按下相应的按键,数码管显示相应的码值
{
delay(1000);
if(!key1)
{
key1_history = 1;
return 1;
}
else
{
return key1_history;
}
}
else if(!key2)
{
delay(1000);
if(!key2)
{
key1_history = 2;
return 2;
}
else
{
return key1_history;
}
}
else if(!key3)
{
delay(1000);
if(!key3)
{
key1_history = 3;
return 3;
}
else
{
return key1_history;
}
}
else if(!key4)
{
delay(1000);
if(!key4)
{
key1_history = 4;
return 4;
}
else
{
return key1_history;
}
}
else
{
return key1_history;
}
}
// Display segment code value 01234567, you can view the pin high level configuration status corresponding to different graphics in the schematic diagram
unsigned char const EL[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F
unsigned char sys_key_board(void)
{
unsigned char key = 0x00;
unsigned char num = 0x00;
key=keyscan(); //调用键盘扫描
if(key == 0xFF)
{
num = key1_history;
}
else
{
switch(key)
{
case 0xee:num = 0x0;break;//0按下相应的键显示相对应的码值
case 0xde:num = 0x1;break;//1 按下相应的键显示相对应的码值
case 0xbe:num = 0x2;break;//2
case 0x7e:num = 0x3;break;//3
case 0xed:num = 0x4;break;//4
case 0xdd:num = 0x5;break;//5
case 0xbd:num = 0x6;break;//6
case 0x7d:num = 0x7;break;//7
case 0xeb:num = 0x8;break;//8
case 0xdb:num = 0x9;break;//9
case 0xbb:num = 0xA;break;//a
case 0x7b:num = 0xB;break;//b
case 0xe7:num = 0xC;break;//c
case 0xd7:num = 0xD;break;//d
case 0xb7:num = 0xE;break;//e
case 0x77:num = 0xF;break;//f
default:num = key1_history; break;
}
key1_history = num;
}
return num;
}
/*------------------------------------------------
键盘扫描程序
------------------------------------------------*/
unsigned char keyscan(void) //键盘扫描函数,使用行列反转扫描法
{
unsigned char cord_h,cord_l;//行列值中间变量
P3=0x0f; //行线输出全为0
cord_h=P3&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
delay(100); //去抖
if(cord_h!=0x0f)
{
cord_h=P3&0x0f; //读入列线值
P3=cord_h|0xf0; //输出当前列线值
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值
}
}
return(0xff); //返回该值
}
Independent button to display the result, DIP switch, connect one end of the button to ground
Matrix keyboard key simulation:
In microcontroller applications, the reliability design of key detection is critical to ensure correct input and stable system operation. The following are some design principles to improve the reliability of key detection: