A new type of key scanning program

Source: Internet
Author: User

However, I have been on the internet for a long time, have seen a lot of source program, did not find the trace of the key handling methods, so I will share him out, and the vast number of colleagues to encourage each other. I strongly believe that this key handling method is convenient and efficient, you can migrate to any kind of embedded processor above, because C language powerful portability.

At the same time, the use of a number of layered ideas, in the microcontroller is also quite useful, but also the focus of this article.

For old birds, I suggest to look directly at those two expressions, and then think about it will understand, and do not need to listen to my back to boast, I do not swim mean, hoho~~ but for beginners, I suggest to read the full text. Because this is the actual project summed up in the experience, the school is not learning things.

The following assumptions you understand C, because the pure C language description, so and processor platform independent, you can in Mcs-51,avr,pic, even the arm platform to test the performance of this program. Of course, I also used in a number of projects, the effect is very good.

Well, the engineer's habits, nonsense should be less, start it. I use the AVR MEGA8 as a platform to explain, there is no other reason, because I am on hand only the AVR board is not 51. With 51 can also, only the chip initialization part is different, but also the register name is different.

Core algorithms:
unsigned char Trg;
unsigned char Cont;
void Keyread (void)
{
unsigned char readdata = pinb^0xff; 1
Trg = readdata & (readdata ^ Cont); 2
Cont = ReadData; 3
}

Finished. Is there an incredible feeling? Of course, do not want to understand the previous will be like that, to understand after you will marvel at the sophisticated algorithm!!

Here's what the program explains:
Trg (Triger) represents the trigger, Cont (continue) represents a continuous press.
1: Read the PORTB port data, take the inverse, and then send to the readdata temporary variable inside to save it.
2: Algorithm 1, used to calculate the trigger variable. A bit and operation, an XOR or operation, I want to learn C language should understand it? TRG is a global variable and can be referenced directly by other programs.
3: Algorithm 2, used to calculate continuous variables.

See here, there is a kind of "know it, do not know its why" feeling it? The code is simple, but how exactly does it accomplish our purpose? OK, let's go around the clouds and see the sky.

Our most commonly used key connection method is as follows: AVR has internal pull-up function, but in order to illustrate the problem, I deliberately use the external pull-up resistor. Then, when the key is not pressed, the read port data is 1, if the key press, then the port read to 0. Here's a look at the specifics of what this algorithm is all about.
(1) When there is no button
The port is 0xff,readdata read port and reverse, obviously, is 0x00.
Trg = readdata & (readdata ^ Cont); (In the initial state, Cont is also 0) very simple mathematical calculation, because ReadData is 0, then it and any number "phase", the result is also 0.
Cont = ReadData; Save Cont is actually equal to ReadData, for 0;

The result is:

ReadData = 0;
TRG = 0;
Cont = 0;

(2) The first time PB0 pressed the situation
The port data is 0xfe,readdata read port and reverse, obviously, is 0x01.
Trg = readdata & (readdata ^ Cont); Because this is the first time it is pressed, cont is the last value and should be 0. Then the value of this equation is not difficult to calculate, that is Trg = 0x01 & (0x01^0x00) = 0x01
Cont = ReadData = 0x01;

The result is:
ReadData = 0x01;
Trg = 0X01;TRG Only at this time the value of the corresponding bit is 1, the other time is 0
Cont = 0x01;

(3) PB0 pressing (long button)
The port data is 0xfe,readdata read port and the inverse is 0x01.
Trg = readdata & (readdata ^ Cont); Because this is a continuous press, the Cont is the last value and should be 0x01. Then the formula becomes TRG = 0x01 & (0x01^0x01) = 0x00
Cont = ReadData = 0x01;

The result is:
ReadData = 0x01;
Trg = 0x00;
Cont = 0x01;

Because now the key is long press, so the MCU will be a certain time (around 20ms) constantly execute this function, then the next time the implementation of the situation will be what?
ReadData = 0x01; This will not change, because the keys are not released.
Trg = readdata & (readdata ^ Cont) = 0x01 & (0x01 ^ 0x01) = 0, as long as the key is not released, this TRG value is always 0!!!
Cont = 0x01; This value is always 0x01!! if the button is not released.

(4) Press the button to loosen the situation
The port data is 0xff,readdata read port and the inverse is 0x00.
Trg = readdata & (readdata ^ Cont) = 0x00 & (0x00^0x01) = 0x00
Cont = ReadData = 0x00;

The result is:
ReadData = 0x00;
Trg = 0x00;
Cont = 0x00;

Obviously, this goes back to the initial state, that is, there is no keystroke pressed.

Sum up, do not know to understand it? In fact, it is simple, the answer is as follows:
Trg is the meaning of the trigger, that is, the jump, as long as there is a button pressed (level from 1 to 0 of the jump), then Trg in the corresponding key bit above will be placed one, we use the PB0 is the value of Trg 0x01, similar, if we PB7 press, TRG value should be 0x80, This is a good understanding, and, most crucially, the value of the TRG is only one occurrence at a time, and is immediately cleared, without the need for manual intervention. So the key function processing program will not be repeated execution, saving a lot of conditions to judge, this is the essence of Oh!! Cont represents the Long button, if the PB0 is pressed, then the value of cont is 0x01, relative to, PB7 press, then cont value should be 0x80, the same good understanding.

If you still do not understand the words, you can calculate the two expressions, it should not be difficult to understand.

Because of this support, the key processing becomes very good, the following look at the application:

Application one: Key processing of one trigger
Suppose PB0 is the buzzer button, click and the buzzer beep the sound. This is simple, but how did everyone do it before? Compare Who's convenience?
#define KEY_BEEP 0x01
void Keyproc (void)
{
if (Trg & key_beep)//If the key_beep is pressed
{
Beep (); Performing buzzer processing functions
}
}

What do you think? Is it harmonious enough? Remember the previous explanation of TRG's essence is what? The essence is that it only happens once. So you press the button, Trg & Key_beep for the "real" situation will only appear once, so it is very convenient to deal with, the buzzer will not be nothing to shout, hoho~~~

Or you would think that this deal is simple, no problem, we continue.

Application 2: Processing of long keys
Projects often encounter a number of requirements, such as: a button if a short click on the execution function A, if the long press 2 seconds will be performed function B, or is required 3 seconds to press on, the Count plus what what the function, very practical. Do not know how people used to do it? I admit that I have been depressed before.

But look what we do here, maybe you'll be surprised, the original program can be so simple


Here is a simple example, in order to just explain the principle, PB0 is the mode button, short press the switch mode, PB1 is plus, if the long press the word is even added (played the electronic watch it?) That's right, that's it! )

#define KEY_MODE 0x01//Mode button
#define Key_plus 0x02//Plus

void Keyproc (void)
{
if (Trg & Key_mode)//If Key_mode is pressed, and you often press this button,
{//It is not going to perform the second time Oh, must first release and then press
mode++; Mode register plus 1, of course, here's just a demo, you can do what you want to
Any code that executes
}
if (Cont & Key_plus)//If the "Plus" button is pressed
{
cnt_plus++; Timing
if (Cnt_plus >)//20ms*100 = 2S If the time is up
{
Func (); You need to execute the program
}
}
}

I don't know how you feel? I think it's quite simple to complete the task, of course, as a demo with the code.

Application 3: Mixed use of touch-type and switch-type buttons
The point touch button is estimated to use the most, especially the single chip microcomputer. Switch type is also very common, such as the home of the lights, those who press will not release, unless off. This is the two key forms of processing principle is nothing special, but have you ever thought, if a system inside these two kinds of keys is how to deal with? I think of my previous treatment, separate two very similar handlers, now looks really stupid, but there is no way AH, structure determines the program. But now, with the method described above, it is easy to get the job done.

Principle? You may also think, for the touch switch, according to the above method of handling a press and long press, for the switch type, we only need to deal with the Cont is OK, why? It's simple, think of it as a long button, so you find something in common and block out all the details. Program will not give, is completely application 2 of the content, here is to explain the principle ~ ~

OK, this good button processing is finished. Maybe some friends will ask, why not say time-lapse problem? Haha, was seen through. Sure enough not to be lazy. Let's talk about this question, and by the way it's very simple to talk about my own time-slice and how to shake it.

Time-lapse method is very traditional, that is, the first time to judge a button, delay a certain amount of times (the general Habit is 20ms) re-read port, if two read the same data, indicating that the real button, not jitter, then enter the key processing program.

Of course, do not tell me you delay (20) like that to die cycle, really that, I sincerely suggest you put down all the things, good to understand the operating system of the time-sharing principle, probably know the idea can, do not need to look at the principle of detail, otherwise you will never escape the "rookie" this circle. Of course I am also a rookie. I mean, the real MCU getting started is from learning to deal with multitasking, and this is the biggest difference between the school program and the company program. Of course, this article is not specifically said this, so also not caught dead.

My main program architecture is this:

volatile unsigned char intrcnt;
void Interrupthandle ()//Interrupt Service Program
{
intrcnt++; 1ms interrupt 1 times, variable
}

void Main (void)
{
Sysinit ();
while (1)//20MS performs a large cycle
{
Keyread (); Scan each sub-program once
Keyproc ();
Func1 ();
Funt2 ();
...
...
while (1)
{
if (INTRCNT>20)//has been waiting until 20ms time to
{
intrcnt= "0";
Break Return to main loop
}
}
}
}

Seems to pull away, back to our earlier question, that is, how to do the button shake processing. We put the Read button program in the main loop, that is, every 20ms we will execute once Keyread () function to get the new TRG and Cont values. Okay, here's my shake-up part: it's simple.

Basic architecture as above, I like the more, has been used. Of course, and this cooperation, each sub-program must be executed for a long time, more can not die cycle, the general use of finite state machine approach to achieve, specific reference other information.

After understanding the basic principles, as to how to use the people slowly thinking, I think it is difficult to smart engineers. For example, there are some processing,

How to determine the key release? Quite simply, both TRG and cont are 0 and must have been released.

A new type of key scanning program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.