Principle and keypad - Based on the key sheet GPIO

   Part I describes the simple ADC realized the need to provide an additional IC ADC. But for IC cost considerations, we can not provide that the ADC, but offers more additional GPIO (General Purpose Input Output: two-way: You can input / output, only two states High / Low), you can use Benpian the implementation.

    GPIO keypad based
    on GPIO realize a key sheet, statements need to provide additional GPIO use. GPIO port can be enough if a key corresponding to a GPIO, enough matrix can be used when a scanning manner, even if the available GPIO matrix scan mode are not satisfied, may also be further extended to increase the diode 2 6 implemented GPIO keys, this section briefly describes the method. Also can be achieved by a method of extended GPIO, is described in a length of the next.

    Embedded development, GPIO control interface is the most common, ordinary GPIO only a High / Low two states, we could be Read / Write operations.

   1: enough for use GPIO
    then each key corresponds to a GPIO port, the circuit design ensures that: there is no key operation of the GPIO port as Low, a key is pressed GPIO port is High, or vice versa. Software design requires only individually scan each GPIO port status, you can know whether there is a key action.

    2: for use as limited GPIO: using the matrix key scanning mode is the most common solution.

    The principle is as follows: a connection between any two GPIO buttons, such GPIO_1 & GPIO_2, its default state is Low, then the key is pressed, both on the communication, which means that we can give GPIO_1 a High signal, then go read GOIO_2, if get High, then this button is pressed, otherwise no keystrokes.

    Mathematics tells us, based on the N GPIO ports such programs, can achieve up to N * (N-1) / 2 keys, that is, the number of combinations of N. FIG follows: 4 GPIO (A / B / C / D) The principle of FIG 6 (KEY_1 ... KEY_6) of keys

 

      Also it becomes relatively simple, to the C-code as follows:

 

    Here, through appropriate process design, the code can be made compact and orderly. In the relatively few cases keys, to read and write next to each method is also good, but when more buttons, process design of the code above is more of an advantage. Can in 1 minute, understand why the use of i * k + k-1 as a return index, I believe your logic is OK.

    3: --- two further expanded up to six buttons GPIO

  If your design, the number of GPIO port is limited, can not meet the needs of matrix scan mode, then you have two options: 1 GPIO port expansion, we will be devoted to the next space; 2 further expanded to increase the number of diode matrix the ability to scan mode. Here we discuss a two GPIO method to achieve six keys, the principle is as follows:

 

 

 

    This implementation is actually using a unidirectional conducting diode characteristics, so that we can distinguish more keys, as follows:

    1: Write GPIO_1 = 1 & GPIO_2 = 1, then Read if GPIO_1 == 0 & GPIO_2 == 0, was KEY_3;

                                                              if GPIO_1 == 0 & GPIO_2 == 1, was Key_1;

                                                              if GPIO_1 == 1 & GPIO_2 == 0, was Key_2;

    2:Write GPIO_1=0 & GPIO_2=1, 接着Read  if GPIO_2==0,则为 KEY_4 or KEY_6;

              Further determination needs: Write GPIO_1 = 1 & GPIO_2 = 0 then Read: if GPIO_1 == 0 was KEY_4;

                                                                                        if GPIO_1 == 1 was KEY_6;

    3: Reverse Order step 2, can be distinguished KEY4 & KEY5.

    Understand the principles, software programming is relatively simple, given a realization as follows:

 

 

In this programming where, after a write operation, may require appropriate Delay so that its steady state; if the embedded system bus-based, but also need to release the bus before the function exits.

    3: Summary and Discussion

    Scanning the keypad matrix based on the realization, it is the most commonly used. In the case of relatively scarce GPIO port can be solved by appropriately increasing the number of diodes in the circuit. If this does not resolve, we must try to expand GPIO, and discussed later.

    The following gives a very good scan + diode matrix extension circuit diagram, you can write a driver for it yet? Oh quite challenging, try it ......

 

 

Original link: https: //blog.csdn.net/nutriyang/article/details/4368050

 

Guess you like

Origin www.cnblogs.com/yuanqiangfei/p/11479355.html