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

📄 ps2键盘(at45db011).c

📁 键盘针对78键的键盘开发的针对PS2的程序
💻 C
📖 第 1 页 / 共 5 页
字号:
	Emluator_RS232(0x0d);
	Emluator_RS232(0x0a);
	
	while(Send_1Byte_To_PS2_Port(0xaa)==0) {}
	while(1)
	{
		
	}
	*/
/*	
	S_Timing=0;
	while(1)
	{
		while(Send_1Byte_To_PS2_Port(0xaa)==0) {}
		Send_1Byte_To_PS2_Port(0x58);
		//Send_1Byte_To_PS2_Port(0xf0);
		//Send_1Byte_To_PS2_Port(0x58);
		if(TRead_PS2_Port(20)==1)
		{
			Emluator_RS232(Key_Reciver_Buffer);while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
		}
		while(S_Timing<5) {WDT_CONTR=0x37;}
		S_Timing=0;
	}		
*/		
////==================================================================================================
	
//*	
	while(1)
	{		
		if(MCR_SDA ==0 )
		{
			//说明,刷卡后MCR_SDA信号会维持330毫秒的低电平时间共检测
			WDT_CONTR=0x37;
			Read_MegaCARD();
			WDT_CONTR=0x37;
			Process_nCard();
		}
		WDT_CONTR=0x37;
		
		k=Read_LKB_Data(1);	
		if(k!=0)
		{	
			while(Send_1Byte_To_PS2_Port(k)==0) {}	
			//Emluator_RS232(k);		
		}			
		
		if(TRead_PS2_Port(1)==1)		//
		{	
			switch(Key_Reciver_Buffer)
			{
				case 0xff:
				{	
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					while(Send_1Byte_To_PS2_Port(0xaa)==0) {}
					
					Send_LKB_Data(0xff);
					
					break;
				}
				
				case 0xfe:
				{
					while(Send_1Byte_To_PS2_Port(Last_Send_To_Ps2_Word)==0) {}
					break;
				}
				
				case 0xfd:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xfc:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xfb:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xfa:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf9:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf8:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf7:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf6:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf5:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf4:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}
				
				case 0xf3:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					
					if(TRead_PS2_Port(20)==1)
					{
						while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					}
					break;
				}
				
				case 0xf2:
				{	
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					while(Send_1Byte_To_PS2_Port(0xab)==0) {}
					while(Send_1Byte_To_PS2_Port(0x83)==0) {}
					break;
				}
				
				case 0xf0:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					
					if(TRead_PS2_Port(20)==1)
					{
						while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					}
					break;
				}
				
				case 0xee:
				{
					while(Send_1Byte_To_PS2_Port(0xee)==0) {}
					break;
				}
				
				case 0xed:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					
					if(TRead_PS2_Port(20)==1)
					{
						while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
							
						if(Key_Reciver_Buffer<8)
						{
							Caps_Scroll_Num_Status=Key_Reciver_Buffer;
						}
					}
					
					while(Read_LKB_Data(30)!=0) {}
					
					Send_LKB_Data(0xed);

					if(Read_LKB_Data(200)==0xfa)
					{
						Send_LKB_Data(Caps_Scroll_Num_Status);
						if(Read_LKB_Data(200)==0xfa)  break;
						
						else
						{
							Send_LKB_Data(0xff);
							break;
						}
					}
					else
					{
						Send_LKB_Data(0xff);
					}
					break;
				}
				
				//如果是读取按键定义命令
				case 0xec:
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					Send_Recode_To_Ps2();					
					break;					
				}
				
				//如果是按键定义包
				case 0xeb:
				{					
					RD_PRO_eb_Frame();					
					break;
				}
			
				//如果是键盘属性包
				case 0xea:
				{					
					RD_PRO_ea_Frame();					
					break;
				}				
				
				default:				
				{
					while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
					break;
				}		
			}
		}
		
		WDT_CONTR=0x37;
		ScanKey();
		WDT_CONTR=0x37;
		Process_Key();
				
		WDT_CONTR=0x37;
		
		if((Key_Send_Buffer_Point!=Key_Send_Buffer_PlacPoint)&&(Key_Send_Buffer_PlacPoint!=0))
		{
			k=ReadByteFromBuffer(Key_Send_Buffer_Point);
			SPI45DB041_CS=1;
			
			while(Send_1Byte_To_PS2_Port(k)==0) {}
				
			Key_Send_Buffer_Point++;
			if(Key_Send_Buffer_Point>=Key_Send_Buffer_PlacPoint)
			{
				Key_Send_Buffer_Point=0;
				Key_Send_Buffer_PlacPoint=0;
			}
		}
	
		Read_Lock();
	}
//*/
}
//=========================================
//第1档:0xfd
//第2档:0xfe
//第3档:0x7f
//第4档:0xbf
//第5档:0xdf
//第6档:0xef
//第7档:0xf7
//第8档:0xfb
//第9档:0xff

void Read_Lock(void)						//
{
	unsigned char i;
		
	if(Key_Send_Buffer_PlacPoint!=0) return;
	
	LOCK_1=0;
	Delay_uS_Time(5);
	P0=0xff;
	i=P0;
	if(((i==0xfd)||(i==0xfe)||(i==0x7f)||(i==0xbf)||(i==0xdf)||(i==0xef)||(i==0xf7)||(i==0xfb))&&(i!=Lock_Lum))
	{
		Lock_Lum=i;
		
		for(i=0;i<7;i++)
		{
			if(Lock_Lum==LOCK_VOL[i])  break;
		}
		
		if(Keycoded_Table==0)
		{					
			Process_MakeCode(128+i);
		}	
	}
	LOCK_1=1;
}

//只上传键定义编码部分
void Send_Recode_To_Ps2(void)
{
	unsigned char i;
	unsigned char j;
	unsigned char k;
	
	if(TRead_PS2_Port(40)==1)
	{
		while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
		
		if(Key_Reciver_Buffer==135)
		{
			WDT_CONTR=0x37;
			ReadOnePageToBuffer(SYSTEM_PARA_PAGE_FLASH);
			i=ReadByteFromBuffer(SYSYTEM_STATUS_BYTE);	
			SPI45DB041_CS=1;
			
			while(Send_1Byte_To_PS2_Port(0xb8)== 0) {}						//	
			while(Send_1Byte_To_PS2_Port(0xb7)== 0) {}						//		
			while(Send_1Byte_To_PS2_Port(0xa5)==0) {}					//
			
			while(Send_1Byte_To_PS2_Port(0xb0)== 0) {}
			while(Send_1Byte_To_PS2_Port(((~(i>>4))&0x0f)+0xb0)==0) {}
			while(Send_1Byte_To_PS2_Port(0xb0)== 0) {}
			while(Send_1Byte_To_PS2_Port(((~i)&0x0f)+0xb0)==0) {}
			
			while(Send_1Byte_To_PS2_Port(0xb0)== 0) {}						//	
			while(Send_1Byte_To_PS2_Port(0xb0)== 0) {}
			while(Send_1Byte_To_PS2_Port(0xe8)==0) {}					//结束符
		}
		else
		{
			WDT_CONTR=0x37;
			
			ReadOnePageToBuffer(Key_Reciver_Buffer);
			
			while(Send_1Byte_To_PS2_Port(((Key_Reciver_Buffer&0xf0)>>4)+0xb0)== 0) {}						//	
			while(Send_1Byte_To_PS2_Port((Key_Reciver_Buffer&0x0f)+0xb0)== 0) {}						//		
			while(Send_1Byte_To_PS2_Port(0xa5)==0) {}					//
				
			k=ReadByteFromBuffer(0x0);
			while(Send_1Byte_To_PS2_Port((((k&0xf0)>>4)+0xb0))==0) {}						//	
			while(Send_1Byte_To_PS2_Port(((k&0x0f)+0xb0))==0) {}						//		
				
			if((k!=0x0)&&(k!=0xff))		
			{
				for(j=0;j<255;j++)
				{	
					WDT_CONTR=0x37;
					k=ReadNextFromBuffer();
					while(Send_1Byte_To_PS2_Port((((k&0xf0)>>4)+0xb0))==0) {}						//	
					while(Send_1Byte_To_PS2_Port(((k&0x0f)+0xb0))==0) {}						//		
					if((k==0x0)||(k==0xff))	break;
				}
			}
			SPI45DB041_CS=1;
			while(Send_1Byte_To_PS2_Port(0xe8)==0) {}					//结束符
		}
	}
}


//===========================================读并处理0x00包==============================================
//	数据格式:EA 06  03 0F CHKH CHKL
//	03字节:Bit7
//			Bit6
//			Bit5
//			Bit4
//			Bit3		1=Tr1_Enable
//			Bit2		1=Beep_Nop_Key
//			Bit1		1=Send_Break_Code
//			Bit0		1=Beep__Enable

//	0F字节:Bit7
//			Bit6
//			Bit5
//			Bit4
//			Bit3		1=Tr3_Enable
//			Bit2		1=Tr2_Enable
//			Bit1		1=Send_Enter
//			Bit0		1=Send_Format_Code

//接收的这两个字节整合后取反存在Flash中,考虑到041出厂时是1状态,所以这些状态标识在RAM中和Flash中是相反的
//=======================================================================================================
void RD_PRO_ea_Frame(void)
{
	unsigned char i;
	unsigned char Reciver_Ok;
	
	Reciver_Ok=1;
	while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
	for(i=0;i<(6-1);i++)
	{
		if(TRead_PS2_Port(40)==1)						//规定30毫秒内读下一个字节,未读到返回
		{
			WDT_CONTR=0x37;
			TEMP_DATA[i]=Key_Reciver_Buffer;
			while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
		}
		else
		{
			Reciver_Ok=0;
			break;
		}
	}
	PS2_TXD=0;
	//校验
	if(Reciver_Ok==1)
	{
		Reciver_Ok=0;
		if(Reciver_Data_Check(0xea)==1)  Reciver_Ok=1;
	}
	
	if(Reciver_Ok==1)
	{
		System_Status_Reg=(((TEMP_DATA[1]<<4)&0xf0)|(TEMP_DATA[2]&0x0f));			//保存设置
				
		ReadOnePageToBuffer(SYSTEM_PARA_PAGE_FLASH);
		WriteOneByteToBuffer(SYSYTEM_STATUS_BYTE,(~System_Status_Reg));
		SPI45DB041_CS=1;
		WriteBufferToPageWithErase(SYSTEM_PARA_PAGE_FLASH);		
	}
	PS2_TXD=1;	
	if(Reciver_Ok==1)	while(Send_1Byte_To_PS2_Port(0xe8)==0) {}					//最后一个字节要等处理完数据后再返回上位机0xfa
	else	while(Send_1Byte_To_PS2_Port(0xe7)==0) {}		
}

//=======================================读并处理0xeb包============================================
/*
EB 0C 00 00 00 58 1C 1B 1C 00 01 A2 EB 0C 00 00 00 58 1C 1B 1C 00 01 A2 ED EB 08 01 00 00 00 00 F4 14 15 16 14 15 16 14 15 16 14 15 14 15 14 15 14 15 14 15 14 15 14 15 14 15 16 14 15 14 15 16 19 14 15 
*/
void RD_PRO_eb_Frame(void)
{
	bit Reciver_Ok;
	unsigned char i;
	unsigned int k;
		
	Reciver_Ok=0;
	while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
	
	if(TRead_PS2_Port(40)==1)												
	{		
		TEMP_DATA[0]=Key_Reciver_Buffer;
		k=Key_Reciver_Buffer;												//长度字节
		
		Reciver_Ok=1;
		while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
		for(i=0;i<3;i++)
		{
			if(TRead_PS2_Port(40)==1)
			{
				WDT_CONTR=0x37;
				TEMP_DATA[i+1]=Key_Reciver_Buffer;
				while(Send_1Byte_To_PS2_Port(0xfa)==0) {}
			}
			else
			{
				Reciver_Ok=0;
				break;
			}
		}
		
		if(Reciver_Ok==1)
		{
			//从这里开始读编码
			Reciver_Ok=0;
			if(TRead_PS2_Port(30)==1)
			{
				WDT_CONTR=0x37;
				while(Send_1Byte_To_PS2_Port(0xfa)==0) {}

⌨️ 快捷键说明

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