Use the microcontroller P1 port to expand into a 4*4 keyboard. Press the key to light up the LED of the P0 port.
The expansion of the matrix keyboard is not complicated, but the detection code part is complicated.
IO expansion principle
A matrix keyboard, also known as a row-column keyboard, is a keyboard that uses four I/O lines as row lines and four I/O lines as column lines.
A key is set at each intersection of a row line and a column line, so that the number of keys on the keyboard is 4*4.
This matrix keyboard structure can effectively improve the utilization rate of I/O ports in the single-chip computer system.
Detection principle
When no key is closed, the row line and column line are open. When a key is closed, the two I/O port lines connected to the closed key are short-circuited.
How to determine whether a key is pressed:
Coarse scan. The row line outputs low level, and the column line outputs high level. Read the Px port, if the data is not equal to 0x0F, a key is pressed.
Fine scan. The row lines output low level row by row, and other IO ports output high level. Read the Px port, if the data is not equal to the output value, a key is pressed. And the number of rows can be determined. Similarly, the column lines output low level row by row, and the number of columns can be detected.
Convert key codes according to row and column numbers
code show as below:
#include#define uchar unsigned char #define LED_ON(X) P0 = ~(1<<(X)) //LED turns on at low level uchar KeyScan(void); void main(void) { uchar key; while(1){ key = KeyScan(); if(key > 0) { key = key -1; LED_ON(key); } } } uchar KeyScan(void) { uchar i,val,row,col,key; P1 = 0xF0; //Row line set to 0, column line set to 1 if( P1 != 0x0F0 ) //button pressed { for(i=0;i<4;i++) //row scan { val = 0xFF&(~(1<
Previous article:C51 MCU LED Example Program Set
Next article:LCD1602 liquid crystal display Chinese characters
Recommended ReadingLatest update time:2024-11-23 19:03
- Popular Resources
- Popular amplifiers
- Microgrid Stability Analysis and Control Microgrid Modeling Stability Analysis and Control to Improve Power Distribution and Power Flow Control (
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- MATLAB and FPGA implementation of wireless communication
- Introduction to Internet of Things Engineering 2nd Edition (Gongyi Wu)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Chinese IC design companies' revenues to grow 44% this year
- SIC4200 Analog Multiplier
- Are only foreign companies exempt from overtime work?
- Design of Distributed Body Control System
- 06 Make GD32L233C expansion board
- The names, wavelengths, characteristics and application fields of each band in the electromagnetic spectrum
- [TI Course] How is the anti-interference ability of TI millimeter-wave radar?
- The ampere-second product of capacitance and the volt-second product of inductance
- Regarding the distinction between NMOS tubes and PMOS tubes
- Pins 2 and 4 are missing from the schematic and package of the BNX016-01 device