/********************************************Keyboard_No timer_No delay Features: The key is effective after releasing the hand, high sensitivity, low resource consumption, high operating efficiency Independent keyboard: K01=P2^4; K02=P2^5; K03=P2^6; K04=P2^7; Matrix keyboard: Row (up to down)_P2.3_P2.2_P2.
/****************************************
Keyboard_No timer_No delay
Features:
The button is effective after releasing the hand, with high sensitivity, low resource consumption and high operating efficiency.
Independent keyboard: K01=P2^4;K02=P2^5;K03=P2^6;K04=P2^7;
The matrix keyboard is: row (up to down)_P2.3_P2.2_P2.1_P2.0
Column (left to right)_P2.7_P2.6_P2.5_P2.4
The operation functions provided are:
//Independent keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key
extern unsigned char keyboard_self();
//Matrix keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key**** to detect the high four bits
extern unsigned char keyboard_matrix();
****************************************/
.
Let’s look at the independent keyboard first (the algorithm is the same as the matrix keyboard)
-----------------------------------------------------------------------
#include
#include
//Independent keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key
extern unsigned char keyboard_self()
{
unsigned char num_key=0;//key number
unsigned char temp=0; //Used to read the key value on P2 line
static unsigned char temp_code=0;//Save key value
static unsigned char num_check=0; //Low level valid times
static unsigned char key_flag=0; //Key valid flag
temp=P2&0xF0; //Read P2 line data
if (temp!=0xF0) //Low level judgment
{
num_check++;
if (num_check == 10) // if the low level is valid for 10 consecutive times (10ms), the button is considered valid
{
key_flag=1; //Enable key valid flag
temp_code=temp; //Save key value
}
}
else//Judge when releasing the hand
{
num_check=0;
if (key_flag == 1) //button is valid
{
key_flag=0;
switch (temp_code) // read the key number
{
case 0xE0: num_key=1;
break;
case 0xD0: num_key=2;
break;
case 0xB0: num_key=3;
break;
case 0x70: num_key=4;
break;
}
}
}
return (num_key);
}
Now it's a matrix keyboard
-----------------------------------------------------------------------
#include
#include
//Matrix keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key**** to detect the high four bits
extern unsigned char keyboard_matrix()
{
unsigned char num_key=0;//key number
unsigned char temp=0; //Read P2 port line data
status unsigned char temp_code=0; //Used to save key value
status unsigned char temp_circle=0xFE; //Save the circular scan value on the P2 line
static unsigned char num_check=0;//low level count
static unsigned char key_flag=0;//Key valid flag
P2=temp_circle; //0xFX
temp=P2; //Read P2 port line data
if (temp!=temp_circle) // there is a key action
{
num_check++; //Low level count | add 1 when the level is low
if (num_check == 10) //Low level is valid for 10 consecutive times (10ms)
{
key_flag=1; //Key valid flag set to 1
temp_code=temp; //Save key value
}
}
else //Release the hand or no key action, the scan line should be changed at this time
{
num_check=0;
if (key_flag == 1) //Key valid judgment
{
key_flag=0;
switch (temp_code) // read the key number
{
//P2^0 line
case 0xEE: num_key=1;
break;
case 0xDE: num_key=2;
break;
case 0xBE: num_key=3;
break;
case 0x7E: num_key=4;
break;
//P2^1 line
case 0xED: num_key=5;
break;
case 0xDD: num_key=6;
break;
case 0xBD: num_key=7;
break;
case 0x7D: num_key=8;
break;
//P2^2 line
case 0xEB: num_key=9;
break;
case 0xDB: num_key=10;
break;
case 0xBB: num_key=11;
break;
case 0x7B: num_key=12;
break;
//P2^3 line
case 0xE7: num_key=13;
break;
case 0xD7: num_key=14;
break;
case 0xB7: num_key=15;
break;
case 0x77: num_key=16;
break;
}
}
temp_circle=_crol_(temp_circle,1);//Change the scan line
if (temp_circle == 0xEF)
{
temp_circle=0xFE;
}
}
return (num_key); //Return key number
}
/****************************************************************************
When no key is pressed, the scan line keeps changing.
When a key is pressed for a long time, the scan line does not change, so that the keys in this row become independent keys, which makes the scanning efficiency extremely high.
For example, when a key on the P2.0 line is pressed, the program will scan this key, and then the scan line will not change.
When the keyboard program is entered 10 times in succession, it detects that 10 key presses are valid, and the scan line does not change until the key is released.
*****************************************************************************/
Previous article:How to use 51 single-chip digital tube to realize the function of marquee?
Next article:A brief analysis of the four ways to use the 51 MCU IO port
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Free application: Anxinke UWB indoor positioning module NodeMCU-BU01
- Analysis of weak optical signal amplification circuit, help needed! ! ! ! ! ! !
- Can someone show me what power module (SPM) this is?
- 39 "Wanli" Raspberry Pi car - ROS learning (ROS project file system structure introduction and import external software packages...
- Have you used digital RMB?
- RTT launches new BSP for stm32 series, and shares the production tutorial with you
- 【Beetle ESP32-C3】Download MicroPython firmware
- [Analog Electronics Elective Test] + Amplifier Bandwidth
- Review summary: Mir Allwinner automotive-grade CPU development board MYC-YT507
- EEWORLD University - Buck Converter and Controller Troubleshooting