4*4 matrix keyboard C51 program

Publisher:painterLatest update time:2016-10-26 Source: eefocusKeywords:4*4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include
#include
#define key_port P0 //Keyboard interface definition
sbit key_port_0=key_port^0;
sbit key_port_1=key_port^1;
sbit key_port_2=key_port^2;
sbit key_port_3=key_port^3;
/*******************************
STC89C59 MCU one millisecond delay function
*******************************/
void delay_ms(unsigned int ms)    
{
    unsigned int i,j;
  for( i=0;i   for(j=0;j<332;j++); //1947 is the value obtained by software simulation and repeated experiments under the 22.1184MHz crystal oscillator of STC89C58
}
/*****************************
   The serial port sends a character 
**************************/
void com_send_dat( unsigned char dat) 

    SBUF=dat; 
 while (TI== 0); 
 TI= 0 ; 

/******************************
   Serial port initialization 
******************************/
void init_com( void ) 

 SCON=0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr //UART is mode 1, 8-bit data, allow reception
    TMOD|=0x20 ; //TMOD: timer 1, mode 2, 8-bit reload //Timer 1 is mode 2, 8-bit automatic reload
 TH1=0xfa ; //Baud:19200 fosc="22.1184MHz 
 TL1=0xfa;
 PCON|=0x80; //SMOD=1; double the baud rate;   
 ES=1; //Enable Serial Interrupt 
   TR1 = 1 ; // timer 1 run 

/**************************
   Keyboard scanning function
*****************************/
unsigned char keyscan(void)
{  
 unsigned char key,i;
    unsigned char co

de key_table[16]=
 {0xee,0xed,0xeb,0xe7,0xde,0xdd,0xdb,0xd7,0xbe,0xbd,0xbb,0xb7,0x7e,0x7d,0x7b,0x77};
    key_port=0x0f; //Determine the row and column position
    if(key_port==0x0f)return(0);//Return to 0 if no key is pressed
    delay_ms(10); //Call the delay function to remove the front key jitter.
    if(key_port==0x0f)return(0);//Judge again The purpose is to ensure correct detection
    else
    { 
  for(i=0;i<4;i++) //The following is the classic calculation of key value (to determine the location of the closed key)
        { 
   P0=_cror_(0x7f,i);
     if(key_port_0==0)break;
           if(key_port_1==0)break;
           if(key_port_2==0)break;
           if(key_port_3==0)break;
        }  
     key=key_port; //Get the key value
  for(;key_port!=0x0f; key_port=0x0f); //Wait for the key to be released, the purpose is to remove the back edge key jitter
  for(i=0;key_table[i]!=key && i<16;i++); //Look up the table to get the value of key 0-F
  key=i;
     return(key); //Return to the main calling function with key value
   }
}
/*****************************
   Keyboard scan test main function
**************************/
void main(void)
{
 unsigned char key;
 init_com(); //Serial port initialization 
 while(1)
 {
  key=keyscan();
  if(key!=0)
  {
  com_send_dat(key);
  delay_ms(200);
  }
 }
}

Keywords:4*4 Reference address:4*4 matrix keyboard C51 program

Previous article:80C51 Serial Expansion Technology
Next article:C51 simulates SPI interface

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号