This circuit diagram is equivalent to a coordinate diagram. Each key has a unique coordinate to determine which key is pressed.
Method 1: Line-by-line scanning: The matrix keyboard is scanned line by line by outputting low level in turn through the upper four bits. When the data received by the lower four bits are not all 1, it means that a key is pressed. Then, the key that is pressed is determined by which bit of the received data is 0.
Method 2: Row and column scanning: output low level through all high four bits and high level through low four bits. When the received data, the low four bits are not all high level, it means that a key is pressed, and then judge which column has a key pressed by the received data value, and then conversely, the high four bits output high level and the low four bits output low level, and then judge which row has a key pressed according to the received high four bits value, so that it can be determined which key is pressed.
#include "reg52.h"
typedef unsigned int u16;
typedef unsigned char u8;
#define GPIO_DIG P0
#define GPIO_KEY P1
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 KeyValue;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //Display the value of 0~F
void delay(u16 i)
{
while(i--);
}
void KeyDown(void)
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f) //Read whether the key is pressed
{
delay(1000); //Delay 10ms to eliminate jitter
if(GPIO_KEY!=0x0f) //Check if the keyboard is pressed again
{
//Test column
GPIO_KEY=0x0f;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//Test line
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //Detect button release detection
{
delay(1000);
a++;
}
}
}
}
void main()
{
LSA=0; //Provide a bit selection for a digital tube
LSB=0;
LSC=0;
while(1)
{
KeyDown(); //Key judgment function
GPIO_DIG=smgduan[KeyValue]; //
}
}
Previous article:Introduction to 51 MCU - Digital tube dynamic display experiment
Next article:MCU Interrupts - Technical Summary
- Popular Resources
- Popular amplifiers
- 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?
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- Design of automotive LIN communication simulator based on Renesas MCU
- When will solid-state batteries become popular?
- Adding solid-state batteries, CATL wants to continue to be the "King of Ning"
- The agency predicts that my country's public electric vehicle charging piles will reach 3.6 million this year, accounting for nearly 70% of the world
- U.S. senators urge NHTSA to issue new vehicle safety rules
- Giants step up investment, accelerating the application of solid-state batteries
- Guangzhou Auto Show: End-to-end competition accelerates, autonomous driving fully impacts luxury...
- Lotus launches ultra-900V hybrid technology "Luyao" to accelerate the "Win26" plan
- Introduction to RS-485 interface chip and related issues in its application
- Who has a domestic 20pin OTP microcontroller with ADC, and it must be cheap?
- [New Driving Force Technology MM32F031C6 Development Board Review] ADC & Timer Interrupt & Independent Watchdog
- Circuit Design of Special Monolithic Switching Power Supply Module
- 【Reward Sharing】Experience MPS Online Design Tools Together
- 【micropython】Bluetooth module has been added to ESP32
- Weekly evaluation information, delivered on time
- 97% off e-coins (can join in group)
- IBM Power Architecture adds new innovation momentum, IBM microprocessor collaboration platform launched in China
- Wireless Sensor Networks Explained