MCS-51 MCU scanning matrix keyboard program

Publisher:MindfulCreatorLatest update time:2012-08-24 Source: 51heiKeywords:mcs-51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include
#define uint unsigned int
#define uchar unsigned char

 

sbit DSCQ=P2^6; //bit defines the segment latch control terminal
sbit WSCQ=P2^7; //bit definition bit latch control terminal


uchar code table[]={0x3f,0x06,0x5b,0x4f, //0,1,2,3
     0x66,0x6d,0x7d,0x07, //4,5,6,7
     0x7f,0x6f,0x77,0x7c, //8,9,a,b
     0x39,0x5e,0x79,0x71};//c,d,e,f

void delay(uint t); //declare delay function
void keyscan(); //Declare scanning function


void main()
{
  
 DSCQ=0; //Segment latch latch
 WSCQ=0; //bit latch latch

 P0=0x00; //P0 port outputs 0000 0000
 WSCQ=1; //bit latch allows
 WSCQ=0; //Bit latch latch (the ground terminals of the digital tubes are all low level 0)

 
 P0=table[8]; //P0 port output 8
 DSCQ=1; //Segment latch allows

 while(1)
 {

   keyscan(); //constant scan
 
 }
 
}

 

void delay(uint t)
{
 uint x,y;
 for(x=t;x>0;x--)
 {
  for(y=120;y>0;y--)
  {
  

  }
  
 }

}


void keyscan()
{
 uchar key1,key2;
 P3=0xfe; //1111 1110 (set P3.0 to low level 0. Write 1 to the high 4 bits to read the status of the 4 high I/O ports)
 key1=P3; //Read P3 port for the first time
 delay(15); //delay 15ms
 key2=P3; //Read P3 port for the second time
 key1=key1&0xf0;//1111 0000 (keep the high 4 bits of the first value and mask the low 4 bits)
 key2=key2&0xf0;//1111 0000 (keep the upper 4 bits of the second value and mask the lower 4 bits)
 if((key1==key2)&&key1!=0xf0)
//If the value of key1 is equal to the value of key2, and the value of key1 is not equal to 1111 0000, it means that a key is pressed.
 {
  switch(key1)
  {
 
  case 0xe0:P0=table[0];break;//1110 0000
  case 0xd0:P0=table[1];break;//1101 0000
  case 0xb0:P0=table[2];break;//1011 0000
  case 0x70:P0=table[3];break;//0111 0000
  
  }
 
 }


 
 P3=0xfd; //1111 1101 (set P3.1 to low level 0. Write 1 to the upper 4 bits to read the status of the 4 high-bit I/O ports)
 key1=P3; //Read P3 port for the first time
 delay(15); //delay 15ms
 key2=P3; //Read P3 port for the second time
 key1=key1&0xf0; //1111 0000 retain the upper 4 bits (retain the upper 4 bits of the first value and mask the lower 4 bits)
 key2=key2&0xf0; //1111 0000 retain the upper 4 bits (retain the upper 4 bits of the second value and mask the lower 4 bits)
 if((key1==key2)&&key1!=0xf0)
//If the value of key11 is equal to the value of key2, and the value of key1 is not equal to 1111 0000, it means that a key is pressed.
 {
  switch(key1)
  {
 
  case 0xe0:P0=table[4];break;//1110 0000
  case 0xd0:P0=table[5];break;//1101 0000
  case 0xb0:P0=table[6];break;//1011 0000
  case 0x70:P0=table[7];break;//0111 0000
  
  }
 
 }

 P3=0xfb; //1111 1011 (set P3.2 to low level 0. Write 1 to the high 4 bits to read the status of the 4 high I/O ports)
 key1=P3; //Read P3 port for the first time
 delay(15); //delay 15ms
 key2=P3; //Read P3 port for the second time
 key1=key1&0xf0;//1111 0000 (keep the high 4 bits of the first value and mask the low 4 bits)
 key2=key2&0xf0;//1111 0000 (keep the upper 4 bits of the second value and mask the lower 4 bits)
 if((key1==key2)&&key1!=0xf0)
//If the value of key11 is equal to the value of key2, and the value of K1 is not equal to 1111 0000, it indicates that a key is pressed
 {
  switch(key1)
  {
 
  case 0xe0:P0=table[8];break; //1110 0000
  case 0xd0:P0=table[9];break; //1101 0000
  case 0xb0:P0=table[10];break;//1011 0000
  case 0x70:P0=table[11];break;//0111 0000
  
  }
 
 }

 

 P3=0xf7; //1111 0111 (set P3.3 to low level 0. Write 1 to the upper 4 bits to read the status of the 4 high-bit I/O ports)
 key1=P3; //Read P3 port for the first time
 delay(15); //delay 15ms
 key2=P3; //Read P3 port for the second time
 key1=key1&0xf0;//1111 0000 (keep the high 4 bits of the first value and mask the low 4 bits)
 key2=key2&0xf0;//1111 0000 (keep the upper 4 bits of the second value and mask the lower 4 bits)
 if((key1==key2)&&key1!=0xf0)
//If the value of key1 is equal to the value of key2, and the value of key1 is not equal to 1111 0000, it means that a key is pressed.
 {
  switch(key1)
  {
 
  case 0xe0:P0=table[12];break;//1110 0000
  case 0xd0:P0=table[13];break;//1101 0000
  case 0xb0:P0=table[14];break;//1011 0000
  case 0x70:P0=table[15];break;//0111 0000
  
  }
 
 }

}
Keywords:mcs-51 Reference address:MCS-51 MCU scanning matrix keyboard program

Previous article:Make the buzzer sing "Beautiful Girl"
Next article:Summary of Single Chip Microcomputer Delay Program

Recommended ReadingLatest update time:2024-11-16 15:19

Introduction to the characteristics of various types of MCS-51 series microcontrollers
MCS is the series symbol for the microcontrollers produced by Intel, for example, Intel's MCS-48, MCS-51, and MCS-96 series of microcontrollers. The MCS-51 series of microcontrollers was developed by Intel in the early 1980s on the basis of the MCS-48 series. It is the first mainstream variety of microcontrollers that
[Microcontroller]
How many I/O ports does the MCS-51 microcontroller have? What are its components and features?
How many I/O ports does the MCS-51 microcontroller have? Answer: The MCS-51 microcontroller has four bidirectional 8-bit I/O ports, of which P0~P3 are tri-state bidirectional ports. P1, P2, and P3 ports are quasi-bidirectional ports (when used as input, the port line is pulled to a high level, so they are called q
[Microcontroller]
Design of Interface between MCS-51 and Serial D/A Converter
1. Overview In microcomputer-controlled electrical equipment, it is often necessary to convert digital signals into analog signals to drive the operation of electrical equipment. In this process, D/A conversion is a very important link and an important component of the microcomputer control system. General D/A c
[Microcontroller]
Design of Interface between MCS-51 and Serial D/A Converter
The general format of an MCS-51 assembly language program
Unlike 8086, there is no distinction between code segment and data segment, and all addresses are unified.  A program consists of command lines or comment lines, and each command line can only contain one command. "Label:" can be used before an instruction to indicate the instruction address for easy reference in th
[Microcontroller]
Introduction to the CPU of MCS-51 single-chip microcomputer
The central processing unit (CPU) of the MCS-51 microcontroller is composed of an arithmetic unit and control logic, including several special function registers (SFRs). 1. ALU-centered arithmetic unit The arithmetic logic unit ALU can perform arithmetic operations such as addition, subtraction, multiplication,
[Microcontroller]
Introduction to the CPU of MCS-51 single-chip microcomputer
Hardware Design of Serial Communication between PC and Multiple MCS-51 Single-Chip Microcomputers
1 Introduction In measurement and control systems and engineering applications, it is common to encounter situations where multiple tasks need to be executed simultaneously, so the master-slave multi-machine distributed system has become a widely used model in modern industry. Most of them are composed of IBM-PC micr
[Microcontroller]
Hardware Design of Serial Communication between PC and Multiple MCS-51 Single-Chip Microcomputers
An article explaining the interrupt system of MCS-51 microcontroller in detail
MCS-51 interrupt system: 5 interrupt sources (two external interrupts, two timers, one serial port), 2 priorities Interrupt related concepts Interrupt: When the CPU is processing something, an emergency event that occurs outside or inside the microcontroller requires the CPU to handle it immediately. Therefore, the
[Microcontroller]
An article explaining the interrupt system of MCS-51 microcontroller in detail
The structure of the MCS-51 interrupt system
The interrupt system of the 8031 ​​microcontroller is simple and practical. Its basic characteristics are: there are 5 fixed maskable interrupt sources, 3 on-chip and 2 off-chip. They each have a fixed interrupt entry address in the program memory, thereby entering the interrupt service program; the 5 interrupt source
[Microcontroller]
The structure of the MCS-51 interrupt system
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号