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

📄 main.c

📁 RFID实现代码
💻 C
📖 第 1 页 / 共 2 页
字号:
			else if(g_cReceBuf[2]==0)							  
			{
			  
			            cStatus =IicSendHY502(AntOff);	  // 发送命令	  			        
			            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
				    	if((cStatus==SUCCESS)&&(cp[1]==SOFTDOWN))
						{
					    	SendBuffer[0]=0x02;
							SendBuffer[1]=cmd;
					
						}
			
			}	  			
			else
			{
				SendBuffer[0]=0x02;
				SendBuffer[1]=~cmd;
				cStatus=1;
			}
		    break;

		case 0x12:     // Halt the card     //终止卡的操作       
            
		
			        	 cStatus =IicSendHY502(CardHalt);    		            
		                 cStatus =IicReadHY502(cp);		  // 保存到数组cP
			             if((cStatus==SUCCESS)&&(cp[1]==HALT_CARD))
		                  {
		                        SendBuffer[0]=0x02;
								SendBuffer[1]=cmd;							
							  						    
		                    }
	                  
						 else
						{
							SendBuffer[0]=0x02;
							SendBuffer[1]=~cmd;
						}
			
			break;

		case 0x13:    // 自动寻卡 ,1字节数据,0x01 开启自动寻卡,0x00关闭      
			if(g_cReceBuf[2]==0x01)
			{
		
		            cStatus =IicSendHY502(AutoCard);	  // 发送命令 		          
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==AUTOSEARCH))
					{
				    	SendBuffer[0]=0x02;
						SendBuffer[1]=cmd;	 
					
						Cardinner=1;	 				    	
						cStatus=0;
					}
					else
					{
						SendBuffer[0]=0x02;
						SendBuffer[1]=~cmd;
						cStatus=1;
					 }	 			
				}
			
			 else if(g_cReceBuf[2]==0)
 			 {		 			
		            cStatus =IicSendHY502(AutoOff);	  // 发送命令  		         
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==AUTOSEARCH))
					{
				    	SendBuffer[0]=0x02;
						SendBuffer[1]=cmd;	 				
		 			
						Cardinner=1;	 				    	
						cStatus=0;
					}
					else
					{
						SendBuffer[0]=0x02;
						SendBuffer[1]=~cmd;
						cStatus=1;
					 }	  			
			 
			 }
			 else
			{
				SendBuffer[0]=0x02;
				SendBuffer[1]=~cmd;
				cStatus=1;
			}
 			break; 
 			

		case 0x14:    // 蜂鸣器操作 ,1字节数据,0x1y 开启并使蜂鸣器响y声,0x0y关闭
			g_cBeeps=g_cReceBuf[2];
			SendBuffer[0]=0x02;
			SendBuffer[1]=cmd;
 			break;

		case 0x15:    // 蜂鸣器操作 ,设置蜂鸣器间隔,1字节数据 ,0.01s
			sp_freq=~g_cReceBuf[2];
			SendBuffer[0]=0x02;
			SendBuffer[1]=cmd;
 			break;
 			
 			
		case 0x19://读卡类型  //无数据    返回卡类型(2 bytes)	    
		
		            cStatus =IicSendHY502(ComCardType);	  // 发送命令			        
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==CARD_TYPE))	
		                { 	       
			               	SendBuffer[0]=4;
			        		SendBuffer[1]=cmd;
			        		SendBuffer[2]=cp[2];
			        		SendBuffer[3]=cp[3];	
				
	            		}
						
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}
		
	            break;		
   

		case 0x20:     // Request,Anticoll,Select,return CardSerialNo(4 bytes)
			        // 寻卡,防冲突,选择卡    返回卡系列号(4 bytes)
	
		            cStatus =IicSendHY502(ComSearchCard);	  // 发送寻卡命令  		         
		            cStatus =IicReadHY502(cp);		  // 读取卡号并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==CARD_SN))
				
						{		                      
									SendBuffer[0]=6;
									SendBuffer[1]=cmd;
									memcpy(&SendBuffer[2],&cp[2],4);
		                   
		                    }		
		      
			else
			{
				SendBuffer[0]=0x02;
				SendBuffer[1]=~cmd;
				cStatus=1;
			}
	
			break;	   
   	 
		case 0x21:		//读块             	   读写块时建议关闭自动寻卡功能可以提高读写的成功率!
						//数据内容  1 字节密钥标识+1 块号+6 字节密钥		  			
		
		            cStatus =IicSendHY502(ComSearchCard);	  // 发送寻卡命令			          
		            cStatus =IicReadHY502(cp);		  // 读取卡号并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==CARD_SN))			         

		                {     
			               	SendBuffer[0]=0x12;
			        		SendBuffer[1]=cmd;	  
							memcpy(&SendBuffer[2],&cp[2],16);			        	
				
	            		}
			
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}
		
       		break;	
 
		case 0x22 :		//写块      
						//1 字节密钥标识+1 块号+6 字节密钥+16 字节写入数据
	
		            cStatus =IicSendHY502(g_cReceBuf);	  // 发送命令  		     
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==WRITE_BLOCK))	
		                { 	       
			               	SendBuffer[0]=0x02;
			        		SendBuffer[1]=cmd;	  			        	
						
	            		}
						
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}				
		
			break;

		case 0x23 :		//初始化钱包           
						//数据内容“1 字节密钥标识+1 块号+6 字节密钥+4 字节钱包”
	
		            cStatus =IicSendHY502(g_cReceBuf);	  // 发送命令		 		        
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==INIT_PURSE))	
		                { 	       
			               	SendBuffer[0]=0x02;
			        		SendBuffer[1]=cmd;	  			        	
					
	            		}
						
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}
			
			break;

		case 0x24:	   //读钱包
						//数据内容"1字节密钥标识+1 块号+6 字节密钥”
			
		            cStatus =IicSendHY502(g_cReceBuf);	  // 发送命令	 		        
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==READ_PURSE))	
		                { 	       
			               	SendBuffer[0]=0x06;
			        		SendBuffer[1]=cmd;	
							memcpy(&SendBuffer[2],&cp[2],4);  			        	
					
	            		}
						
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}
		
			break;


		case 0x25:	//钱包充值
					//数据内容“1 字节密钥标识+1 块号+6 字节密钥+4 字节增加值(低字节在前)”
		
		            cStatus =IicSendHY502(g_cReceBuf);	  // 发送命令	  		      
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==ADD_PURSE))	
		                { 	       
			               	SendBuffer[0]=0x02;
			        		SendBuffer[1]=cmd;	  			        	
					
	            		}
						
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}
		
            break;
                
 
		case 0x26:	//钱包扣款	   						
					//数据内容“11 字节密钥标识+1 块号+6 字节密钥+4 字节增加值(低字节在前)”
	
		            cStatus =IicSendHY502(g_cReceBuf);	  // 发送命令				       	        
		            cStatus =IicReadHY502(cp);		  // 读取并存入到cP
			    	if((cStatus==SUCCESS)&&(cp[1]==DEC_PURSE))	
		                { 	       
			               	SendBuffer[0]=0x02;
			        		SendBuffer[1]=cmd;	  			        	
				
	            		}
						
		            else
		            {
		        			SendBuffer[0]=0x02;
		        			SendBuffer[1]=~cmd;
							cStatus=1;
		        	}
			
            break;

    
		default:
			SendBuffer[0]=0x02;
			SendBuffer[1]=0xFF;	
		    break;

	}
	return cStatus;
}


/************************** main function start here *******************************/
void main()
{		   
   	uchar idata cStatus;

	InitializeSystem();		   // 初始化系统    	     	

	//设置IIC地址:0xA2 
    //IIC地址设置说明:单字节,高四位固定为A;低四位为:a2 a1 a0 R/W 
   //设置IIC地址后,需要一定时间的延时,等待读卡器配置完成。
       a0=1;
       a1=0;
       a2=0;    


	RST_HY=0;			  // 复位模块 ,复位后等模块稳定再开始操作
    delay(500);          //延时等待模块初始化
	RST_HY=1;            
	LED_YELLOW=0;
	LED_GREEN=0;
	delay(500);	
    LED_YELLOW=1;	
    LED_GREEN=1;   


	while (1)
    {
		    LED_GREEN=sig;     // detect module inturrupt ,if there is a card in ,then sig=0,unless you colse the auto search.				 
		     // KeyPress();	处理按键
			 // add your code here

		//检查命令标志
 	    if (g_bReceCommandOk)			
		{				   							   	
		    g_bReceCommandOk = FALSE;		 		
			cStatus=uart_process();
			if(cStatus)
			{
				cStatus=0;
				if(g_cBeeps==0x10)
				{
					g_cBeeps+=2;	   //错误响2声
				}
			}
			else
			{
				if(g_cBeeps==0x10)
				{
					g_cBeeps+=1;		 //正确响一声
				}

			}
			if(g_cBeeps>0x10)
			{
			   g_cBeepDiv0=g_cBeeps-0x10;
			   g_cBeeps=0x10;    
			   g_cBeepDiv0<<=5;
			}
		
			UartSend(SendBuffer);   // Send data to uart.
			// add your code here
    	}	 
	
	}
}


/*****************************************************************************
*system initialize
*UART baudrate = 19200bps, baud generator is T1
*****************************************************************************/
void InitializeSystem()
{
   
	ET2 = 0;
	T2CON = 0x04;
    PCON = 0x80;
    SCON = 0x70;
    TMOD = 0x21;        //TMOD = 0x22;

	TH1   = BAUD_19200;  //默认波特率
	TL1   = TH1;
	TR1   = TRUE;             // 波特率发生器

	TH0 = 0xDC;
    TL0 = 0x00;
    TR0 = 1;
    ET0=1;

	ET1=0;
	EA=1;
	EX0=0;//1;
	IT0 = 1;
	TR2=0;
 	ES = TRUE;
	g_bReceCommandOk=0;	   		 					
	BUZ=0;
}

⌨️ 快捷键说明

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