51 MCU Experiment 7: Matrix Buttons

Publisher:灵感发电站Latest update time:2020-04-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The circuit diagram of the development board matrix button module is as follows:

The circuit diagram of the digital tube module of the development board is as follows:

Matrix key scanning principle:


Method 1: Line-by-line scanning: We can scan the matrix keyboard 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, we can determine which key is pressed by which bit of the received data is 0.


Method 2: Row and column scanning: We can output low level by all the high four bits and high level by the low four bits. When the low four bits of the received data are not all high level, it means that a key has been pressed. Then, we can judge which column has been pressed by the received data value. Then, we can judge which row has been pressed by the high four bits and low four bits. Then, we can judge which row has been pressed by the received high four bits. In this way, we can determine which key has been pressed.

In this program, the matrix buttons are scanned using row and column scanning!


In this program, the matrix buttons are scanned using row and column scanning!

Program purpose: Assign values ​​of 0, 1, ..., 15 to the 16 buttons in the matrix respectively. When pressed, the label will be displayed on the first digital tube.

Notice:


The value represents the key pressed. To find out the row and column relationship between the keys, only a one-dimensional value is needed to determine the position of the key without finding the row/column numbers separately.

Waiting for the key release statement: while() is different from the while() in the independent key experiment program, so pay attention to the difference!

New tips: Before programming, you can assign new variable names to registers such as IO ports in time to facilitate program modification and other operations, increasing program readability!

#include

#define uc unsigned char

#define dig P0

#define key P1

sbit lsa=P2^2;

sbit lsb=P2^3;

sbit lsc=P2^4;

int value; 

uc code x[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,

0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay(uc x)

{

uc i,j;

for(i=1;i<=x;i++)

for(j=1;j<=110;j++);

}

void keypros()

{

uc a=0;

key=0x0f;

if(key!=0x0f)

{

delay(10);

if(key!=0x0f)

{

key=0x0f;

switch(key)

{

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

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

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

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

}

key=0xf0;

switch(key)

{

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

case(0xb0): value=value+4; break;

case(0xd0): value=value+8; break;

case(0xe0): value=value+12; break;

}

}

while((key!=0xf0)&&(a<50))

{

a++;

delay(10);

}

        /*while(key!=0xf0);*/

}

}

void main()

{

lsa=lsb=lsc=0;

while(1)

{

keypros();

dig=x[value];

}

}

Reference address:51 MCU Experiment 7: Matrix Buttons

Previous article:STC89C52 series MCU internal resources - timers and counters
Next article:STC89C52 series microcontroller internal resources - serial communication

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号