Briefly Talking about the Principle of Detecting Buttons by Single Chip Computer and the Method of Interrupting Button Detection

The principle of single-chip detection key

Let me talk about the independent keyboard detection first. In the peripheral circuit of the single chip microcomputer, the commonly used keys are mechanical elastic switches. When the switch is closed, the line is turned on, and when the switch is turned off, the line is disconnected. The principle of the MCU detecting the button: one end of the button is grounded, and the other end is connected to an I/O port of the MCU, and the I/O port is first assigned a high level, and then the MCU continuously detects whether the I/O port becomes low Level, when the button is closed, it is equivalent to the I/O port connected to the ground, it will become a low level. When the MCU detects whether the button is pressed, the actual waveform of the voltage is slightly different from the ideal waveform. The waveform has jitter at the moment of pressing and releasing. The length of the jitter time is related to the mechanical characteristics of the button. Therefore, the MCU must add a debounce operation when detecting whether the keyboard is pressed, so when writing the keyboard detection program of the MCU, the debounce delay is generally added when detecting the press. When the independent keyboard is connected to the single-chip microcomputer, each button needs an I/O port, which will occupy too much I/O port resources. So the matrix keyboard is brought out.

In the connection mode of the matrix keyboard, each row connects one end of each key together to form a row line, and each column connects the other end of the key together to form a column line. In this case, 16 buttons are arranged in 4 rows and 4 columns as long as 8 lines. Its key detection, to put it simply, is to send a column of low level first, and the rest are all high level, and then take turns to check to confirm the ranks.

There is another thing to mention here. The switch-case statement is also called the switch statement. It is a conditional selection statement specially used to deal with the multi-branch structure. Use the switch statement to directly handle multiple branches.

The key detection is the first few routines played by the students who are new to the MCU. There are many types of keys (common buttons, matrix keyboards, etc.), and there are many methods of key detection. For ordinary buttons, of course, you will say that it is not easy to design a peripheral circuit to read through the GPIO port. Perhaps you say that edge detection is done through external interrupts. For matrix keyboards, you often use separate scanning methods for key detection. Usually We will add a key anti-shake (the classic method is to delay the second anti-shake method for key anti-shake, and when the key is released, you may use the while statement to anti-shake, that is, if the key is kept pressed, Let the program die in while), these are common methods for beginners, but if it is a large program, it often does not allow you to have so many delays and endless loops.

Interrupt is a frequently used key detection method, the time between interrupts can be used as anti-shake processing, and this method is suitable for program fusion, because the data reading of other sensors is usually done by means of interrupt (such as camera). Here I give a problem and a solution.

Problem: Design a key detection program (multi-key), read through the interrupt, and only respond to the main program when the key is updated, otherwise ignore it, and require anti-shake design, only two detections are The key value is updated only when it is true. The truth table is as follows.

Briefly Talking about the Principle of Detecting Buttons by Single Chip Computer and the Method of Interrupting Button Detection

IN_old_stable is the old certain value.

IN_new_stable is the new determined value.

IN_old is the key value read last time.

IN_new is the latest key value read.

Of course, you need to do an iteration after each interruption:

IN_old=IN_new;

IN_old_stable=IN_new_stable;

Only the new certain value is trustworthy.

Calculation rule by truth table:

IN_new_stable: D

IN_old_stable: A

IN_old: B

IN_new: C

Have:

So have:

Solution: I take STM32F103 as an example, I need to read GPIO, the lower four bits of port B.

We first deal with the interrupt as follows:

IN_old=IN_new;

IN_old_stable=IN_new_stable; //The first two lines are iterated first.

IN_new=(GPIO_ReadInputData(GPIOB))&0x00001111;//Read the lower four bits of GPIOB.

IN_temp=(IN_old^IN_new)&IN_old_stable; //Intermediate variable

IN_temp|=IN_old&IN_new;

IN_new_stable=IN_temp; this gives us the latest key value, which is determined by the old determined value in the past, the value of the two times before and after the key press, so you see, you have to experience at least three interruptions to update a key value .

Ok, here we have detected the value of the key, we have one more step, how to tell the main function whether there is a key value update.

We can know by analysis: if we compare IN_new_stable and IN_old_stable twice to determine whether the value is right

Define temp=IN_new_stable^IN_old_stable; if there is a key value update on the lower four digits, then IN_new_stable and IN_old_stable determine whether the value is different twice, and it is reflected in the corresponding bit through XOR calculation.

Well, we assume that the lower four bits are pulled high during hardware design, we initialize an intermediate variable sthchange=0x00001111;

We XOR the sthchange and temp to know which one is set low.

sthchange^=temp.

It is enough to give sthchange to the main program to judge. When there is no key value update, sthchange=0, and when there is key value update, sthchange》1, you can not know which key value has changed through bit operation.

Network Tools

we are the best supplier in China to offer the networking tools including sort of insertion tools, impact and punch down tool (Ericsson punch down tool, Siemens punch down tool, Corning punch down tool and so on), crimping tool(RJ45 crimping tools, coaxial cable crimping tool, picabond ratchet crimper), cable stripper and cutter, Cable Tester and connector removal tool, and so on.

In short, we offer the networking tools for cutting and stripping coaxial cable, twist cable, and optical fiber. And professional compression crimping tool for different connectors, insertion tools for different modules .To save cost for customers, we have desired some tools with multi-function. So you can keep one tool instead of several different types. Meanwhile, for some coaxial cable crimping tool you can change the head to fit the cable specification yourself. It will make you lose weight on tool set but to finish your work perfectly.

Insertion Tool, Punch Tool, Cat 5 Cable Tester, Crimping Pliers, Cable Crimping Tool

NINGBO YULIANG TELECOM MUNICATIONS EQUIPMENT CO.,LTD. , https://www.yltelecom.com

This entry was posted in on