Matrix keyboard scanning principle (with microcontroller driver)

Publisher:sedsedqLatest update time:2022-04-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This circuit diagram is equivalent to a coordinate diagram. Each key has a unique coordinate to determine which key is pressed.

Method 1: Line-by-line scanning: The matrix keyboard is scanned line by line by outputting low level in turn through the upper four bits. When the data received by the lower four bits are not all 1, it means that a key is pressed. Then, the key that is pressed is determined by which bit of the received data is 0.


Method 2: Row and column scanning: output low level through all high four bits and high level through low four bits. When the received data, the low four bits are not all high level, it means that a key is pressed, and then judge which column has a key pressed by the received data value, and then conversely, the high four bits output high level and the low four bits output low level, and then judge which row has a key pressed according to the received high four bits value, so that it can be determined which key is pressed.


#include "reg52.h"             

typedef unsigned int u16;     

typedef unsigned char u8;

 

#define GPIO_DIG P0

#define GPIO_KEY P1

 

sbit LSA=P2^2;

sbit LSB=P2^3;

sbit LSC=P2^4;

 

u8 KeyValue;    

 

u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

                    0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //Display the value of 0~F

 

 

void delay(u16 i)

{

    while(i--);    

}

 

 

void KeyDown(void)

{

    char a=0;

    GPIO_KEY=0x0f;

    if(GPIO_KEY!=0x0f) //Read whether the key is pressed

    {

        delay(1000); //Delay 10ms to eliminate jitter

        if(GPIO_KEY!=0x0f) //Check if the keyboard is pressed again

        {    

            //Test column

            GPIO_KEY=0x0f;

            switch(GPIO_KEY)

            {

                case(0X07): KeyValue=0;break;

                case(0X0b): KeyValue=1;break;

                case(0X0d): KeyValue=2;break;

                case(0X0e): KeyValue=3;break;

            }

            //Test line

            GPIO_KEY=0XF0;

            switch(GPIO_KEY)

            {

                case(0X70): KeyValue=KeyValue;break;

                case(0Xb0): KeyValue=KeyValue+4;break;

                case(0Xd0): KeyValue=KeyValue+8;break;

                case(0Xe0): KeyValue=KeyValue+12;break;

            }

            while((a<50)&&(GPIO_KEY!=0xf0)) //Detect button release detection

            {

                delay(1000);

                a++;

            }

        }

    }

}

 

 

void main()

{    

    LSA=0; //Provide a bit selection for a digital tube

    LSB=0;

    LSC=0;

    while(1)

    {    

        KeyDown(); //Key judgment function

        GPIO_DIG=smgduan[KeyValue]; //

    }        

}

 

 

 


Reference address:Matrix keyboard scanning principle (with microcontroller driver)

Previous article:Introduction to 51 MCU - Digital tube dynamic display experiment
Next article:MCU Interrupts - Technical Summary

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号