Technology Sharing

51 MCU embedded development: 5. Button, matrix button operation and protues simulation

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


提示

1 Button Introduction

1.1 Button Types

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:

  1. Push Button Switch: This is a simple button that connects a circuit when pressed and disconnects it when released. They are often used to turn devices on and off or to trigger specific functions.
  2. Toggle Switch: A toggle switch is a switch with fixed positions that can be manually switched. They usually have two or more fixed positions (such as on and off), and the circuit is connected or disconnected by switching the position of the switch.
  3. Slide Switch: A slide switch is a switch that changes state by sliding the button. They usually have two positions and connect or disconnect the circuit by sliding the slider from one position to the other.
  4. Toggle Switch: A toggle switch is a switch that changes state by flipping a button. They usually have a middle position and two extreme positions, and the circuit is connected or disconnected by flipping the button from one position to another.
  5. Button Switch: A push button switch is similar to a push button switch, but usually has a spring mechanism that automatically returns to its original position when the button is released. They are often used in applications where pressing and holding the button maintains a connection, while releasing the button breaks the circuit.
  6. Electronic Switch: An electronic switch is a switch that uses electronic components (such as transistors or relays) to control the connection and disconnection of a circuit. They can control the switch state through electrical signals or other triggering methods.

1.2 Button Application Scenarios

Some application scenarios of mechanical buttons:

  1. Computer keyboard: Mechanical keyboards are widely used in the computer field. Because mechanical keys provide better touch and key travel, typing and gaming experience are more comfortable and precise.
  2. Gaming equipment: Mechanical buttons are widely used in game consoles, game controllers, and gaming mechanical keyboards. The tactile feedback and fast response time of mechanical buttons help improve the control performance of games.
  3. Industrial control equipment: Mechanical buttons are highly durable and suitable for scenarios in industrial control equipment that require frequent operations, such as mechanical control panels, robot consoles, etc.
  4. Communication equipment: Mechanical buttons can be used in communication equipment such as mobile phones, telephones, and walkie-talkies to provide better button feel and reliability.
  5. Audio equipment: Mechanical buttons are widely used in audio equipment, such as mixing consoles, audio control panels, music keyboards, etc.
  6. Medical equipment: Mechanical buttons are commonly used in medical equipment, such as medical instruments, operating table control panels, etc. The stability and reliability of mechanical buttons are crucial to the normal operation of medical equipment.
  7. Automobile and aviation equipment: Mechanical buttons can be used in scenarios such as car control panels and aircraft dashboards that require high reliability and durability.
    The durability, tactile feel, and reliability of mechanical keys make them the preferred choice in many fields. Whether it is to improve work efficiency or to provide a better user experience, mechanical keys are widely used in many application scenarios.

2 Button Circuit

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.

insert image description here

The actual buttons of the development board are shown in the figure
insert image description here

insert image description here

3-button software design

3.1 Button Implementation

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.

insert image description here

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;  	//取位码
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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;
	}
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

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());
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

3.2 Key filtering method

/********************************************************
函数名称: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;
	}
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

3.3 Matrix Button Software Design

// 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
  • 1
  • 2
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);     //返回该值
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

3.4 Key Protues Simulation

Independent button to display the result, DIP switch, connect one end of the button to ground

insert image description here

Matrix keyboard key simulation:insert image description here

4 Key operation summary

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:

  1. Eliminate key jitter: Key jitter refers to the short, unstable signal generated when a key is pressed or released. To eliminate jitter, you can use hardware or software jitter elimination methods. In hardware, you can use external capacitors or RC networks to filter out jitter. In software, you can use software delays or state machines to stabilize key signals.
  2. Use external pull-up/pull-down resistors: By using external pull-up or pull-down resistors connected to the button pins, you can ensure that the button remains stable when not pressed. Doing so prevents floating signals when the button is not pressed.
  3. Delay and debounce algorithms: Implementing appropriate delay and debounce algorithms in software can improve the reliability of key detection. Delay can ensure a stable key state and avoid false triggering. Debounce algorithms can filter out key jitter to ensure that only valid key operations are detected.
  4. Multiple detection and confirmation: To improve reliability, multiple key detection and confirmation mechanisms can be used. For example, a software timer or interrupt can be used to periodically detect the key status and multiple confirmations can be used to ensure that the key is effectively triggered.
  5. Input buffer and state machine: Use the input buffer to store key input, and then use the state machine to process the input. The state machine can track the key state and process key events to ensure the correctness and stability of key operations.
  6. Interference isolation of power supply and ground line: In order to prevent the influence of power supply interference and ground line return current on key signals, appropriate isolation measures can be taken. For example, use independent power supply and ground line planes, or use filters to reduce interference.
  7. Fault-tolerance mechanisms for hardware and software: Designing fault-tolerance mechanisms can protect the reliability of key input when errors or abnormal conditions occur. For example, redundancy detection, checksum detection, error handling, and recovery mechanisms can be used.
    In general, reliable key detection design requires comprehensive consideration of various factors in hardware and software, and appropriate optimization and adjustment according to specific application scenarios. Through reasonable design and implementation, the stability, accuracy and reliability of the key can be ensured.