10. Matrix buttons

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

insert image description here

The independent keys will be regrouped.

There are pull-up resistors in front of the button IO ports.

Scanning principle:

method one:

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 Two:

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.

From matrix buttons to independent buttons:

Make one line connecting the four buttons output low level continuously, and the others output high level (avoid affecting the used lines)


The last digit of the digital tube is controlled by the matrix button to display the number corresponding to the matrix button. The program is as follows:

The horizontal order is 0123, 4567, 89AB, CDEF:


#include

typedef unsigned char u8;

typedef unsigned int u16;


sbit LSA=P2^2;

sbit LSB=P2^3;

sbit LSC=P2^4;

#define GPIO_KEY P1 //Define the matrix key IO port as P1

#define GPIO_DIG P0 //Define the static digital tube IO port as P0

u8 KeyValue; //Each key is represented by a KeyValue

u8 a; //Prepare for the following press time detection

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

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

void delay(u16 i)

{

while(i--);

}

void KeyDown()

{

GPIO_KEY=0x0f; //First give the upper four bits a low level, then the lower four bits a high level, and then determine which column it is based on which of the lower four bits is not a high level

if(GPIO_KEY!=0x0f)

{

delay(1000); //debounce

if(GPIO_KEY!=0x0f)

{

GPIO_KEY=0x0f; //Reassign value

switch(GPIO_KEY)

{

case 0x07: KeyValue=0;break;

case 0x0b: KeyValue=1;break;

case 0x0d: KeyValue=2;break;

case 0x0e: KeyValue=3;break;

}//Remember the following numbers, 0123

GPIO_KEY=0xf0; //Give the upper four bits a high level and the lower four bits a high level, and determine which row it is based on which of the upper four bits is not a high level

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;

}//Remember the number of lines, +0+4+8+12

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

{

delay(1000);

a++;

}//The condition for the program to jump out of the loop is to release or press it for a long enough time (to avoid continuous CPU occupation)

}

}

}

void main()

{

LSA=0; 

LSB=0;

LSC=0; //Select static digital tube display

while(1)

{

KeyDown();

GPIO_DIG=smgduanxuan[KeyValue]; //Give the static digital tube a displayed value

}

}


#You can achieve verticality by modifying the assignment of KeyValuede.

How to test the effect of hands-free detection

When using the matrix buttons, the buzzer will sound at the same time. Analyze the reasons in detail.


Remember to be rigorous in programming to avoid wasting time due to errors in program input. In this programming process, a semicolon was added at the end of #define, causing the program to keep reporting errors!!!


Reference address:10. Matrix buttons

Previous article:11.8*8LED dot matrix
Next article:9. Independent buttons

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号