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

📄 key.c

📁 Avr-atmega64 按键扫描和按键处理程序
💻 C
字号:
#include <iom128v.h>
#include <macros.h>
#include "lcd.h"
#include "eeplist.h"
//控制端口定义
#define KEY_PORT	 PORTF
#define KEY_DDR		 DDRF
#define KEY_PIN  	 PINF
#define KEY_INT_PORT     PORTE
#define KEY_INT_DDR      DDRE
#define KEY_INT_PIN      PINE
//端口配置定义
#define KEY_DDR_Num       0x0F  //端口输入输出方向配置
#define KEY_PIN_Num       0xF0  //端口输入掩码
#define KEY_INT_Bit       0x7f  //端口
//逻辑定义
#define TRUE              1
#define FALSE             0
//定义表
const unsigned char KEY[17] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
//定义变量
unsigned char IDcntMemory = 0; //用于记录ID按下的数字已经有几位了
unsigned char MAcntMemory = 0; //用于记录密码按下的数字已经有几位了
unsigned char key_jz; //按键的健值
unsigned char key_press_flag = FALSE; //是否有按键按下
unsigned char Index = 1;  //菜单索引
unsigned char IndexNum;  //菜单条目索引个数
unsigned char FormIndex = 0; //表明显现在是第几个窗口在现显示

unsigned char IDBuf[5];  //0~3存放ID 4存放ID长度
unsigned char CodeBuf[5];//0~3存放ID 4存放长度
extern unsigned char ASCIIDisplayBuf[16]; //存放显示时需要的数据
extern unsigned char TimeBuf[8];          //存放系统时间
//初始化按键需要的配置
void Mcu_init_Key(void)
{
	KEY_PORT = KEY_PIN_Num;
	KEY_DDR = KEY_DDR_Num;
	EIMSK |= 0x80;
}

//被中断处理函数调用,返回按键值
unsigned char Mcu_Get_Key(void)
{
	unsigned char temp = 0;//暂时存储按键值
	unsigned char cnt;
	EIMSK &= (~0x80);//关闭中断,防止再次进入中断,恢复其IO功能
	KEY_INT_DDR &= KEY_INT_Bit;//置位输入方向
	for(cnt = 4;cnt < 8;cnt++)
	{
	    if(cnt == 4)
        	  KEY_PORT = 0xFE;
		else if(cnt == 5)
			  KEY_PORT = 0xFD;
		else if(cnt == 6)
		  	 KEY_PORT = 0xFB;
		else
			KEY_PORT = 0xF7;
		if((KEY_PIN&KEY_PIN_Num) == 0xE0)
		{
			temp = 1+(cnt-4);
			key_press_flag = TRUE; //是否有按键按下
			break;	
		}
		else if((KEY_PIN&KEY_PIN_Num) == 0xD0)
		{
			temp = 5+(cnt-4);
			key_press_flag = TRUE; //是否有按键按下
			break;	
		}
		else if((KEY_PIN&KEY_PIN_Num) == 0xB0)
		{
			temp = 9+(cnt-4);
			key_press_flag = TRUE; //是否有按键按下
			break;	
		}
		else if((KEY_PIN&KEY_PIN_Num) == 0x70)
		{
			temp = 13+(cnt-4);
			key_press_flag = TRUE; //是否有按键按下
			break;	
		}
		else
		{
			key_press_flag = FALSE; //是否有按键按下;
		} 
	}
	while(!(KEY_INT_PIN&(0x80))); //等待按键释放
	Mcu_init_Key();
	return (KEY[temp-1]+16);
}

void Mcu_deal_KeyEvent(void)
{
     unsigned char *tempadress;
	 key_press_flag = FALSE; 
	 tempadress = ASCIIDisplayBuf;
 	 switch(key_jz-16)
	 {
	    case 0://数字
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		if(FormIndex == Form_Normall||FormIndex == Form_Code_ID)//证明是进入密码严整状态
		{
		   FormIndex = Form_Code_ID;
		   if(IDcntMemory < 4)//暂时定密码和ID都是6位
		   {
		      IDBuf[IDcntMemory] = key_jz;
		      IDcntMemory = IDcntMemory + 1;
		      IDBuf[4] = IDcntMemory;
		   }
		   Screen_DisPlay(FormIndex,Index,IDBuf);
		}
		else if(FormIndex == Form_Wait||FormIndex == Form_Identify_Code_ID)//等待管理员验证时发现是密码验证
		{
		   FormIndex = Form_Identify_Code_ID;
		   if(IDcntMemory < 4)//暂时定密码和ID都是6位
		   {
		      IDBuf[IDcntMemory] = key_jz;
		      IDcntMemory = IDcntMemory + 1;
		      IDBuf[4] = IDcntMemory;
		   }
		   Screen_DisPlay(FormIndex,Index,IDBuf);
		}
		else if(FormIndex == Form_Code_MA||FormIndex == Form_Identify_Code_MA)//等待输入密码
		{
		   if(MAcntMemory < 4)//暂时定密码和ID都是6位
		   {
		      CodeBuf[MAcntMemory] = key_jz;
		      MAcntMemory = MAcntMemory + 1;
		      CodeBuf[4] = MAcntMemory;
		   }
		   Screen_DisPlay(FormIndex,Index,CodeBuf);
		}
		else if(FormIndex == Form_Manager_Enroll)//等待管理员注册指纹时,有按键触发,需要更新id
		{
		   if(IDcntMemory < 4)//暂时定密码和ID都是6位
		   {
		      IDBuf[IDcntMemory] = key_jz;
		      IDcntMemory = IDcntMemory + 1;
		      IDBuf[4] = IDcntMemory;
		   }
		   Screen_DisPlay(FormIndex,Index,IDBuf);
		}
		else
		{
		;
		}
		break;
		case 10://上
		if(FormIndex == Form_Menu_AZ||FormIndex == Form_Menu_AA||FormIndex == Form_Menu_AB||FormIndex == Form_Menu_AC)
		{
		  if(Index > 0) Index = Index - 1;
		  if(Index == 0) Index = IndexNum;
		  Screen_DisPlay(FormIndex,Index,ASCIIDisplayBuf);
		}
		if(FormIndex == Form_Manager_Enroll||FormIndex == Form_User_Enroll)
		{
		  if(Index > 0) Index = Index - 1;
		  if(Index == 0) Index = IndexNum;
		  Screen_DisPlay(FormIndex,Index,IDBuf);
		}		
		break;
		case 11://下
		if(FormIndex == Form_Menu_AZ||FormIndex == Form_Menu_AA||FormIndex == Form_Menu_AB||FormIndex == Form_Menu_AC)
		{
		  if(Index <= IndexNum) Index = Index + 1;
		  if(Index > IndexNum) Index = 1;
		  Screen_DisPlay(FormIndex,Index,ASCIIDisplayBuf);
		}
		if(FormIndex == Form_Manager_Enroll||FormIndex == Form_User_Enroll)
		{
		  if(Index > 0) Index = Index - 1;
		  if(Index == 0) Index = IndexNum;
		  Screen_DisPlay(FormIndex,Index,IDBuf);
		}
		break;
		case 12://menu
		if(FormIndex < Form_Menu_AZ&&EEPROMread(ManagerNumEEPRomAdr)==0) //保证menu按键按下第一次才会出现menu窗口
		{
		  FormIndex = Form_Menu_AZ;
		  Screen_DisPlay(FormIndex,Index,ASCIIDisplayBuf);
		}
		else if(FormIndex < Form_Menu_AZ&&EEPROMread(ManagerNumEEPRomAdr)!=0)
		{
		   FormIndex = Form_Wait;//等待指纹验证或者密码验证
		   Screen_DisPlay(FormIndex,Index,ASCIIDisplayBuf);
		}
		else 
		{
		  ;
		}
		break;
		case 13://esc//退回到上一个屏幕
		if(FormIndex == Form_Menu_AZ||FormIndex == Form_Wait)
		{
		  FormIndex = Form_Normall; 
		  Index = 1;//菜单索引条复位
		  Screen_DisPlay(FormIndex,Index,TimeBuf);
		}
	    else if(FormIndex == Form_Menu_AA||FormIndex == Form_Menu_AB||FormIndex == Form_Menu_AC)
		{
		  FormIndex = Form_Menu_AZ; 
		  Index = 1;//菜单索引条复位
		  Screen_DisPlay(FormIndex,Index,ASCIIDisplayBuf);
		}
		else if(FormIndex == Form_Code_ID||FormIndex == Form_Code_MA||FormIndex == Form_Identify_Code_ID||FormIndex == Form_Identify_Code_MA)
		{//密码验证状态下如果按下ESC按键,返回到normall状态
		  FormIndex = Form_Normall; 
		  Index = 1;//菜单索引条复位
		  IDcntMemory = 0; //清楚记录的数字已经有几位了
		  MAcntMemory = 0;
		  IDBuf[4] = 0;   //记录的长度分别用数组的第4位传递,所以这里也要清零
		  CodeBuf[4] = 0;
		  Screen_DisPlay(FormIndex,Index,TimeBuf);
		}
		else if(FormIndex == Form_Manager_Enroll||FormIndex == Form_User_Enroll||FormIndex == Form_User_DelFP||FormIndex == Form_Record_Del)
		{
		  FormIndex = Form_Menu_AA; 
		  Index = 1;//菜单索引条复位
		  Screen_DisPlay(FormIndex,Index,ASCIIDisplayBuf);		
		}
		else
		{
		  ;
		}
		break;
		case 14://ok
		  if(FormIndex == Form_Menu_AZ && Index == 1)//数据管理ok
		  {
		      FormIndex = Form_Menu_AA;
		  }
		  else if(FormIndex == Form_Menu_AZ && Index == 2)//系统设置ok
		  {
		      FormIndex = Form_Menu_AB;
		  }
		  else if(FormIndex == Form_Menu_AZ && Index == 3)//系统信息ok
		  {
		      FormIndex = Form_Menu_AC;
			  ASCIIDisplayBuf[0] = 3;
			  ASCIIDisplayBuf[1] = 1;
			  ASCIIDisplayBuf[2] = 0;
			  ASCIIDisplayBuf[3] = 5;
			  ASCIIDisplayBuf[4] = 0;
			  ASCIIDisplayBuf[5] = 198;
		  }
		  else if(FormIndex == Form_Code_MA)
		  {
				 IDcntMemory = 0; //清楚记录的数字已经有几位了
		         MAcntMemory = 0;
		         IDBuf[4] = 0;   //记录的长度分别用数组的第4位传递,所以这里也要清零
		         CodeBuf[4] = 0;
				//调用密码比对函数,根据比对的结果决定是否进入菜单
				//*****************wldebug*******************************8
				if(CodeBuf[0] == 17&&CodeBuf[1] == 18&&CodeBuf[2] == 19&&CodeBuf[3] == 20)//1\2\3\4因为显示和实际错16
				{
				   FormIndex = Form_Menu_AZ; 
				}
				else 
				{
				   FormIndex = Form_Normall; 
				}
		  }
		  else if(FormIndex == Form_Identify_Code_MA)
		  {
				 IDcntMemory = 0; //清楚记录的数字已经有几位了
		         MAcntMemory = 0;
		         IDBuf[4] = 0;   //记录的长度分别用数组的第4位传递,所以这里也要清零
		         CodeBuf[4] = 0;
				//调用密码比对函数,根据比对的结果决定是否进入菜单
				//*****************wldebug*******************************8
				if(CodeBuf[0] == 17&&CodeBuf[1] == 18&&CodeBuf[2] == 19&&CodeBuf[3] == 20)//1\2\3\4因为显示和实际错16
				{
				   FormIndex = Form_Menu_AZ; 
				}
				else 
				{
				   FormIndex = Form_Normall; 
				}
		  }
		  else if(FormIndex == Form_Code_ID)
		  {
		      FormIndex = Form_Code_MA;
			  IDcntMemory = 0;//清除记下按键的ID长度
		  }
		  else if(FormIndex == Form_Identify_Code_ID)
		  {
		      FormIndex = Form_Identify_Code_MA;
			  IDcntMemory = 0;
		  }
		  else if(FormIndex == Form_Menu_AA)
		  	{
		  		if(Index == 1)//管理员注册
		  			{
		  				FormIndex = Form_Manager_Enroll;//Form_Menu_AAA;
						tempadress = CodeBuf;
		  			}
		  		else if(Index == 2)//用户注册
		  			{
		  				FormIndex = Form_User_Enroll;
		  			}
		  		else if(Index == 3)//用户删除
		  			{
		  				FormIndex = Form_User_DelFP;
		  			}
		  		else //记录删除
		  			{
		  				FormIndex = Form_Record_Del;
		  			}
		  	}
		  //else if(FormIndex == Form_Menu_AAA)
		  	//{
		  		//FormIndex = Form_ManagerEnroll;
		  	//}
		  else 
		  {
		    ; 
		  }
		  Index = 1;//菜单索引条复位
		  Screen_DisPlay(FormIndex,Index,tempadress);
		break;
		case 15://on/off
		break;
		break;
		default :
		break;
	 }
}

⌨️ 快捷键说明

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