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

📄 duxiem1ka.c

📁 读写Mifare One卡, 说明:此例程为一个块的读写操作
💻 C
📖 第 1 页 / 共 3 页
字号:
	   zhilingma=SBUF;
if (zhilingma!=jiaozhengshizhongma)
    {
      
	   // led_cyc_on();

/*-----------------------------------------------------------------------------------------------
;Step1
;通讯测试
;下传命令:3C 04 00 60 00 00  BCC 0D
;成功返回:3C 01 01 BCC 0D
;错误返回:3C 01 00 BCC 0D*/

     
      tmpResult=0xFC;
	  Commu_test();
	  //SetRes(CardStatus,1); //Result中最高位是Commu_Test成功标志,执行此条把tmpResult的最高位置零
  	  delay10ms(3);
 
      if (CardStatus)  //如果通讯测试正常,则执行寻卡操作
	        {Search_Card(CardNo,SearMode);}
          else
	        goto start;
	  delay10ms(3);
 
/*-----------------------------------------------------------------------------------------------
;Step2
;寻卡 void	Search_Card(uchar *CardNo,uchar SearMode)
;下传命令:3C 04 01 70 00(寻卡模式00(IDLE)或01(ALL)) 00 BCC 0D
;成功返回:3C 05 四字节卡号 00 BCC 0D
;错误返回:3C 01 FF(错误返回代码) BCC 0D
;其中:00寻卡模式要求操作的卡在读写完成后要离开感应区才能再读写;
;01寻卡模式要求操作的卡在读写完成后卡片只需关闭后又可读写;*/
    //sen(0xff);
 
	//SetRes(CardStatus,2);//Result中第二位成功标志,执行此条把tmpResult的第二位置零
//******************************************************
      if (zhilingma==chqingkongma)  //清空刷卡记录
	     {
		        RI=0;
		        for (i=0;i<48;i++)
			     {
startkaqingkong:
      tmpResult=0xFC;
	  Commu_test();
	  //SetRes(CardStatus,1); //Result中最高位是Commu_Test成功标志,执行此条把tmpResult的最高位置零
  	  delay10ms(3);
 
      if (CardStatus)  //如果通讯测试正常,则执行寻卡操作
	        {Search_Card(CardNo,SearMode);}
          else
	        { sen(0xfe);  goto startkaqingkong; };
	  delay10ms(3);
		            //RI=0;
				    //while(!RI);
                    //CardBlockNo=SBUF;
                    //RI=0;
                   // if  ((i+1)%4!=0 || i==0)
					if (i!=(i/4)*4+3)
					   {
					      CardBlockNo=i+0x0c;
                          if (CardStatus)
		                        {
      	                           Psd_Verify(CardBlockKey,BlockKeyAorB,CardBlockNo,CardNo);
                                   if (CardStatus)
		                                 {
			                               for (j=0;j<16;j++)
		                                          CardDataBuf[j]=0x00;
                                            delay10ms(3);
 	                                        Write_Card(CardBlockNo,CardDataBuf);
                                              //TurnOff_Card();
                                          }
                                        else
                                         { sen(0xff);   sen(CardBlockNo);goto startkaqingkong; };
                               }
                                else
                                  { sen(0xfa);  goto startkaqingkong; };
				       };
				  //delay10ms(3);
				 sen(CardBlockNo);
			   };
		 };

//******************************************************
//******************************************************
      if (zhilingma==dukama)  //读卡指令 0x55
	     {
		  //sen(0xf9);
		  RI=0;
           for (i=0;i<64;i++)
		     {
rrr2:		      sen(i);

              Search_Card(CardNo,SearMode);
              CardBlockNo=i;

	  if (CardStatus)
		   {
      	      Psd_Verify(CardBlockKey,BlockKeyAorB,CardBlockNo,CardNo);
              if (CardStatus)
		        {
 	             Read_Card(CardBlockNo,CardDataBuf);
                 if (CardStatus)
		               {
			            for (j=0;j<16;j++)
		                  sen(CardDataBuf[j]);
					   }
					 else
					   {  sen(0xfe);
					      TurnOff_Card();
	 
	                      delay10ms(3);
						  goto rrr2;    //读取数据失败
					   };  
                 }
                else
                 {
                    sen(0xf1);  //密码校验失败
				    goto rrr2;
                 };
              
            }
		   else
		     {
		                   sen(0xf0);   //寻卡失败
					     TurnOff_Card();
	 
   	                     delay10ms(3);
						 goto rrr2;
			  };


		 }; //for 
      };

	   };  //if (zhilingma==jiaozhengshizhongma)
//******************************************************
	   if (zhilingma==dukashujuma)  //读卡数据指令
	     {
		  //sen(0xf9);
		  RI=0;
           for (i=0;i<64;i++)
		     {
		      sen(i);
              if (i!=(i/4)*4+3)
			    {
startkashuju:
      tmpResult=0xFC;
	  Commu_test();
	  //SetRes(CardStatus,1); //Result中最高位是Commu_Test成功标志,执行此条把tmpResult的最高位置零
  	  delay10ms(3);
 
      if (CardStatus)  //如果通讯测试正常,则执行寻卡操作
	        {Search_Card(CardNo,SearMode);}
          else
	        goto startkashuju;
	  delay10ms(3);
				
                CardBlockNo=i;
				if (CardStatus)
      	           { Psd_Verify(CardBlockKey,BlockKeyAorB,CardBlockNo,CardNo);}
				  else
   			       {goto startkashuju;};
                delay10ms(3);
                if (CardStatus)
		              {
 	                   Read_Card(CardBlockNo,CardDataBuf);
					
                       if (CardStatus)
		                    {
			                  for (j=0;j<16;j++)
		                             sen(CardDataBuf[j]);
						    }
						  else
						    {sen(0xee);goto startkashuju;};
                      }
				   else
                      {
                       sen(0xff);
                      };
                 delay10ms(3);
                 };
       delay10ms(3);
 //TurnOff_Card();
             };

		 };
//******************************************************
			      if (SBUF==jiaozhengshizhongma)
			          {
				       //校正时钟

                       RI=0;
				       //接收6个字节数据,顺序年、月、日、时、分、秒
					  // shoufa485=shou;
				       for (i=0;i<6;i++)
				          {
					       // time=0;
					        while(!RI)
					     	    {
						         //超时处理
			     				  //if (time>50)
				     			    // { goto xianshishizhong; };
			     				};
		     				 shezhiriqishijian[i]=SBUF;
		     			     RI=0;
		     		      };//接收6个字节数据,顺序年、月、日、时、分、秒
                      //设置时钟芯片  2007.07.09
 	                    i=(readrtc(0x0a));    //读取设置寄存器A
     	     		    writertc(0x0a,i&0xaf);   //打开时钟芯片
 	                    i=(readrtc(0x0a));    //读取设置寄存器A
     	     		    writertc(0x0a,i|0x20);   
 	                    i=(readrtc(0x0b));    //读取设置寄存器B
     	     		    writertc(0x0b,i|0x80);   //
	//设置寄存器B为可以设置状态
 	                   // i=(readrtc(0x0b));    //读取设置寄存器B
     	     		   // writertc(0x0b,i|0x86);   




		     		   //写入时钟芯片中
     	     		    writertc(0x00,shezhiriqishijian[5]);  //秒
    	     		    writertc(0x02,shezhiriqishijian[4]);  //分
    	     		    writertc(0x04,shezhiriqishijian[3]);  //时
     	     		    // writertc(0x06,shezhizhou);         //周
    	     	        writertc(0x07,shezhiriqishijian[2]);   //日
    			        writertc(0x08,shezhiriqishijian[1]);  //月
   			            writertc(0x09,shezhiriqishijian[0]);  //年

	//设置寄存器B为数据更新状态
                        //设置时钟芯片  2007.08.27
     	     		    writertc(0x0b,i&0x7f);   //
     	     		    writertc(0x0b,0x02);   //

		     		   //读取时钟芯片中并发送
				       //读取日期时间
					 // time=0;
					  //while(time<20);
// xianshishizhong:
                      xtshijian[5]=(readrtc(0x00));    //读取秒
	                  xtshijian[4]=(readrtc(0x02));     //读取分
	                  xtshijian[3]=(readrtc(0x04));     //读取时
	                  xtshijian[2]=(readrtc(0x07));      //读取日
	                  xtshijian[1]=(readrtc(0x08));     //读取月
	                  xtshijian[0]=(readrtc(0x09));   //读取年
                        //shoufa485=shou;
	                   //  sen(0x30);				    
					    //shoufa485=fa;
					  for (i=0;i<6;i++)
					      {sen(xtshijian[i]);};
                      //sen(readrtc(0x0b));
 
                       };
				  //校正时钟
				  //********************************8
	   if (zhilingma==cqingkongma)  //把卡0x0c清空指令
	     {
		        RI=0;
				while(!RI);
                CardBlockNo=SBUF;
                RI=0;
      	        Psd_Verify(CardBlockKey,BlockKeyAorB,CardBlockNo,CardNo);
                if (CardStatus)
		           {
			        for (j=0;j<16;j++)
		                CardDataBuf[j]=0x00;

 	               Write_Card(CardBlockNo,CardDataBuf);

                   };
		 };
//******************************************************
	   if (zhilingma==xieshujuma)  //写数据卡指令 02 38 02 39 
	     {
		        RI=0;
                CardBlockNo=0x04;
      	        Psd_Verify(CardBlockKey,BlockKeyAorB,CardBlockNo,CardNo);
                if (CardStatus)
		           {
			        for (j=0;j<16;j++)
		                CardDataBuf[j]=0x00;
                   CardDataBuf[0]=0x02;    
				   CardDataBuf[1]=0x38; 
				   CardDataBuf[2]=0x02; 
				   CardDataBuf[3]=0x39; 
 	               Write_Card(CardBlockNo,CardDataBuf);

                   };


		 };



 //******************************************************
	   if (zhilingma==qingkongma)  //把卡清空指令
	     {
		 };

	   if (zhilingma==dukuaima)  //把卡清空指令
	     {

		  RI=0;
          while(!RI);
 
	        sen(i);

               CardBlockNo=i;

       	      Psd_Verify(CardBlockKey,BlockKeyAorB,CardBlockNo,CardNo);
              if (CardStatus)
		        {
 	             Read_Card(CardBlockNo,CardDataBuf);
                 if (CardStatus)
		               {
			            for (j=0;j<16;j++)
		                  sen(CardDataBuf[j]);
                        }
						else
						 {
						   sen(0xdd);
						 };
				 }
			   else
			     {
				  sen(0xde);
				 };
        }

	TurnOff_Card();
	SetRes(CardStatus,6);
	delay10ms(3);
        display(tmpResult);

//*****************************************************

//数据区:
   //启动卡:1块   4块
       // 1块:AA 以后全零
	   // 4块:02 38 02 38  以后全零


   //通行卡:1块   4块 
       // 1块:55 以后全零
	   // 4块:02 38 以后全零


       //使用后  	C块    D块   E块   10块
           //C块:02 38 09 02 18 54 00 10 除最后1个外全零  02

           //D块:02 38 09 02 18 55 00 10 除最后1个外全零  02

		   //E块:同D块

		   //10块:同D块


//2007.9.3  程序流程


//上电流程
  //1、读取标志,判断是否初始化过;
  //2、如果初始化过,则初始化标志置位,等待读卡;
  //3、如果没有初始化过,等待读卡;


//1、寻卡
//2、如果有卡则密码验证
//3、验证正确后,读1块,判断是启动卡,还是通行卡
//4、如果是启动卡,读取4块,验证数据一致性,如果一致则存储数据
//5、如果是通行卡,读取4块,验证是否和启动数据一致,如果一致并且C块没有数据则写收车数据;
//6、如果和启动数据不一致,则报告错误;
//7、返回  寻卡






//*****************************************************


    }
}

⌨️ 快捷键说明

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