C51 single chip matrix keyboard electronic scanning debounce program

There is a C51 project in this section, which uses Xinhualong's C51 F020 microcontroller. In the project, make a matrix keyboard that becomes a fact 4*5. The matrix circuit diagram is as shown

Matrix circuit diagram

Among them, four column lines are connected to the P2~P5 port line of F020, and five line lines are connected to the P5 port line (the P5 port of F020 is different from the ordinary C51 expansion interface, which can not be addressed). At the same time, the four column lines are connected to a four-input NAND gate (74LS20), and the door output is connected to the external interrupt 1 of F020. In this case, no matter what one button is pressed, a discontinuity will occur, and the signaling program will hold a keyboard electronic scan.

For a newbie to write the electronic scanning program of the keyboard, the basic functions can make it a reality, but the penalty for the debounce of the keyboard is always poor, the expression is or can not shake, or the button is too card, Or collect the 纰缪 key value. It seems that the novice masters the electronic keyboard scanning principle of the matrix keyboard (more online information), but the knowledge of the keyboard debounce is lacking. Basically, according to the delay time in the book, the key value is collected again. In the real application. Such a punishment is far from enough, too simple. Reality debounce should be more reasonable, that is, continuous collection of key values, when the collected key value is not different for a period of time, that is, the button status has been stabilized, this key value is the real key value. In addition, when the button is released, there will be jitter, which will lead to the mis-collection of the key value. When the key is released, the de-shake should also be set. The method of punishment is also the continuous selection of the key-free state for a period of time. Thought that the button was released. In accordance with this method, I rewrote the novice program, which is extremely good in real-life applications.

The program is now released for reference by novices.

Key.h file content

#ifndef __key_H__

#define __key_H__

#define 灭茬_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 content

#include

#include "key.h"

Bit Key_Down; //Is there a mark with a key 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; // Is there a tag with a key pressed?

Unsigned int idata Keypress_Count; // variable that is incremented by one millisecond

Unsigned int idata Keyrelease_Count; // variable that is incremented by one millisecond

/ / Matrix keyboard application interrupt 1 as a keyboard interrupt

To suit the demands of customers as a supplier, wholesaler and trader, we are engaged in offering Battery. Enclosed in polyester tube to stop spillage, these batteries are manufactured with perfection at our vendor's premises. Widely demanded across the nationwide market, these batteries are suitable to use in UPS and inverters.

Battery

Solar Battery,Lead Acid Battery,Rechargeable Battery,Marine Gel Battery

Delight Eco Energy Supplies Co., Ltd. , https://www.cndelight.com

Posted on