C51 MCU matrix keyboard scanning debounce program

Publisher:神雕Latest update time:2011-10-27 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Matrix circuit diagram

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]

void Init_Key()

{

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

Reference address:C51 MCU matrix keyboard scanning debounce program

Previous article:8253 interrupt service program flow chart
Next article:Design of Liquid Level Monitor Based on Multiple Single Chip Microcomputers

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号