principle:
16 keyboards can be controlled through eight pins. The upper four bits control the rows and the lower four bits control the columns.
Code:
#include #define SMG P0 //Macro definition digital tube #define KEY P1 //Macro definition matrix keyboard typedef unsigned char u8; typedef unsigned int u16; u8 sum; //Define a global variable to save the value of the key u8 SMG_Code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0xff}; //The content displayed by the digital tube void delay(u16 i) { while(i--); } void KEY_J(void) //Matrix keyboard function { u8 a=0; //A local variable is defined KEY=0x0f; //When I define 0x0f, it means I start column scanning, from high to low if(KEY!=0x0f) //KEY=0x0f was defined before. If there is a change, it should be that a button has been pressed. { delay(1000);//normal debounce if(KEY!=0x0f) { switch(KEY)//This is interesting, because here we need to determine which column of keys is pressed. One side of the key is low level. After the connection, the high level will be pulled low, so the pin will become low level. Find the corresponding column and you will know which column it is. { case 0x07:sum=1;break; case 0x0b:sum=2;break; case 0x0d:sum=3;break; case 0x0e:sum=4;break; } KEY=0xf0; //Column scanning is completed, now start row scanning switch(KEY)//In order, the pressed pin will turn to low level to determine which row is pressed. { case 0x70:sum=sum;break; //The number obtained in the first row is the column case 0xb0:sum=sum+4;break;//The number obtained in the second row is the column plus 4, and each subsequent row adds 4 to the number. case 0xd0:sum=sum+8;break; case 0xe0:sum=sum+12;break; } } } //It's okay not to add it, but it will be more stable if it is added. while((a<50)&&(KEY!=0xf0)) //Detect the key release detection, which means that when you keep pressing this button, the next execution will be performed after 0.05 seconds, or the following will be executed when you release it. { delay(100); a++; } } void main(void) { while(1) { KEY_J(); //Call matrix keyboard function SMG=SMG_Code[sum]; //Nigital tube display } } operation result: Click a corresponding one to display it.
Previous article:51 Microcontroller Basics: Timer Interrupt (1)
Next article:51 Microcontroller Basics External Interrupt (2)
Recommended ReadingLatest update time:2024-11-15 14:39
- Popular Resources
- Popular amplifiers
- Siemens S7-1200-PLC Programming and Application Tutorial (3rd Edition) (Edited by Shi Shouyong)
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Learning PLC is easy - PLC ladder diagram and statement table with pictures (Li Changjun, Zhou Hua)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- 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
- 【RT-Thread Reading Notes】(0) Some basic concepts and understanding of threads
- [GD32E503 Evaluation] Realizing USB disk function based on uCOSIII project 1
- Zero drift phenomenon and its causes
- RISC-V open source hardware board — HiFive Unleashed
- How to choose a replacement battery for the Dyson V6 vacuum cleaner?
- CY8CKIT-149 PSoC 4100S PLUS PROTOTYPING KIT Unboxing and Power-On Run Example
- B-U585I-IOT02A Bluetooth communication problem
- Super detailed teaching you how to use HFSS to design and simulate inverted F antenna 2
- How does an intelligent fully automatic multimeter measure capacitance?
- PCB current and signal integrity design