Key program design can handle accelerator keys, one-key multi-function, etc.

Publisher:psi33Latest update time:2016-03-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
My key program design can handle accelerator keys, one-key multi-function, etc. 
There is a P1 port determinant keyboard program example at the back.
 xwj  posted on  2005-4-15  16:36  Talking about single-chip microcomputers  ← Return to the forum    Report this post 

Hehe
Seeing the forum discussing keys, I also talked about my ideas for designing key programs. Welcome to criticize.

1. Generally, there is no delay in the keyboard program, but the keys are scanned by calling at intervals (such as 10 milliseconds) to release CPU resources as much as possible;
2. All keyboard programs are basically in one mode, which can handle accelerator keys, one-key multi-function, etc.;
3. There are only 2 external interfaces. To change to a keyboard with different hardware, you only need to add the keyboard program to the project and compile it. No other changes are required. It is very convenient.


Basically, it is a pattern:
Is there a key? - YES - the same as last time? - No - Keyold = 0; Exit 
       |
      YES - Keyold++;
       |
      Keyold == first key delay KEYDELAY? - No - Exit
       |
      YES - Keyok = 1;      // Press and hold for a certain period of time to send the first key, and then send the acceleration key
             Save the key value to Keydata; (decode if necessary)
             Keyold = KEYDELAY-KEYLOOP (delay for pressing and holding); - Exit
  |
  No - Keyold = 0? - YES - Exit
            |
           N0 - Keyok = 1;     // Not 0 means that the key was pressed and then released, of course, it is treated as a valid key and
                 saved to Keydata; (decode if necessary)
                 Keyold = 0; - Exit

All keyboard program external interfaces:
unsigned  char  KeyGetcode (void)         // Return key code
{
    key_ok = 0;
    return (key_data);
}

bit  KeyTest (void)             // Check if there is a key
{
    KeyScan ();             //Key scanreturn
    (key_ok);
}
     
 



//--------------------------------------------------------------------------//
//                                 Source program open to the public                               //
//                     (c)  Copyright  2001-2005  xuwenjun                      //
//                             All  Rights  Reserved                            //
//                                     V1.00                                  //
//--------------------------------------------------------------------------//
//Title:  P1 port determinant keyboard program                                                 //
//File name:  xwj_hlkey.c                                                        //
//Version:  V1.00                                                              //
//Modifier:  Xu WenjunE                          -mail:xuwenjun@21cn.com            //
//Date:  05-05-13                                                           //
//Description:  P1 port determinant keyboard program                                                 //
//Statement:                                                                    //
//         The following code is only provided free of charge for learning purposes, but the source must be stated in the file after quotation or modification.  //
//         Please contact the author for commercial use.     E-mail:xuwenjun@21cn.com            //
//         If you have any questions, please mailto  xuwenjun@21cn.com  and feel free   to communicate with me!                   //
//--------------------------------------------------------------------------//
//Old version:  No                              old version file name:                       //
//Created by:  Xu Wenjun                          E-mail: xuwenjun@21cn.com            //
//Date:  05-05-13                                                           //
//--------------------------------------------------------------------------//
#i nclude 
#i nclude  "xwj_hlkey.h"                 //   P1 port column keyboard   //
//#i nclude  "xwj_bp.h"                 //Peak buzzer

#define  KEYDELAY  25     
#define  KEYLOOP  10     
unsigned  char  key_bak;
unsigned  char  key_temp;
unsigned  char  key_old;
unsigned  char  key_data;
bit  key_ok;

    //   P1 port column keyboard   //
#define  KEYPIN_L   P1                //  Define the key scan column port as  the low four-bit input of P1   //
#define  KEYPIN_H   P1               //  Define the key scan row port as  the high four-bit scan output of P1   //

    //Internal private function   //
unsigned  char  fnKeycode(unsigned  char  key);      //   Output sequential key values ​​according to the keyboard mapping table             //

    //   P1 port column keyboard   //
//---------------------------------------------------------------------------//
void  KeyScan(void)                     //key scan
    {
    unsigned  char  sccode,recode;
    KEYPIN_L  KEYPIN_L|0x0f;                     //   P1 low four bits are column line input          //
    KEYPIN_H  KEYPIN_H&0x0f;                     //   P1 high four bits are row line all zero scan code  //
    if  ((KEYPIN_L&0x0f)  !=   0x0f)     //A key is pressed
        {
        key_temp  key value;              //Key scan, get key valueif
        key_temp  ==  key_bak)
            {
            key_old++;
            if  (key_old==KEYDELAY)                 //Continuous key, if not, shield this if
                {
                key_ok=1;
                key_data=fnKeycode(key_temp);     //Key decodingkey_old
                =KEYDELAY-KEYLOOP;
            }
        }
        else
            {
            key_old=0;
            key_bak=key_temp;
        }
    }
    else                             //Key lifted
        {
        if  (key_old)
            {
            key_ok=1;
            key_data=fnKeycode(key_temp);     //key decoding

        }
        key_old=0;
    }
    KEYPIN_H  KEYPIN_H|0xf0;
}

//---------------------------------------------------------------------------//

unsigned  char  KeyGetcode(void)         //return key code
{
    key_ok=0;
    return(key_data);
}

bit  KeyTest(void)                 //Check if there is a key
{
    KeyScan();             //Key scan
    return(key_ok);
}
Reference address:Key program design can handle accelerator keys, one-key multi-function, etc.

Previous article:C51 program for single chip microcomputer to drive standard PC keyboard
Next article:Traffic light C language program based on AT89C52 single chip microcomputer

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号