51 MCU Learning 5-Independent Buttons and Matrix Keyboard

Publisher:中原读书客Latest update time:2015-06-24 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Episode 14

 

The principle of keyboard

Keyboards are divided into coded keyboards (such as computer keyboards) and non-coded keyboards (identified by programs themselves).

Non-encoded keyboards are divided into: independent non-encoded keyboards (independent keys), row-column non-encoded keyboards (4*4 array keyboards)

 

Circuit diagram of a standalone keyboard.

 

Because the IO port of the 51 single-chip microcomputer is not a bidirectional port but a quasi-bidirectional port, in order to make the IO port have input function, the IO port must be set to 1. After setting it to 1, when the button is pressed, the level of the IO port will be pulled down, that is, it will be set to 0. When the IO port is detected to be 0, it can be determined that the button has been pressed. There will be a jitter process when the button is pressed (the spring will jitter). Since the speed of the single-chip microcomputer detecting the IO port is very fast, which exceeds the frequency of the spring jitter, when the single-chip microcomputer detects that the IO port is 0, it needs to delay for a short time and then detect whether the IO is 0. If it is still 0, it is confirmed that the button is pressed. Because there is a pull-up resistor in the IO port, when the button is released, the IO port is pulled high again.

Routine:

#include

 

#define uint unsigned int

#define uchar unsigned char

 

sbit Key = P3^4; // key

sbit Led = P1^0; // Ice 灯

void delay(uint z);

 

/******** Main function ********/

void main()

{

while(1)

{

if(!Key)

{

delay(10); // debounce operation

if(!Key)

Led = 0; // Led lights up when pressed

else

Led = 1;

}

}

}

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

 

Episode 15

4*4 matrix keyboard

In the above figure, one button occupies one IO port. If there are 16 buttons, 16 IO ports will be occupied. In order to reduce the use of IO ports, it is necessary to connect them in a matrix. As shown in the following figure


 

Matrix scanning principle

From the figure we can see that P30, P31, P32, P33 are rows (lower four bits), and P34, P35, P36, P37 are columns (upper four bits).

Suppose we press the S6 button. [page]

The first step is to determine the column and assign 0xF0 = 1111 0000 to P3 port, then P37, P36, P35, P34 are all set to 1, P33, P32, P31, P30 are all set to 0. When S6 is pressed, since P31 on one side of the S6 button is 0, P35 connected to the other side of S6 is pulled low, that is, equal to 0. As shown in the figure below

At this time, the column value P3 = 1101 0000 = 0xD0. In the program, we only need to determine whether P3 is equal to 0xF0. If it is not equal, it means that a key is pressed.

The second step is to not change the state of the upper four bits: 1101 Set all the lower four bits to 1 (P3 = P3 | 0x0F). At this time, it becomes 1101 1111. Since the microcontroller scans the columns very quickly, the key is still in the pressed state when scanning the rows (human reaction is not as fast as the microcontroller). S6 is pressed, and since P35 connected to S6 is at a low level (i.e. 0), P31 changes from a high level (i.e. 1) to a low level (i.e. 0) as shown in the figure below.

The value obtained at this time is P3 = 1101 1101 = S6 is pressed. This is the detection principle. 

 

Full Program:

#include

 

#define uint unsigned int

#define uchar unsigned char

 

sbit Led = P1^0;

sbit Led1 = P1^1;

void delay(uint z);

 

/******** Main function ********/

void main()

{

uchar Key_Temp;

uchar Key; // Key value

 

while(1)

{

Key = 0; // clear to 0 

P3 = 0xF0;

Key_Temp = P3;

Key_Temp &= 0xF0; // Only take the upper four bits This sentence is necessary because 51IO is only quasi-bidirectional. To make it have input function, it needs to be set to 1

if(0xF0!=Key_Temp) // Check if a key is pressed

{

delay(10); // Delay for a while to skip the jitter time

Key_Temp = P3 & 0xF0 ; // First take P3 and then AND it with 0xF0 to get the upper four bits

if(0xF0 != Key_Temp) // Check again whether the key is pressed

{

P3 = Key_Temp | 0x0F; // Keep the upper four bits Set all the lower four bits to 1 and output

Key = P3; // read in again

}

}  

switch(Key)

{

 

case 0xEE: Led = 0; break;  // S1

case 0xDE: Led1 = 0; break;

case 0xBE: break;

case 0x7E: break;

case 0xED: break;

case 0xDD: break;

case 0xBD: break;

case 0x7D: break;

case 0xEB: break;

case 0xDB: break;

case 0xBB: break;

case 0x7B: break;

case 0xE7: break;

case 0xD7: break;

case 0xB7: break;

case 0x77: break;  // S16

default:

Led = Led1 = 1;

}

}

}

 

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

Reference address:51 MCU Learning 5-Independent Buttons and Matrix Keyboard

Previous article:4*4 keyboard scan code
Next article:51 MCU Learning 3- Stepper Motor

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号