51 Microcontroller Basics Matrix Keyboard 4X4

Publisher:和谐共存Latest update time:2022-10-13 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

principle:


16 keyboards can be controlled through eight pins. The upper four bits control the rows and the lower four bits control the columns.

Code:


#include

 

#define SMG P0 //Macro definition digital tube

#define KEY P1 //Macro definition matrix keyboard

 

typedef unsigned char u8;

typedef unsigned int u16;

 

u8 sum; //Define a global variable to save the value of the key

 

u8 SMG_Code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0xff}; //The content displayed by the digital tube

 

void delay(u16 i)

{

while(i--);

}

void KEY_J(void) //Matrix keyboard function

{

u8 a=0; //A local variable is defined

KEY=0x0f; //When I define 0x0f, it means I start column scanning, from high to low

if(KEY!=0x0f) //KEY=0x0f was defined before. If there is a change, it should be that a button has been pressed.

{

delay(1000);//normal debounce

if(KEY!=0x0f)

{

switch(KEY)//This is interesting, because here we need to determine which column of keys is pressed. One side of the key is low level. After the connection, the high level will be pulled low, so the pin will become low level. Find the corresponding column and you will know which column it is.

{

case 0x07:sum=1;break;

case 0x0b:sum=2;break;

case 0x0d:sum=3;break;

case 0x0e:sum=4;break;

}

KEY=0xf0; //Column scanning is completed, now start row scanning

switch(KEY)//In order, the pressed pin will turn to low level to determine which row is pressed.

{

case 0x70:sum=sum;break; //The number obtained in the first row is the column

case 0xb0:sum=sum+4;break;//The number obtained in the second row is the column plus 4, and each subsequent row adds 4 to the number.

case 0xd0:sum=sum+8;break;

case 0xe0:sum=sum+12;break;

}

}

}

//It's okay not to add it, but it will be more stable if it is added.

while((a<50)&&(KEY!=0xf0)) //Detect the key release detection, which means that when you keep pressing this button, the next execution will be performed after 0.05 seconds, or the following will be executed when you release it.

{

delay(100);

a++;

}

}

void main(void)

{

while(1)

{

KEY_J(); //Call matrix keyboard function

SMG=SMG_Code[sum]; //Nigital tube display

}

}


operation result:


Click a corresponding one to display it.

Reference address:51 Microcontroller Basics Matrix Keyboard 4X4

Previous article:51 Microcontroller Basics: Timer Interrupt (1)
Next article:51 Microcontroller Basics External Interrupt (2)

Recommended ReadingLatest update time:2024-11-15 14:39

Proteus user notes 51 single chip microcomputer 4x4 matrix button
The inversion method is used to determine the key coordinates, that is, the row number and column number are used to obtain the key code. After writing it, I found a problem with Proteus: when I directly used the if (P1&0xf0!=0xf0) statement, it could not be called out. When I used an intermediate variable for trans
[Microcontroller]
HOLTEK MCU-4X4 keyboard using software scanning
Function Description: The 4X4 keyboard corresponds to 16 numbers from 0 to F. Pressing any key will display the corresponding number on the common anode seven-segment display. Circuit Description:         Circuit             Port B of the HT46F49E is connected to a common anode seven segment display.      
[Microcontroller]
HOLTEK MCU-4X4 keyboard using software scanning
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号