51 MCU long key test program

Publisher:心境恬淡Latest update time:2016-09-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include
#include
/*
Program function: This program is used to test the 4X4 matrix keyboard; and has the ability to long press test.
            When a key is pressed, the pressed key number is displayed on LCD1602, such as: K1 K2.... K16;
            When a key is pressed for more than 1S, a key value is returned every 250MS, realizing the function of long pressing.
            At the same time, the key value is sent to the port through the serial port for display; 
Program Description: This is a traditional method of using delay to scan keys, drive keys and test programs; this method is
          It is not used in actual project programming. It is used in teaching when students are just beginning to learn microcontrollers.
Note: In this program, only the long press of K4 is displayed. If you want to handle the long press of other keys, just add
      Just handle the relevant ones.
*/
/*
Matrix keyboard pin definition
*/
sbit keyin4 = P2^7;
sbit keyin3 = P2^6;
sbit keyin2 = P2^5;
sbit keyin1 = P2^4;
sbit keyout1 = P2^3;
sbit keyout2 = P2^2;
sbit keyout3 = P2^1;
sbit keyout4 = P2^0;
 
//Macro definition
#define TH0_NUM 1000 //Easy to transplant the program;
#define TL0_NUM 1000
 
bit Flag1s, Flag250Ms, Flag2Ms; //Time reached flag
 
//lcd1602 display array
unsigned char keynum[]="keynum:k ";
unsigned char keycount[3]="000";
 
//Key value query array
                         //k1,k2,k3,k4,k5,k6,k7,k8,
unsigned char code keytabel[]={0xe7,0xd7,0xb7,0x77,0xeb,0xdb,0xbb,0x7b,
                         //k9,k10,k11,k12,k13,k14,k15,k16,
                          0xed,0xdd,0xbd,0x7d,0xee,0xde,0xbe,0x7e};
/*
External function declaration
*/
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
void ConfigUART(unsigned int baud); 
void DelayMs(unsigned int ms);
unsigned char keyscan();
 
void InitTimer0();
 
/*
Main Program
*/
void main()
{
 unsigned char i,j,keytemp=0;
 InitTimer0();
 ConfigUART(9600);
InitLcd1602();
LcdShowStr(0,0,keynum);
while(1)
    {
     keytemp=keyscan();
     if(keytemp!=0) //Judge whether a key is pressed; 
      {       
       for(i=0;i<16;i++) //When a key is pressed, look up the table to get the key value; 
       {
        if(keytemp == keytabel[i])
        break;
       }
         
       keynum[8]=(i+1)/10+0x30;
       keynum[9]=(i+1)%10+0x30;
       TI=1;
       SBUF=keytemp;
       while(TI!=1);
       TI=0;
       LcdShowStr(0,0,keynum);
       if(keytemp==0x77) //Used to detect long press of up key K4, and display on the second line of LCD1602
           { //Add 1 every 250MS;
           j++; 
           if(j>254)
           j=0;
           keycount[0] = j/100%10+0x30;
           keycount[1] = j/10%10+0x30;
           keycount[2] = j%10+0x30; 
          
           TI=1; //Used for serial port, print plus 1 every 250MS;
           printf("key is long pushing %s \n",keycount); 
           while(TI!=1);
            TI=0; 
           }
        LcdShowStr(0,1,keycount);
 
      }
    }
}
/*
Function: N milliseconds delay
*/
void DelayMs(unsigned int ms)
{
unsigned int i,j;
for(i=0;i
for(j=0;j<113;j++);
 
}
/*
Function: Matrix keyboard scanning (with long key detection function)
*/
unsigned char keyscan()
{
static unsigned char num1,temp;
    P2 = 0xf0;
    num1=P2;
    if(num1!=0xf0)
      {
      DelayMs(50);
      num1=P2; 
      if(num1!=0xf0)
           {
            TR0=1; //Turn on the timer
            temp=num1;//
            P2 = 0x0f;
            DelayMs(1);
            num1=P2;    
            temp =temp|num1;
            do
             {
                P2 = 0xf0;
                num1=P2;
                if(Flag1s == 1) // Check if the key is pressed for 1S; 
             {
                    if(Flag250Ms==1) //If you don't release the key within 1S, it means you have long pressed a key; then return every 250MS
 
                        { //One key value; this implements the function of long pressing a key
                         Flag250Ms=0;
                        return temp;
                 }
             }
             }
            while(num1!=0xf0);      
         }
        else
        {
         TR0=0; //If not pressed, turn off the timer
         Flag1s=0;
         Flag250Ms=0;
         temp=0;
         }      
      }
    else
       {     
        TR0=0;
        Flag1s=0;
        Flag250Ms=0;
        temp = 0;
        }
    TR0=0; //When you let go, you should also turn off the timer and clear several flags; 
    Flag1s=0;
    Flag250Ms=0;
    return temp;      
}
    
    
    /*
    Function: Initialize timer 0 and set it to interrupt once every 1MS
    */
    
    void InitTimer0()
    {
        TMOD &=0xf0; //This operation mode will not destroy other settings of the register;
        TMOD |= 0x01; //Mode 1, 16-bit timer
        TH0/=(65536-TH0_NUM)/256;
        TL0=(65536-TL0_NUM)%256;
        EA=1;
        ET0=1;
        TR0=0; 
    }
    /*
    Function: Timer 0 interrupt service routine
    */
    void Timer0_Interrupt() interrupt 1
    {
        static unsigned char i,j,k;
        TH0=(65536-TH0_NUM)/256;
        TL0=(65536-TL0_NUM)%256;
        i++;j++;
        if(i>2)
         {
            i=0;
            Flag2Ms=1;
         }
        if(j==250)
         {
            j=0;
            Flag250Ms=1;
            k++;
            if(k==4)
             {
                k=0;
                Flag1s=1;
             }
         }
}
Reference address:51 MCU long key test program

Previous article:Design and implementation of liquid level monitoring instrument using multiple single chip microcomputers
Next article:51 MCU 4X4 matrix keyboard driver and application (C language)

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号