Study Notes: 51 MCU Keyboard

Publisher:pi26Latest update time:2021-10-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Basic supplement

1. Keyboard tasks

(1) Determine whether a key is pressed. If yes, proceed to the next step.

(2) Identify which key is pressed and find the corresponding key value.

(3) Based on the key value, find the handler entry for the corresponding key value.


2. Keyboard recognition

Whether the key is closed or not is reflected in the high or low level of the row line output voltage. The microcontroller can confirm whether the key is pressed or released by detecting the high and low states of the row line level. In order to ensure that the microcontroller only confirms that the key is valid once for a key action (the so-called key validity means that after pressing the key, it must be released), the influence of the jitter period t1 and t3 must be eliminated.


3. How to eliminate key jitter

(1) Use software delay to eliminate key jitter. The basic idea is: when a key is detected to be pressed, the row line corresponding to the key is at a low level. After executing a subroutine with a delay of 10ms, check whether the row line level is still at a low level. If it is still at a low level, it is confirmed that a key has been pressed in the row. When the key is released, the low level of the row line becomes a high level. After executing a subroutine with a delay of 10ms, the row line is detected to be at a high level, indicating that the key has been released.

(2) Use a dedicated keyboard/display interface chip, which has an automatic de-jitter hardware circuit.


4. Non-encoded keyboard and encoded keyboard

(1) A non-encoded keyboard means that when you press a key, the key number information cannot be obtained directly, but must be obtained through software. There are two common structures of non-encoded keyboards: independent keyboards and matrix keyboards.

(2) An encoded keyboard is one that can directly obtain the key number of a key when it is pressed, for example using a dedicated keyboard interface chip.


5. Scanning method of non-encoded keyboard

(1) Query scan:

When the MCU is idle, the keyboard scanning subroutine is called to scan the keyboard repeatedly to respond to the keyboard input request. If the query frequency of the MCU is too high, although it can respond to the keyboard input in time, it will also affect the progress of other tasks. If the query frequency is too low, the keyboard input may be missed. Therefore, the keyboard scanning frequency should be adjusted according to the busyness of the MCU system and the operation frequency of the keyboard.


(2) Scheduled scanning:

The microcontroller can scan the keyboard once at a certain interval, which is called timed scanning. This method usually uses the timer interrupt generated by the timer in the microcontroller to scan the keyboard after entering the interrupt subroutine. When a key is pressed, it identifies the pressed key and executes the corresponding key handler. Since the time for each key press is generally not less than 100ms, in order not to miss a valid key press, the period of the timed interrupt is generally less than 100ms.


(3) Interrupt scanning:

In order to further improve the efficiency of the MCU scanning keyboard, the interrupt scanning method can be used, that is, the keyboard will send an interrupt request signal to the MCU only when a key is pressed. The MCU responds to the interrupt, executes the keyboard scanning interrupt service subroutine, identifies the pressed key, and jumps to the processing program of the key. If no key is pressed, the MCU will ignore the keyboard. The advantage of this method is that it will only be processed when a key is pressed, so it has strong real-time performance and high work efficiency.


(ii) Independent keyboard

1. Schematic diagram of independent keyboard

The characteristic of a stand-alone keyboard is that each key is independent of each other, and each key is connected to an I/O port line. By detecting the level state of the I/O port input line, it is easy to determine which key is pressed.


2. Independent keyboard K1 controls LED1 code implementation

#include

sbit led1=P2^0; //because led1 is controlled by port p2^0

sbit k1=P3^1; //The output level of port P31 is controlled by key k1

void delay(int i)

{

while(i--);

}

void keyproc()

{

if(k1==0)

{

delay(1000); //delay to eliminate jitter

if(k1==0)

{

led1=~led1;

}

while(!k1) ;

}

}

void main()

{

while(1)

{

keyproc();

}

}


(III) Matrix keyboard

1. Schematic diagram of matrix keyboard


Matrix (also known as row-column) keyboards are usually used in situations where there are a large number of keys. It consists of row lines and column lines, and the keys are located at the intersection of rows and columns. Its interface circuit is shown in the figure above.

2. The matrix keyboard corresponds to the digital tube output 0 to code implementation

#include "reg52.h"

typedef unsigned char uchar;

typedef unsigned int uint;

#define GPIO_DIG P0

#define GPIO_KEY P1

 

uchar code smgduan[16]= {0x3f, 0x06, 0x5b, 0x4f,

                         0x66, 0x6d, 0x7d, 0x07,

     0x7f, 0x6f, 0x77, 0x7c,

     0x39, 0x5e, 0x79, 0x71}; //Static digital tube code value

 

uint KeyColValue;

uint KeyLineValue;

 

void delay(uint i) //delay function

{

    while(i --);

}

 

void KeyDown() //Keyboard key scanning function

{

    char a; 

    GPIO_KEY = 0x0f;

    if(GPIO_KEY != 0x0f) // Check which of the 4 rows is pressed

    {

        delay(1000); //delay to eliminate jitter

        if(GPIO_KEY != 0x0f) //Check again which of the 4 rows is pressed

        {

            switch(GPIO_KEY) //Determine which row of keys are pressed based on the IO value

            {

                case(0x07): KeyColValue = 0; break;

                case(0x0b): KeyColValue = 1; break;

                case(0x0d): KeyColValue = 2; break;

                case(0x0e): KeyColValue = 3; break;

            }

        }

    }

    GPIO_KEY = 0xf0;

    if(GPIO_KEY != 0xf0) //Check whether a key in the 4 rows is pressed

    {

        delay(1000); //delay to eliminate jitter

        if(GPIO_KEY != 0xf0) //Check again which column of keys in the 4 rows is pressed

        {

            switch(GPIO_KEY) //Determine which column of keys is pressed based on the IO value

            {

                case(0x70): KeyLineValue = 0; break;

                case(0xb0): KeyLineValue = 1; break;

                case(0xd0): KeyLineValue = 2; break;

                case(0xe0): KeyLineValue = 3; break;

            }

        }

 

        while((a < 50) && (GPIO_KEY != 0xf0)) // Delay to ensure no key is pressed

        {

            delay(1000);

            a++;

        }

    }

}

 

void main()

{

    while(1)

    {

        KeyDown(); //Check if the key is pressed

        GPIO_DIG = smgduan[KeyLineValue*4 + KeyColValue]; //According to the row and column values ​​of the keys, the static digital tube displays the corresponding values

    }

}

Reference address:Study Notes: 51 MCU Keyboard

Previous article:51 MCU Study Notes Interrupt
Next article:Make the 51 single-chip eight-segment digital tube light up

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号