In the embedded system, the most commonly used input device is the key. The user’s application needs can be transferred to the system software through the corresponding key. The software turns to complete the user’s request and realize simple human-computer interaction. The author here makes a brief introduction to the realization of matrix key.

1. Overview of key input

The key is a normally open button switch. At ordinary times, the two contacts of the key are in the open state, and they are closed only when the key is pressed. The key control circuit is used to monitor the key in real time. When a key is connected, the input pin level in the circuit monitoring changes. After detecting this change, the control circuit scans the key, locates the position of the key, and feeds back the relevant key information to the upper application. Common key input design has independent key, matrix key. Independent keys each key occupies an IO port, the circuit configuration is flexible, and the software is simple, but when there are many keys, the IO port wastes a lot. Matrix keys are suitable for the occasions with a large number of keys, which are composed of row lines and column lines. The keys are located at the intersection of rows and columns. Save IO port. Generally, the key control circuit detects the key input through the query mode or interrupt mode. The query mode needs to occupy a certain amount of CPU resources. If the query frequency is too low, the key input may be lost, and if the query frequency is too high, the CPU resources will be wasted. Generally, the key query frequency is about 50 Hz. The interrupt mode needs to occupy the CPU all the way, but it will not occupy the CPU resources. As long as the key is pressed, the CPU can immediately detect the input, scan and get the key value.

2. Hardware design

The matrix key design of 4×4 is adopted here. Of course, the matrix keyboard can be connected to the external interrupt pin of MCU through four Schottky diodes to form a four input and gate (refer to the author’s article), so as to realize the interrupt mode to detect the key input. In order to be compatible with the common matrix key design of the development board, the matrix key interface of 4×4 is connected to the P1 port, and the key input is detected by query.

Design of 4x4 matrix key driver based on 51 single chip microcomputer

Figure 2-1 4×4 matrix button

3. Driver implementation

Because we use the key design of query mode, the MCU needs a certain frequency to scan the P1 port keys. Usually, the frequency is about 50 Hz, which is more appropriate. In order to ensure the scanning frequency, the timer is usually used to generate a time scale to scan periodically. P1.4 ~ P1.7 column lines are connected to VCC through pull-up resistance, P1.0 ~ p1.3 line lines generate corresponding scanning signal, no key, column line is in high level state, press the key, column line level state will be determined by the line level connected with the column line. If the row line level is low, the column line level is low; if the row line level is high, the column line level is high.

The key scan function is as follows, which needs to be executed periodically to scan the status of the key. Taking 51 single chip microcomputer as an example, P1.0 ~ p1.3 output scanning signal line by line, and implement interface macro keyoutputselect ()

#define KeyOutputSelect(Select) {P1 = ~(1<>

After the scan line is output, the key status of the corresponding scan line (P1.4 ~ P1.7) needs to be read, and the pin status reading interface macro keygetpinstate() is also implemented in the key. H module header file

#define KeyGetPinState() (P1>> 4)

After reading the status of the key pins under the corresponding scan line, it is necessary to determine which pin level is 0 (press), reverse the read pin status, and search 1 algorithm for the pin status variable. The speed of getting the key value can reach the fastest, and when multiple keys are pressed at the same time, the key with the highest priority can be correctly obtained. If the key is pressed effectively, the key value of 0 ~ 15 will be obtained. If the key is not pressed, the key value of 16 will be obtained.

voidKeyScan()

{

unsigned char i;

unsigned char KeyValue;

unsigned char PinState;

if (KeyState.State == STATE_ DISABLE) {

return; // When the key is disabled, the keyboard is not scanned

}

//Key value is 0 ~ 15, no key value is 16, any number of keys can be pressed

//Return the key value with the highest priority correctly

KeyValue = 0;

for (i=0; i<4; i++)=””>4;>

KeyOutputSelect(i); // Output scan line

//Get the key status of the corresponding scan line

PinState = KeyGetPinState();

//When a key is pressed, the position of 0 in pinstate is the key value position

PinState = ~PinState;

//Search pinstate for the first bit of 1

if (!( PinState & 0xf)) {

KeyValue += 4;

continue; // The scan line does not press the key to enter the next scan line

}

//The scan line has a key press to search the position of 1

if (!( PinState & 0x3)) {

KeyValue += 2; // The lower 2 bits (P1.4 ~ P1.5) are not pressed

PinState >>= 2; // Shift retrieval (p1.6-p1.7)

}

if (!( PinState & 0x1)){

KeyValue += 1;

}

break; // Press the key to exit and continue scanning

}

KeyStore(KeyValue); // Save key status

}

After getting the key value, we need to process the key value and save the possible key message into the buffer according to the key state, so that the user program can read and process it. Keys usually have the following states: Press, release and long press. It needs to support the functions of press detection, release detection, long press and combo. It also needs to de shake and filter the keys. The state of keys is often switched in these cases. Therefore, it is a clear idea to program the state machine of keys. In the keystore () function, we can judge the key state transition. In the module, we track and record the key state through the key state structure variable keystate

typedef struct {

unsigned char State; // Key state transition

unsigned int TImeCount; // The timing used to track each state

} KEY_ STATE;

staTIc KEY_ STATE KeyState; // Key state machine state transition

After detecting the corresponding key event (key)_ UP、KEY_ DOWN、KEY_ The corresponding key messages need to be generated and saved into the key cache. Usually, a key queue cache can be opened to save multiple generated key messages, so that the key will not be lost due to the user code’s failure to process the key in time. In order to avoid complexity, the author takes a key buffer as an example, and the key event structure variable keybuffer is used to save the key messages

typedef struct {

unsigned char Value;

unsigned char State;

} KEY_ EVENT;

//The key value obtained by key scanning is stored in keybuffer, including key value and key status

staTIc volaTIle KEY_ EVENT KeyBuffer;

The key jitter elimination and long press are based on time. We define the jitter elimination time, long press time judgment and corresponding state macro in the module

//The scanning cycle of the key is 20ms

#define WOBBLE_ Count 1 / / key jitter elimination count, 1 key scan cycle (20ms)

#define LONG_ Count 100 / / long press 100 scan cycles to judge as long press (2S)

#define STATE_ Init 0x0 / / key initialization status

#define STATE_ Wobble 0x1 / / button dithering status

#define STATE_ Long 0x2 / / press and hold the key to detect the status

#define STATE_ Release 0x3 / / key release status

#define STATE_ Disable 0x4 / / the key is disabled

The complete keystore () function is implemented as follows

static voidKeyStore(unsigned char Value)

{

static unsigned char LastValue;

switch (KeyState.State) {

case STATE_ Init: / / wait for the initial state key

if (Value < key_ null)=””>

//Record the key pressed and enter the state of eliminating chattering

LastValue = Value;

KeyState.TimeCount = WOBBLE_ COUNT -1;

KeyState.State = STATE_ WOBBLE;

}

break;

case STATE_ WOBBLE:

if (KeyState.TimeCount) {

KeyState.TimeCount–; // It’s not time to eliminate shaking

break;

}

//If it is judged to be the same key value again after shaking elimination, it is considered that the key is pressed to save the key value

//And enter the long press detection

Source; 21ic

Application of Small Signal Schottky diode in digital circuit

Leave a Reply

Your email address will not be published. Required fields are marked *