When the number of keys on a keyboard is relatively large, assigning one microcontroller IO port to each key will result in insufficient IO ports. To reduce the occupation of IO ports, keys are usually arranged in a matrix format. In a matrix keyboard, each horizontal line and vertical line are not directly connected at their intersections; instead, they are connected through a key (see the circuit diagram that follows for details). In this way, one port (e.g., the P1 port) can form 4 × 4 = 16 keys, doubling the number compared to using the port lines directly for the keyboard. Moreover, the advantage becomes more significant as the number of lines increases. Thus, it can be seen that when a relatively large number of keys is required, it is reasonable to use the matrix method to implement the keyboard.
From left to right, the terminal order is: empty, 1, 2, 3, 4, 5, 6, 7, empty.
![]()

From left to right, the terminal order is: empty, 1, 2, 3, 4, 5, 6, 7, 8, empty.










