STC89C52 MCU matrix keyboard experiment

Publisher:温馨小筑Latest update time:2016-04-15 Source: eefocusKeywords:STC89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
 ///////////////////////////////////////////////////////////////////////////

 Function implemented:  Complete the test of independent buttons. When one of the twenty buttons key5-key20 is pressed,
            the specific value will be displayed on the first digital tube of the development board. The displayed value is
   defined as follows: when key5 is pressed, 0 is displayed, when key6 is pressed, 2 is displayed, and so on,
   to achieve the function of pressing the corresponding value to display the corresponding value.
 Experimental board model: BS-XYD-C52
 Experiment name:  Static digital tube
 Author:  Xie Yingdong
 Writing date:  2012-4-26       
/// ...

#include
#include  
                    
#define uchar unsigned char  //定义unsigned char 为 uchar
#define uint  unsigned int

sbit Duan = P2^6;      //Define the segment selection enable terminal of the digital tube
sbit Wei = P2^7;        //Define the bit selection enable terminal of the digital tube

#define Digital_tube_Wei_Enable Wei=1;   //Turn on the bit select enable terminal for controlling the digital tube
#define Digital_tube_Wei_Disable Wei=0;     //Turn off the bit select enable terminal for controlling the digital tube

#define Digital_tube_Duan_Enable Duan=1;  //Turn on the segment selection enable terminal for controlling the digital tube
#define Digital_tube_Duan_Disable Duan=0;  //Turn off the segment selection enable terminal for controlling the digital tube

#define Digital_tube_Duan P0    //Define the digital tube data port

#define KEY_DOWN  0
#define Keyport P3

uchar code Dis_table[]=       //Convert BCD code into array of digital tube scan code
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,
                                                           0x79,0x71};
uchar code Dis_Position[]=   //Define array of digital tube position selection
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
 ///
...


         
   
​ 
​ This is done in the same way , so if you change the frequency of the crystal oscillator , please
         make corresponding changes
 / ...


 
 
 
    
    
 

 

Function name: One_DigitalTube_displayFunction
: Complete the specified display of the digital tube on the experimental board, that is, display a specific number on a specific digital tube, such as
         displaying 0 on the first digital tube.
Parameter introduction: uData: BCD code array of the number to be displayed
         uNumber: Select which digital tube to display, that is, let a specific digital tube display
Return value:  None
Note: The digital tube on the experimental board is a common cathode digital tube. If you use a common anode digital tube, be careful not
         to reverse it
 /// ...

void One_DigitalTube_display(uchar uData,uchar uNumber)
{
 Digital_tube_Duan=Dis_Position[uNumber]; //Light up a specific digital tube
 Digital_tube_Wei_Enable;      //Enable the bit selection of the digital tube
 Digital_tube_Wei_Disable;      //Disable the bit selection of the digital tube
 DelayMs(5);         //Adjust the timing to achieve stable display
 
 Digital_tube_Duan_Enable;      //Enable the segment selection of the digital tube
 Digital_tube_Duan=Dis_table[uData]; //Enter the value to be displayed
 Digital_tube_Duan_Disable;      //Disable the segment selection of the digital tube
}
 ///
...

         
   

​ 

 

void Scan_Keyboard()
{
 uchar cTemp_Value;
 //Scan the first column of the matrix keyboardKeyport
 =0xef;              //Assign a specific value to the keyboard port to achieve the following detectioncTemp_Value
 =Keyport;   //Assign the keyboard port value to a temporary variableif
 (cTemp_Value!=0xef)
 {
  DelayMs(10);       //Eliminate jitter, that is, eliminate interferencecTemp_Value
       =Keyport;  //Reassign to temporary valueif
       (cTemp_Value!=0xef)   //Check againwhile
       ((Keyport&0x0F)!=0x0F)  //Judge again whether a key is pressed
  {
     switch(cTemp_Value)     
     {
       case 0xee:
        One_DigitalTube_display(0,0);break;  //The fifth key is pressed, display 0
       case 0xed:
        One_DigitalTube_display(4,0);break;  //The ninth key is pressed, display 4
       case 0xeb:
        One_DigitalTube_display(8,0);break;  //The thirteenth button is pressed, and 8 is displayed
       case 0xe7:
        One_DigitalTube_display(12,0);break;  //The seventeenth button is pressed, and C is displayed
     }
  }
 }
 //Scan the second column of the matrix keyboard
 Keyport=0xdf;              //Assign a specific value to the keyboard port to achieve the following detection
 cTemp_Value=Keyport;   //Assign the keyboard port value to a temporary variable
 if(cTemp_Value!=0xdf)
 {
  DelayMs(10);       //Eliminate jitter, that is, eliminate interference
       cTemp_Value=Keyport;  //Reassign to the temporary value
       if(cTemp_Value!=0xdf)   //Check again
       while((Keyport&0x0F)!=0x0F)   //Judge again whether a button is pressed
  {
     switch(cTemp_Value)     
     {
       case 0xde:
        One_DigitalTube_display(1,0);break;  //The sixth button is pressed, and 1 is displayed
       case 0xdd:
        One_DigitalTube_display(5,0);break;  //The tenth key is pressed, and 5 is displayed
       case 0xdb:
        One_DigitalTube_display(9,0);break;  //The fourteenth key is pressed, and 9 is displayed
       case 0xd7:
        One_DigitalTube_display(13,0);break;  //The eighteenth key is pressed, and D is displayed
     }
  }
 }
 //Scan the third column of the matrix
 keyboardKeyport=0xbf;              //Assign a specific value to the keyboard port to achieve the following detection
 cTemp_Value=Keyport;   //Assign the keyboard port value to a temporary variableif
 (cTemp_Value!=0xbf)
 {
  DelayMs(10);       //Eliminate jitter, that is, eliminate interferencecTemp_Value
       =Keyport;  //Reassign to the temporary valueif
       (cTemp_Value!=0xbf)   //Check againwhile
       ((Keyport&0x0F)!=0x0F)   //Judge whether a key is pressed again
  {
     switch(cTemp_Value)     
     {
       case 0xbe:
        One_DigitalTube_display(2,0);break;  //The seventh button is pressed, and the display shows 2
       case 0xbd:
        One_DigitalTube_display(6,0);break;  //The eleventh button is pressed, and the display shows 6
       case 0xbb:
        One_DigitalTube_display(10,0);break;  //The fifteenth button is pressed, and the display shows A
       case 0xb7:
        One_DigitalTube_display(14,0);break;  //The nineteenth button is pressed, and the display shows E
     }
  }
 }
 //Scan the fourth column of the matrix keyboard
 Keyport=0x7f;              //Assign specific values ​​to the keyboard port to implement subsequent detection
 cTemp_Value=Keyport;   //Assign the keyboard port value to the temporary variable
 if(cTemp_Value!=0x7f)
 {
  DelayMs(10);       //Eliminate jitter, that is, eliminate interference
       cTemp_Value=Keyport;  //Reassign to the temporary value
       if(cTemp_Value!=0x7f)   //Check again
       while((Keyport&0x0F)!=0x0F)   //Judge again whether a key is pressed
  {
     switch(cTemp_Value)     
     {
       case 0x7e:
        One_DigitalTube_display(3,0);break;  //The eighth key is pressed, and 3 is displayed
       case 0x7d:
        One_DigitalTube_display(7,0);break;  //The twelfth key is pressed, and 7 is displayed
       case 0x7b:
        One_DigitalTube_display(11,0);break;  //The sixteenth key is pressed, and B is displayed
       case 0x77:
        One_DigitalTube_display(15,0);break;  //The 20th button is pressed, and F is displayed
     }
  }
 }
}
/// ...

Function name: mainFunction
function: Continuously scan the keyboard in a loop. When a key is pressed, the corresponding value is displayed on the digital tube
Parameter introduction: None
Return value:  None
Notes: None
/ ...

void main()
{
 DelayMs(50);
 while(1)
 {
  Scan_Keyboard();    //Call keyboard scanning function
 }
}

Keywords:STC89C52 Reference address:STC89C52 MCU matrix keyboard experiment

Previous article:STC89C52 MCU external interrupt 0 experiment
Next article:STC89C52 MCU key experiment

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号