Analysis of 51 MCU keyboard scanning program

Publisher:快乐舞蹈Latest update time:2021-02-04 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/********************************************Keyboard_No timer_No delay Features: The key is effective after releasing the hand, high sensitivity, low resource consumption, high operating efficiency Independent keyboard: K01=P2^4; K02=P2^5; K03=P2^6; K04=P2^7; Matrix keyboard: Row (up to down)_P2.3_P2.2_P2.


/****************************************



Keyboard_No timer_No delay


Features:


The button is effective after releasing the hand, with high sensitivity, low resource consumption and high operating efficiency.


Independent keyboard: K01=P2^4;K02=P2^5;K03=P2^6;K04=P2^7;


The matrix keyboard is: row (up to down)_P2.3_P2.2_P2.1_P2.0


Column (left to right)_P2.7_P2.6_P2.5_P2.4


The operation functions provided are:


//Independent keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key


extern unsigned char keyboard_self();


//Matrix keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key**** to detect the high four bits


extern unsigned char keyboard_matrix();


****************************************/


.


Let’s look at the independent keyboard first (the algorithm is the same as the matrix keyboard)


-----------------------------------------------------------------------


#include


#include


//Independent keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key


extern unsigned char keyboard_self()


{


unsigned char num_key=0;//key number


unsigned char temp=0; //Used to read the key value on P2 line


static unsigned char temp_code=0;//Save key value


static unsigned char num_check=0; //Low level valid times


static unsigned char key_flag=0; //Key valid flag


temp=P2&0xF0; //Read P2 line data


if (temp!=0xF0) //Low level judgment


{


num_check++;


if (num_check == 10) // if the low level is valid for 10 consecutive times (10ms), the button is considered valid


{


key_flag=1; //Enable key valid flag


temp_code=temp; //Save key value


}


}


else//Judge when releasing the hand


{


num_check=0;


if (key_flag == 1) //button is valid


{


key_flag=0;


switch (temp_code) // read the key number


{


case 0xE0: num_key=1;


break;


case 0xD0: num_key=2;


break;


case 0xB0: num_key=3;


break;


case 0x70: num_key=4;


break;


}


}


}


return (num_key);


}


Now it's a matrix keyboard


-----------------------------------------------------------------------


#include


#include


//Matrix keyboard. If there is no key action, the return value is num_key=0, otherwise it returns the key number num_key**** to detect the high four bits


extern unsigned char keyboard_matrix()


{


unsigned char num_key=0;//key number


unsigned char temp=0; //Read P2 port line data


status unsigned char temp_code=0; //Used to save key value


status unsigned char temp_circle=0xFE; //Save the circular scan value on the P2 line


static unsigned char num_check=0;//low level count


static unsigned char key_flag=0;//Key valid flag


P2=temp_circle; //0xFX


temp=P2; //Read P2 port line data


if (temp!=temp_circle) // there is a key action


{


num_check++; //Low level count | add 1 when the level is low


if (num_check == 10) //Low level is valid for 10 consecutive times (10ms)


{


key_flag=1; //Key valid flag set to 1


temp_code=temp; //Save key value


}


}


else //Release the hand or no key action, the scan line should be changed at this time


{


num_check=0;


if (key_flag == 1) //Key valid judgment


{


key_flag=0;


switch (temp_code) // read the key number


{


//P2^0 line


case 0xEE: num_key=1;


break;


case 0xDE: num_key=2;


break;


case 0xBE: num_key=3;


break;


case 0x7E: num_key=4;


break;


//P2^1 line


case 0xED: num_key=5;


break;


case 0xDD: num_key=6;


break;


case 0xBD: num_key=7;


break;


case 0x7D: num_key=8;


break;


//P2^2 line


case 0xEB: num_key=9;


break;


case 0xDB: num_key=10;


break;


case 0xBB: num_key=11;


break;


case 0x7B: num_key=12;


break;


//P2^3 line


case 0xE7: num_key=13;


break;


case 0xD7: num_key=14;


break;


case 0xB7: num_key=15;


break;


case 0x77: num_key=16;


break;


}


}


temp_circle=_crol_(temp_circle,1);//Change the scan line


if (temp_circle == 0xEF)


{


temp_circle=0xFE;


}


}


return (num_key); //Return key number


}


/****************************************************************************


When no key is pressed, the scan line keeps changing.


When a key is pressed for a long time, the scan line does not change, so that the keys in this row become independent keys, which makes the scanning efficiency extremely high.


For example, when a key on the P2.0 line is pressed, the program will scan this key, and then the scan line will not change.


When the keyboard program is entered 10 times in succession, it detects that 10 key presses are valid, and the scan line does not change until the key is released.


*****************************************************************************/


Reference address:Analysis of 51 MCU keyboard scanning program

Previous article:How to use 51 single-chip digital tube to realize the function of marquee?
Next article:A brief analysis of the four ways to use the 51 MCU IO port

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号