A matrix keyboard scanning program that feels better

Publisher:collectorsLatest update time:2012-08-15 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I came up with this program when I was writing a calculator program. I always felt that the commonly used keyboard scanning program was troublesome to use. It always had to scan line by line, and it looked very cumbersome. So I came up with a matrix keyboard scanning program. The principle is very simple!
The basic idea of ​​this program is that two intersecting straight lines have only one intersection point. To put it simply, it detects the rows first and then the columns, or vice versa.
This keyboard scanning subroutine is a parameterless function with a return value, so you just need to calculate an array beforehand!
The following is the implementation of the sub-function. I only use a 3*4 matrix keyboard as an example here. The principle is the same! [page] 
==============================================================================
uchar code key_table[]=
{
0x77,
0x7b,
0x7d
, 0xb7 ,
0xbb
, 0xbd
,
0xd7, 0xdb,
0xdd,
0xe7,
0xeb,
0xed
};
/*The above array must be calculated in advance. You can get the value corresponding to the corresponding key by connecting the row and column values ​​together*/
unsigned char key_scan(void ) 
{
unsigned char i, key_code; /*Variable declaration*/
key_port = 0x0f; /*Here key_port is a GPIO port defined in the previous macro.
First set the row (or column) */                  
if (key_port ! = 0x0f) /* If a key is pressed, the initial value of GPIO will change,
The following statements will be executed*/ 
{
delay(5); /*The delay here is to achieve debounce, which will not be introduced here*/
if(key_port != 0x0f) /*Confirm that a key is really pressed*/
{
key_code = key_port; /*Assign the value of the row (or column) to key_code*/
key_port = 0xf0; /*Then set the column (or row)*/
key_code |= key_port; /*Combine the row and column values ​​and assign them to key_code*/
for(i = 0; i < 12; i++) /*Because there is only one key_code corresponding to each key pressed,
Therefore, according to the number of keys on the matrix keyboard, the corresponding FOR loop can be used to determine which key is pressed*/ 
{
if(key_table[i] == key_code)
{
key = i+1;
break;
}
}
}
}
else key = 0;
return key; /*The returned KEY value is the key at the corresponding position*/
}
 
Reference address:A matrix keyboard scanning program that feels better

Previous article:Use of common LCD screen LCD1602
Next article:Design of digital clock based on stc89c52rc microcontroller (digital tube display)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号