There is a C51 project in this section, which uses Xinhualong's C51 F020 single chip microcomputer. The project aims to realize a 4*5 matrix keyboard. The matrix circuit diagram is as follows
Among them, the four column lines are connected to the P2~P5 port lines of F020, and the five row lines are connected to the P5 port line (the P5 port of F020 is different from the ordinary C51 expansion interface and cannot be bit-addressed). At the same time, the four column lines are connected to a four-input NAND gate (74LS20), and the gate output is connected to the external interrupt 1 of F020. In this way, no matter which key is pressed, an interrupt will be generated, and the signaling program will perform keyboard electronic scanning.
I asked a novice to write a keyboard electronic scanning program for me. The basic functions can be realized, but the keyboard de-shaking treatment is always not done well. It is manifested in that the de-shaking cannot be done, or the button response is too stuck, or the wrong key value is collected. It seems that the novice has a good grasp of the electronic scanning principle of the matrix keyboard (there are many materials on the Internet), but the knowledge of keyboard de-shaking is lacking. Basically, they delay for a period of time and then collect the key value according to the book. In actual applications, such treatment is far from enough and too simple. In fact, the de-shaking treatment should be more reasonable, that is, to continuously collect key values. When the collected key values are the same for a period of time, it is considered that the button state has been stable and this key value is the real key value. In addition, when the button is released, there will be jitter, which will lead to the wrong key value. Therefore, when the key is released, the de-shaking treatment should also be carried out. The treatment method is to collect the state of no key pressing for a period of time, and then think that the button is released. According to this method, I rewrote the novice program, and it performed very well in actual applications.
The program is now released as follows for reference by novices.
Key.h file contents
#ifndef __key_H__
#define __key_H__
#define stubble_KEY 0x0000
#define S1 0x3801
#define S2 0x3401
#define S3 0x3802
#define S4 0x3402
#define S5 0x3804
#define S6 0x3404
#define S7 0x3808
#define S8 0x3408
#define S9 0x3810
#define S10 0x3410
#define S11 0x2C01
#define S12 0x1C01
#define S13 0x2C02
#define S14 0x1C02
#define S15 0x2C04
#define S16 0x1C04
#define S17 0x2C08
#define S18 0x1C08
#define S19 0x2C10
#define S20 0x1C10
#define KEY_DELAY 20
extern unsigned int Key_Value;
extern void Init_Key();
extern void Scan_Key();
extern bit Key_Pressed;
extern bit Key_Released;
extern unsigned int idata Keypress_Count;
extern unsigned int idata Keyrelease_Count;
#endif
key.c file contents
#include
#include "key.h"
bit Key_Down; //Whether a key is pressed
unsigned int idata Keypress_Count;
sbit Col_Key0 = P2^2;
sbit Col_Key1 = P2^3;
sbit Col_Key2 = P2^4;
sbit Col_Key3 = P2^5;
bit Key_Pressed;
bit Key_Released;
unsigned int Key_Value; bit Key_Down; //Whether a key is pressed
unsigned int idata Keypress_Count; //Variable that increases once per millisecond
unsigned int idata Keyrelease_Count; //Variable that increases once per millisecond
//Matrix keyboard uses interrupt 1 as keyboard interrupt [page]
{
P5 = 0; //Set all lines to 0
EX1 = 1; // Allow external clock seconds to interrupt
IT1 = 1; // External clock interrupt configuration is edge triggered
}
void Key_Int() interrupt 2
{
Key_Pressed = 1;
EX1 = 0;
}
void Scan_Key()
{
unsigned char temp,rowvalue;
unsigned int key;
int i;
temp = P2;
temp &= 0x3C;
if(temp == 0x3C)
{
Key_Released = 0;
Key_Pressed = 0;
key = stubble_KEY;
EX1 = 1;
}
else
{
key = temp;
key = key<<8;
rowvalue = 0x01;
for(i=0;i<5;i )
{
P5 = rowvalue<
DelayMs⑴;
temp = P2;
temp &= 0x3C;
if(temp == 0x3c)
{
rowvalue = rowvalue<
key = key | rowvalue;
P5 = 0x00;
break;
}
}
P5 = 0x00;
DelayMs⑴;
}
if(key!=灭茬_KEY) //If a key is pressed
{ if(key==Key_Value) //If the same key is pressed
{
if(Keypress_Count>=KEY_DELAY)
{
Key_Down = 1;
}
}
else if(Key_Down != 1)
{
Keypress_C
Previous article:8253 interrupt service program flow chart
Next article:Design of Liquid Level Monitor Based on Multiple Single Chip Microcomputers
- Popular Resources
- Popular amplifiers
- Enter the Matrix MS-DOS screensaver.Sample of using bios functions (by int 10h) for text data out
- Multi-channel pressure measuring instrument based on C8051F020 single chip microcomputer
- Design of automatic player for work and rest signal based on 51 single chip microcomputer
- AVR243 Matrix Keyboard Decoder
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- [ESK32-360 Review] 6. Hello! Hello! Hello! Hello!
- [NXP Rapid IoT Review] NXP Rapid IOT Experience
- Is there any teacher who can help me write a program for Mitsubishi FX3U to output pulses?
- RTL8762, an excellent domestic BLE low-power solution
- Motor control and host computer connection
- Bear Pie Huawei IoT operating system LiteOS bare metal driver transplantation 01-Explain driver transplantation using LED as an example
- 【GD32450I-EVAL】Simple oscilloscope demonstration
- [GD32E231 DIY Contest] 05. Automatic Fish Feeding Robot - PWM
- Three special PCB routing techniques: right-angle routing, differential routing, and serpentine routing
- 【Ended】 R&S Live 【PCI Express Gen 3 Conformance Test】