Best practice of MCU software — matrix keyboard driver

Time:2021-8-7

1. Matrix keyboard vs independent keys

In the process of MCU application development, independent keys are common, but when a large number of keys are required, the use of matrix keyboard can reduce IO occupation and improve the utilization of system resources. For example, a MCU project requires 16 buttons. If the independent key scheme is adopted, 16 MCU pins need to be occupied. If the 4X4 matrix keyboard is adopted, only 4 + 4 MCU pins are required, which saves twice the occupation of IO resources, as shown in the figure. However, matrix keyboard also has its disadvantages. Compared with independent keys, the program design is slightly more complex.

This paper discusses the working principle of matrix keyboard, and provides a matrix keyboard driver with clear structure and easy to use.

2. Working principle of matrix keyboard

image

As shown in the figure, the 4X4 matrix keyboard is connected to the P2 port of MCU with 8 pins. P2 [3:0] is the row line and P2 [7:4] is the column line.

When programming P2 = 0xFE:

Then p2.0 is set to 0, that is, one end of all buttons in the first row is grounded. The single chip microcomputer can judge which column the pressed button is in by detecting the high four bits of P2. If P2 = = 0xEE is detected, the first button is pressed, if P2 = = 0xde is detected, the second button is pressed, if P2 = = be is detected, the third button is pressed, and if P2 = = 7e is detected, the fourth button is pressed.

When programming P2 = 0xfd, P2 = 0xfb, 0xff:

By analogy, you can determine which button is pressed in the second, third and fourth lines respectively.

The common detection procedure of matrix keyboard is line by line detection, namely:

//Pseudo code

Disadvantages of the above code:

  1. Code redundancy, the detection of each line is almost duplicate code;
  2. Each detection line needs a delay to eliminate jitter, which increases the delay and may miss detection

3. Matrix keyboard driver

3.1 implementation ideas

Idea:


Step 1:Make all the row lines 0, read the value of the column line and store it in th. This value reflects the column where the button is currently pressed


Step 2:Let all the column lines be 0, read the value of the row line and store it in TL. this value reflects the row where the button is currently pressed


Step 3:This value reflects the row and column of the currently pressed button.

For example:

Let P2 = 0xf0, and then read that the value of P2 is 0x70, it indicates that there is a key pressed in the fourth column

Let P2 = 0x0f, and then read that the value of P2 is 0x07, it indicates that there is a key pressed in the fourth row

Or after operation, 0x77 is obtained, which is the key code of the button in the fourth row and fourth column

Or after operation, 0xff is obtained, indicating that no key is pressed. It can be used as a condition to determine whether a key is pressed.

3.2 matrixkbd.c

/**
#include unsigned char key_ no;

3.3 external interface matrixkbd. H

/**

4. Experiment

Detect matrix keyboard events;

When pressing and popping up, the serial port prints out the key number

4.1 timer drive

4.1.1 timer drive timer0. C

In order to facilitate calling, time trigger mode is adopted and timer drive is added:

/**int systick;

4.1.2 timer external interface timer0. H

/**

4.2 serial port drive

4.2.1 UART. C realized by serial port driver

To facilitate printing, view debugging information and realize serial port driver:

/**void uart_ init(void)

4.2.2 serial port drive external interface UART. H

/**

4.3 main program

In the main program:

  1. Call the driver in the background according to the requirements of the driver: call key every 10ms_ scan
  2. Real time monitoring events: flg_ up,flg_ down,flg_ 10ms
/**

image