⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key.c

📁 这是一个为51系列单片机开发的4*4键盘驱动程序。该驱动程序包括2个文件
💻 C
字号:

#pragma OT(8,speed)
#include "reg.h" 

//----Key scaning module----
//Designed by QTS.
//2005,8


//----Module Description----
//This is a 4*4 key array scaning module. It can return the code of the key with or without a wait delay.
//User can define the I/O pins linked to the key array and the code of the key.
//Shakeproof function is included in the scaning process.


//----Module Usage----
//To use this module, the user needs to add this file to the project and set the option for this file as follows:
//Check "Generate Asembler SRC File" and "Assemble SRC File".
//Check out "Link Publics Only".
//Also the user should include key.h in the calling C source file.


//----Module Includes----
//(func) unsigned char Key_Check(void): function returns  the current key code without delaying.
//                                      Return 0xff for an empty key.
//(func) unsigned char Key_Read(void): function returns the key code. Wait when a key has been pressed.
//Notes: Key_Check is used in the case that other checking tasks need to be deal during the same loop,
//       The user needs to write a checking loop to check the key and other flags.
//       A typital example is showed bellow:
//       ------------------------------------
//	     unsigned char c;
//       bit	push=0;
//       while(1)
//       {
//           c=Key_Check();
//           if(c!=0xff&&push==0)
//           {
//                *deal the key.
//                push=1;
//           }
//           else if(c==255&&push==1)
//               push=0;
//       }
//       ------------------------------------
//       Key_Read is used when no other checking tasks are needed or the user wants the program to pause
//       and waits for a key press. It can be called directly in the program.
//       **Attention to another small difference between the two key checking functions:
//       If you use Key_Check, the key dealing function will be immediately called when you push the key;
//       while Key_Read will wait until you release the key. 




//----Configuration----
code char Key_Table[]=
{
    1,2,3,4,5,6,7,8,9,0,10,11,12,13,14,15 
}
;

//Key_Table[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
//	1	2	3	4
//	5	6	7	8
//	9	10	11	12
//	13	14	15	16
// This module allow the user to define the key code. The key code can be any value except 0xff
// because 0xff is the default value for an empty key.
#define Key_Pin P1 
//User can choose any I/O port to link to the key array.
//When having selected the port, the user should pull up the 4 column bits in the circurt.
//The high 4 bits should be linked to the column.
//		 5	  6	   7	8   (pull up,input)
//	|  |	   |	|
//      0----|----|----|----|--
//	1----|----|----|----|--
//	2----|----|----|----|--
//	3----|----|----|----|--
//(output) 
unsigned char Key_Check(void);
unsigned char Key_Read(void);
void Key_Wait(void);
unsigned char Key_Check(void)
{
    unsigned char key ;
    unsigned char r0,a ;
    Key_Pin=0x0f ;
    if(Key_Pin!=0x0f)
    {
        Key_Wait();
        if(Key_Pin!=0x0f)
        {
            r0=0x00 ;
            a=0xf7 ;
            while(1)
            {
                a=a<<1 ;
                a|=0x01 ;
                Key_Pin=a ;
                if((Key_Pin&0x0f)!=0x0f)
                break ;
                r0+=4 ;
            }
            Key_Pin=0x0f ;
            a=Key_Pin ;
            while(1)
            {
                if(!(a&0x01))
                break ;
                r0++;
                a=a>>1 ;
            }
            return(Key_Table[r0]);
        }
        else 
        key=0xff ;
    }
    else 
    key=0xff ;
    return(key);
}

unsigned char Key_Read(void)
{
    unsigned char key,key1 ;
    while((key=Key_Check())==0xff)
    ;
    while((key1=Key_Check())!=0xff)
    ;
    return(key);
}

//Wait about 20ms
void Delay_50us(unsigned int t)
{
    unsigned char j ;
    for(;t>0;t--)
    for(j=19;j>0;j--)
    ;
}

void Key_Wait(void)
{
    Delay_50us(400);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -