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

📄 keypad.c

📁 一个通用嵌入式产品按键处理程序
💻 C
字号:
/*
*******************************************************************************
*                     (c) Copyright 2008 
*                             All Rights Reserved
*
*                                   Version V1.00
*
* Product Number: 
* Compiler Tool : 
* MCU		: 
* File 		: Keypad.c
* By   		: Tom.Yin
* Date 		: 
*******************************************************************************
*/

/*
*******************************************************************************
* 			          INCLUDE FILE
*******************************************************************************
*/
#include "Include.h"


/*
*******************************************************************************
*				      GLOBAL 
*******************************************************************************
*/
unsigned char Key_Any;


/*
*******************************************************************************
* 		             LOCAL DEFINE
*******************************************************************************
*/
#define KEYPAD_DEBOUNCE_DELAY			2

/*
*******************************************************************************
* 		             LOCAL PARAMETER
*******************************************************************************
*/
static unsigned char Got_Key;
static unsigned char Pressed_Key_Counter;
static unsigned char Key_Debounce_Delay;
static unsigned char Keys;
static unsigned char Key_Old_Scaned;
static unsigned char Key_Save_Scaned;

/*
*******************************************************************************
* 		            FUNCTION DECLARE
*******************************************************************************
*/
void Init_Keypad(void);
void Clear_Keys(void);
void Process_Keypad_Events_1(void);
void Pressed_Key_count(void);
void Process_Keypad_Events(void);

unsigned char Is_Key_Hold(char Key_Index);
unsigned char Is_Any_Key_Hold(void);
unsigned char Is_Key_Pressed(char Key_Index);


/*
*******************************************************************************
*                             Init_Keypad
* Description: Inital Keypad process
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Init_Keypad(void)
{
	Init_Key_Scan();

	Got_Key				= 0;
	Key_Any				= 0;

	Key_Old_Scaned		= 0;
	Keys				= 0;

	Key_Debounce_Delay 	= 0;
	Pressed_Key_Counter	= 0;
}

/*
*******************************************************************************
*                             Is_Keypad_Changed
* Description: Any Key Status Changed, then get a change Information 
*
* Arguments  : None
*
* Returns    : Changed? Y return true, else return false
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
static unsigned char  Is_Keypad_Changed(void)
{
	if (Key_Old_Scaned != Key_Scaned)
	{
		return TRUE;
	}

	return FALSE;
}

/*
*******************************************************************************
*                             Get_Pressed_Keys
* Description: Count pressed keys
*
* Arguments  : None
*
* Returns    : How many Keys hold now.
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
static unsigned char Get_Pressed_Keys(unsigned char key)
{
	unsigned char keys;

	keys = 0;

	if (key & 1) keys ++;
	if (key & 2) keys ++;
	if (key & 4) keys ++;
	if (key & 8) keys ++;
	if (key & 16) keys ++;
	if (key & 32) keys ++;
	if (key & 64) keys ++;
	if (key & 128) keys ++;

	return keys;
}

/*
*******************************************************************************
*                             Process_Keypad_Events_1
* Description: Key Debounce Process
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Process_Keypad_Events_1(void)  
{
	Tick_Timer(Key_Debounce_Delay);

	Get_Key_Scan_Status();

	Key_Any = 0;
	Keys	= 0;

	if (Is_Keypad_Changed())
	{
		Key_Old_Scaned 		= Key_Scaned;
		Key_Debounce_Delay	= KEYPAD_DEBOUNCE_DELAY;
		return;
	}

	if (Key_Debounce_Delay)
	{
		return;
	}

	if (Has_New_Key_Pressed())
	{
		if (Got_Key)
		{
			// append fast key process wait...
			if (Key_Save_Scaned != Key_Scaned )
			{
				Got_Key			= 0;
			}
		}    
		else
		{
			unsigned char Keys_Pressed;

			Key_Any			= 1;
			Got_Key			= 1;
			Keys_Pressed	= 0;
			Keys			= Key_Scaned;
			Keys_Pressed 	+= Get_Pressed_Keys(Keys);
		}
		
		Key_Save_Scaned = Key_Scaned;
	} 
	else 
	{
		Got_Key 		= 0;
	}
}

/*
*******************************************************************************
*                             Pressed_Key_count
* Description: After key debounce process, count how many keys pressed
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Pressed_Key_count(void)
{
	unsigned char key_count;
	unsigned char key_count_bak;

	key_count_bak	= Pressed_Key_Counter;
	key_count		= 0; 

	key_count		+= Get_Pressed_Keys(Key_Scaned);
	
	if (key_count_bak > 1)
	{
		if (key_count > 0)
		{
			Pressed_Key_Counter = 2;
		}
		else
		{
			Pressed_Key_Counter = 0;
		}
	}
	else
	{
		Pressed_Key_Counter = key_count;
	}
}

/*
*******************************************************************************
*                             Process_Keypad_Events
* Description: Keypad Event Process
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Process_Keypad_Events(void)
{
	Process_Keypad_Events_1();
	Pressed_Key_count();
}

/*
*******************************************************************************
*                             Is_Key_Hold
* Description: appoint key hold now?
*
* Arguments  : Key_Index--------------appoint key(KI_UP...KI_BACK)
*
* Returns    : hold return true, else return false
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
unsigned char Is_Key_Hold(char key_index)
{
	unsigned char baddr;

	baddr = key_index & 0x07;

	return (Key_Scaned & (1 << baddr)) != 0;
}

/*
*******************************************************************************
*                             Is_Any_Key_Hold
* Description: any key hold now?
*
* Arguments  : None
*
* Returns    : hold return true, else return false
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
unsigned char  Is_Any_Key_Hold(void)
{
	unsigned char hold_keys;

	hold_keys = 0;
	hold_keys += Get_Pressed_Keys(Key_Old_Scaned);

	if (hold_keys > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*
*******************************************************************************
*                             Is_Key_Pressed
* Description: appoint key pressed now?
*
* Arguments  : Key_Index--------------appoint key(KI_UP...KI_BACK)
*
* Returns    : hold return true, else return false
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
unsigned char  Is_Key_Pressed(char key_index)
{
	unsigned char baddr;

	baddr = key_index & 0x07;

	return ((Keys & (1 << baddr)) != 0) && (Pressed_Key_Counter == 1);
}

/*
*******************************************************************************
*                             Clear_Keys
* Description: Clearn all keys flg
*
* Arguments  : None
*
* Returns    : None
* 
* Programers : Tom.Yin
*
* Date	     : 
*
*******************************************************************************
*/
void Clear_Keys(void)
{
	Keys = 0;
}




⌨️ 快捷键说明

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