key.c

来自「这是一个为51系列单片机开发的4*4键盘驱动程序。该驱动程序包括2个文件」· C语言 代码 · 共 174 行

C
174
字号
#pragma OT(8,speed)
#include "reg51.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','2','6','7','8','3','a','b','c','4','e','f','g'};
//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,input)
//		 |	  |	   |	|
//  0----|----|----|----|--
//	1----|----|----|----|--
//	2----|----|----|----|--
//	3----|----|----|----|--
//(output) 
unsigned char Key_Check(void);
unsigned char Key_Read(void);
unsigned char Key_Check(void)
{
	unsigned char key;
#pragma asm
	push	acc
	mov		a,r0
	push	acc
	mov		Key_Pin,#0fh
	mov		a,Key_Pin
	cjne	a,#0fh,Key_Down
	sjmp	Key_End
Key_Down:
	call	Key_Wait
	mov		a,Key_Pin
	cjne	a,#0fh,Key_keycheck
	sjmp	Key_End
Key_keycheck:
	mov		r0,#00h
	mov		a,#0f7h
Key_checkline:
	rl		a
	push	acc
	mov		Key_Pin,a
	mov		a,Key_Pin
	anl		a,#0fh
	cjne	a,#0fh,Key_lineend
	mov		a,r0
	add		a,#04h
	mov		r0,a
	pop		acc
	jmp		Key_checkline
Key_lineend:
	pop		acc
	mov		Key_Pin,#0fh
	mov		a,Key_Pin
Key_checkcol:
	jnb		acc.0,Key_colend
	push	acc
	mov		a,r0
	add		a,#1h
	mov		r0,a
	pop		acc
	rr		a
	sjmp	Key_checkcol
Key_colend:
	mov		a,r0
	sjmp	Key_Out
Key_End:
	mov		key?040,#0ffh
	jmp		Key_CheckEnd
Key_Out:
	push	dpl
	push	dph
	mov		dptr,#Key_Table
	movc	a,@a+dptr
	pop		dph
	pop		dpl
	mov		key?040,a
Key_CheckEnd:
	pop		acc
	mov		r0,a
	pop		acc
#pragma endasm	
	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 Key_Wait(void)
{
#pragma asm
	push	acc
	mov		a,r7
	push	acc
	mov		a,r6
	push	acc
	mov		r7,#01ah
Key_Loop1:
	mov		r6,#0ffh
Key_Loop2:
	nop
	djnz	r6,Key_Loop2
	djnz	r7,Key_Loop1
	pop		acc
	mov		r6,a
	pop		acc
	mov		r7,a
	pop		acc
#pragma endasm	
}

⌨️ 快捷键说明

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