51 single chip microcomputer STC89C52 matrix keyboard scanning method detection

Publisher:花开堂前Latest update time:2022-10-08 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Program source code

/*-----------------------Include header file area-------------------------*/

#include  //MCU header file 

/*-----------------------Data type definition area-----------------------*/

typedef unsigned char u8; //define type unsigned char alias u8

typedef unsigned int u16; //define type unsigned int alias u16

/*-----------------------User-defined data area---------------------*/

#define FOSC 11059200L  //System clock

#define Timer_value (65536-(FOSC/12/1000)*1) //Timer value = (timer overflow value - (system clock/12T mode/1000 = timer value for timing 1ms)) * timing time in ms) 

//The common anode digital tube displays the character array, and the corresponding characters are "0123456789ABCDEF-"

code u8 LED_Table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBF,0xFF};

u8 LED_show[8]; //Data array displayed by digital tube

u8 key;  //Get the key value variable

/*-----------------------Port/Pin Definition Area----------------------*/

sbit x1=P1^0; //Port connected to the first row of the matrix

sbit x2=P1^1; //Port connected to the second row of the matrix

sbit x3=P1^2; //Port connected to the third row of the matrix

sbit x4=P1^3; //Port connected to the 4th row of the matrix

sbit y1=P1^4; //Port connected to the first column of the matrix

sbit y2=P1^5; //Port connected to the second column of the matrix

sbit y3=P1^6; //Port connected to the third column of the matrix

sbit y4=P1^7; //Port connected to the 4th column of the matrix

/*-----------------------Function declaration area---------------------------*/ 

void delay(u16 ms); //delay function declaration

void Timer0_Init(void); //Timer 0 configuration function declaration

void LED_Scan(void); //Declaration of digital tube scanning function

u8 Get_KEY_value(void); //Get key value function declaration

/*-----------------------Main function area-----------------------------*/

void main()

{

Timer0_Init(); //Timer 0 initialization

while(1)  // Repeat the while loop program

{

if(Get_KEY_value()!=0) //The returned key value is not equal to 0, which means a key is pressed

{

delay(10); //delay to eliminate jitter

if(Get_KEY_value()!=0) //Confirm whether the key is actually pressed

{

key=Get_KEY_value(); //Get the key value

}

}

else //No key is pressed 

{

key=0; //If no key is pressed, key is 0

}

LED_show[0]=LED_Table[key/10]; //The first digital tube displays the tens digit of the key value

LED_show[1]=LED_Table[key%10]; //The second digital tube displays the unit digit of the key value

}

/*-----------------------Timer 0 interrupt function area--------------------*/

void Timer0()interrupt 1

{

TH0=Timer_value>>8; //Reset the high 8 bits of the timer value

TL0=Timer_value; //Reset the lower 8 bits of the timer value

    LED_Scan(); //Digital tube scanning

}

/*----------------------------------------------------------------

Function name: delay()

Function: Delay

Function parameter: ms is the delay time, the delay range is 0~65535

Return value: None

----------------------------------------------------------------*/

void delay(u16 ms)

{

u8 i;

while(ms--) //loop delay 1ms times

{

for(i=115;i>0;i--); //delay 1ms

}

}

/*----------------------------------------------------------------

    Function Name: Timer0_Init()

    Function: Timer 0 initialization

    Function parameters: None

    Return value: None

----------------------------------------------------------------*/

void Timer0_Init(void)

{

TMOD=0x01; //Set timer 0 to mode 1 (16-bit counting mode)

TH0=Timer_value>>8; //Set the high 8 bits of the timer value

TL0=Timer_value; //Set the lower 8 bits of the timer value

TR0=1; //Timer 0 counter starts timing

ET0=1; //Enable timer 0 interrupt

EA=1; //Open the general interrupt

}

/*----------------------------------------------------------------

Function name: LED_Scan()

Function: Digital tube scanning

Function parameters: None

Return value: None

----------------------------------------------------------------*/

void LED_Scan(void)

{

static u8 i=0;

P0=0xFF; //Display blanking (the blanking level is opposite to the effective level. If the bit selection is enabled first and then the segment selection data is displayed, the blanking is the segment selection, otherwise the blanking is the bit selection)

P2=~(0x01< //Enable the (i+1)th digital tube position selection

P0=LED_show[i]; //Display the (i+1)th digital tube segment selection data

i=(i+1)%2; //Display 2 digital tubes

}

/*----------------------------------------------------------------

Function name: Get_KEY_value()

Function: Get key value

Function parameters: None

Return value: 0 if no key is entered, 1 to 16 if a key is entered

----------------------------------------------------------------*/

u8 Get_KEY_value(void)

{

//Detect the first row of buttons

x1=0; x2=1; x3=1; x4=1;

if(y1==0) return 1;

else if(y2==0) return 2;

else if(y3==0) return 3;

else if(y4==0) return 4;

//Detect the second row of buttons

x1=1; x2=0; x3=1; x4=1;

if(y1==0) return 5;

else if(y2==0) return 6;

else if(y3==0) return 7;

else if(y4==0) return 8;

//Detect the third row of buttons

x1=1; x2=1; x3=0; x4=1;

if(y1==0) return 9;

else if(y2==0) return 10;

else if(y3==0) return 11;

else if(y4==0) return 12;

//Detect the fourth row of buttons

x1=1; x2=1; x3=1; x4=0;

if(y1==0) return 13;

else if(y2==0) return 14;

else if(y3==0) return 15;

else if(y4==0) return 16;

return 0; //No key pressed returns 0

}


Simulation Circuit

insert image description here

Reference address:51 single chip microcomputer STC89C52 matrix keyboard scanning method detection

Previous article:51 single-chip microcomputer STC12C5A60S2 timer is used as delay function, and the timer realizes accurate delay
Next article:51 MCU STC89C52 timer interrupt method to scan digital tube and realize stopwatch

Recommended ReadingLatest update time:2024-11-16 10:34

8051 MCU (STC89C52) realizes serial communication in polling mode
8051 sends 0 ~ 15 to the laptop, and there is a short interval between this transmission and the previous transmission. #include STC89C5xRC.H   void delay() { int i, j; for(i = 0; i 300; i++) for(j = 0; j 300; j++) ; }   void send() { int i; for(i = 0; i = 15; i ++) { delay(); SBUF = i; while(TI == 0);
[Microcontroller]
8051 MCU (STC89C52) realizes serial communication in polling mode
STC89C52+AT24C02 realizes recording of device boot times
1. Project introduction During the use of some equipment, it is necessary to count and record the number of times the equipment is used. This can be used to evaluate the actual service life of equipment, determine maintenance cycles, predict failure risks, etc., and is of great significance to improving the stability
[Microcontroller]
STC89C52+AT24C02 realizes recording of device boot times
STC89C52RC MCU Extra Chapter | 04 - Understanding the header file and the _nop_ function
1 intrins.h header file The header file intrins.h is often used in our daily development of 51 single-chip microcomputers, especially the _nop_() function. The following is the content of intrins.h : /*-------------------------------------------------------------------------- INTRINS.H Intrinsic functions for C51.
[Microcontroller]
STC89C52RC MCU Extra Chapter | 04 - Understanding the header file <intrins.h> and the _nop_ function
STC89C52 interrupt final chapter ---- self-study notes
I. Overview 1.1 Five interrupts External interrupt 0 INT0 Timer/Counter (C/T) Interrupt 0 T0 External interrupt 1 INT1 Timer interrupt 1 T1 Serial communication interrupt RX and TX 1.2. Interrupt Enable Register (IE)   1.3 Interrupt request flag (TCON)   ITO (TCON.0): External interrupt 0 trigger control bit.  
[Microcontroller]
STC89C52 interrupt final chapter ---- self-study notes
51 single chip microcomputer STC89C52 AD analog-to-digital conversion
CSDA BIT P3.2   WRR BIT P3.6   RDD BIT P3.7   BIT HEAT P2.7   GAME BIT P2.6   CSAD BIT P0.7   DIOLA BIT P2.5      ORG 0000H   LOOP0:   MOV P0,#0FFH ; Turn off the digital tube display   SETB FALLS   SETB RDD   SETB WRR   STARTAD: ; Start conversion   CLR CSAD   //NOP   CLR WRR   //NOP   SETB WRR  
[Microcontroller]
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号