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

📄 main.lst

📁 51控制RC500读写程序源代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
 285   2      			break;		
 286   2      		case 0x25:     // Read the mifare card
 287   2      		            // 读卡
 288   2      			status=M500PiccRead(RevBuffer[1],&RevBuffer[2]);
 289   2      			if(status==0)
 290   2      			{
 291   3      				if(cardtype==mifare1S50||cardtype==mifare1S70)
 292   3      					RevBuffer[0]=17;
 293   3      				else if(cardtype==1)
 294   3      					RevBuffer[0]=9;
 295   3      				else
 296   3      					RevBuffer[0]=16;			
 297   3      			}
 298   2      			else
 299   2      			{
 300   3      			    RevBuffer[0]=1;
 301   3      			}
 302   2      			RevBuffer[1]=status;			
 303   2      			break;
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 6   

 304   2      		case 0x26:     // Write the mifare card
 305   2      		            // 写卡  下载密码
 306   2      			status=M500PiccWrite(RevBuffer[1],&RevBuffer[2]);
 307   2      			RevBuffer[0]=1;
 308   2      			RevBuffer[1]=status;			
 309   2      			break;
 310   2      		case 0x27:    //钱包块值操作:加.减,
 311   2      			status = M500PiccValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3],RevBuffer[7]);
 312   2      			RevBuffer[0]=1;	
 313   2      			RevBuffer[1]=status;
 314   2      			break;
 315   2      		case 0x28:                  //终止卡的操作
 316   2      			status=M500PiccHalt();			
 317   2      			RevBuffer[0]=1;			//返回休眠卡的结果
 318   2      			RevBuffer[1]=status;
 319   2      			break;			
 320   2      	   case 0x29:								//进入软件掉电模式
 321   2      		    SetBitMask(RegControl,0x10);        // PowerDown = 1       
 322   2      		    cmd_ok();	                    // 内部电流消耗模块包括晶振在内关闭
 323   2      			break;
 324   2      		case 0x30:								//退出软件掉电模式
 325   2      		    ClearBitMask(RegControl,0x10);      // PowerDown = 0        
 326   2      		    cmd_ok();
 327   2      		    break;	
 328   2      		default:						    	// 其余默认操作错误
 329   2      			RevBuffer[0]=1;
 330   2      			RevBuffer[1]=1;
 331   2      		    break;		
 332   2      	}
 333   1      	
 334   1      }
 335          
 336          ///////////////////////////////////////////////////////////////////////
 337          //发送命令成功信号0100给上位机
 338          ///////////////////////////////////////////////////////////////////////
 339          void cmd_ok(void)
 340          {
 341   1      	RevBuffer[0]=1;	
 342   1      	RevBuffer[1]=0;
 343   1      }
 344          ///////////////////////////////////////////////////////////////////////
 345          
 346          /**********************RC500操作函数定义******************************/
 347          ///////////////////////////////////////////////////////////////////////
 348          // 往一个地址写一个数据
 349          ///////////////////////////////////////////////////////////////////////
 350          void WriteRawIO(uchar Address,uchar value)
 351          {
 352   1      	XBYTE[Address]=value;
 353   1      }
 354          
 355          ///////////////////////////////////////////////////////////////////////
 356          // 从一个地址读出一个数据
 357          ///////////////////////////////////////////////////////////////////////
 358          uchar ReadRawIO(uchar Address)
 359          {
 360   1      	return XBYTE[Address];
 361   1      }
 362          
 363          ///////////////////////////////////////////////////////////////////////
 364          // G E N E R I C    W R I T E
 365          // 往一个地址写一个数据(EEPROM)
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 7   

 366          ///////////////////////////////////////////////////////////////////////
 367          void WriteIO(uchar Address, uchar value)
 368          {
 369   1          WriteRawIO(0x00,GetRegPage(Address));  
 370   1          WriteRawIO(Address,value);              
 371   1      }
 372          
 373          ///////////////////////////////////////////////////////////////////////
 374          //          G E N E R I C    R E A D
 375          // 从一个地址读出一个数据(EEPROM)
 376          ///////////////////////////////////////////////////////////////////////
 377          uchar ReadIO(uchar Address)
 378          {
 379   1         WriteRawIO(0x00,GetRegPage(Address));
 380   1         return ReadRawIO(Address);                    
 381   1      }  
 382          
 383          ///////////////////////////////////////////////////////////////////////
 384          // 设置RC500定时时间
 385          ///////////////////////////////////////////////////////////////////////
 386          void M500PcdSetTmo(uchar tmoLength)
 387          {
 388   1          switch(tmoLength)
 389   1          {  
 390   2              case 1:                       
 391   2                  WriteIO(RegTimerClock,0x07); 
 392   2                  WriteIO(RegTimerReload,0x6a);
 393   2                  break;
 394   2              case 2:                       
 395   2                  WriteIO(RegTimerClock,0x07); 
 396   2                  WriteIO(RegTimerReload,0xa0);
 397   2                  break;
 398   2              case 3:  
 399   2                  WriteIO(RegTimerClock,0x09); 
 400   2                  WriteIO(RegTimerReload,0xa0);
 401   2                  break;
 402   2              case 4: 
 403   2                  WriteIO(RegTimerClock,0x09);
 404   2                  WriteIO(RegTimerReload,0xff);
 405   2                  break;
 406   2              case 5:  
 407   2                  WriteIO(RegTimerClock,0x0b); 
 408   2                  WriteIO(RegTimerReload,0xff);
 409   2                  break;
 410   2              case 6:                       
 411   2                  WriteIO(RegTimerClock,0x0d); 
 412   2                  WriteIO(RegTimerReload,0xff);
 413   2                  break;
 414   2              case 7:                      
 415   2                  WriteIO(RegTimerClock,0x0f); 
 416   2                  WriteIO(RegTimerReload,0xff);
 417   2                  break;
 418   2              default:                       
 419   2                  WriteIO(RegTimerClock,0x07); 
 420   2                  WriteIO(RegTimerReload,tmoLength);
 421   2                  break;
 422   2          }     
 423   1      }
 424          
 425          ///////////////////////////////////////////////////////////////////////
 426          // Request Command defined in ISO14443(Mifare)
 427          ///////////////////////////////////////////////////////////////////////
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 8   

 428          char  M500PcdCmd(uchar cmd,
 429                         volatile uchar data *rcv,
 430                          MfCmdInfo idata *info)
 431          {
 432   1          char          idata status    = MI_OK;
 433   1          char          idata tmpStatus ;
 434   1          uchar idata lastBits;
 435   1          unsigned int  idata timecnt = 0;
 436   1          uchar idata irqEn = 0x00;
 437   1          uchar idata waitFor = 0x00;
 438   1          uchar idata timerCtl  = 0x00;
 439   1      
 440   1          WriteIO(RegInterruptEn,0x7F);			//Clear Int
 441   1          WriteIO(RegInterruptRq,0x7F);
 442   1          WriteIO(RegCommand,PCD_IDLE); 			// (0x00) No action: cancel current command
 443   1      
 444   1          FlushFIFO();     
 445   1          MpIsrInfo = info;  
 446   1          MpIsrOut = rcv;
 447   1          info->irqSource = 0x00;
 448   1          switch(cmd)
 449   1          {
 450   2              case PCD_IDLE:                
 451   2                  irqEn = 0x00;
 452   2                  waitFor = 0x00;
 453   2                  break;
 454   2              case PCD_WRITEE2:            
 455   2                  irqEn = 0x11;
 456   2                  waitFor = 0x10;
 457   2                  break;
 458   2              case PCD_READE2:     
 459   2                  irqEn = 0x07;
 460   2                  waitFor = 0x04;
 461   2                  break;
 462   2              case PCD_LOADCONFIG:  
 463   2              case PCD_LOADKEYE2:   
 464   2              case PCD_AUTHENT1:    
 465   2                  irqEn = 0x05;
 466   2                  waitFor = 0x04;
 467   2                  break;
 468   2              case PCD_CALCCRC:
 469   2                  irqEn = 0x11;
 470   2                  waitFor = 0x10;
 471   2                  break;
 472   2              case PCD_AUTHENT2: 
 473   2                  irqEn = 0x04;
 474   2                  waitFor = 0x04;
 475   2                  break;
 476   2              case PCD_RECEIVE:     
 477   2                  info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
 478   2                  irqEn = 0x06;
 479   2                  waitFor = 0x04;
 480   2                  break;
 481   2              case PCD_LOADKEY: 
 482   2                  irqEn = 0x05;
 483   2                  waitFor = 0x04;
 484   2                  break;
 485   2              case PCD_TRANSMIT: 
 486   2                  irqEn = 0x05;
 487   2                  waitFor = 0x04;
 488   2                  break;
 489   2              case PCD_TRANSCEIVE: 
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 9   

 490   2      	        info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
 491   2                  irqEn = 0x3D;
 492   2                  waitFor = 0x04;
 493   2                  break;
 494   2              default:
 495   2                  status = MI_UNKNOWN_COMMAND;
 496   2          }        
 497   1          if (status == MI_OK)
 498   1          {
 499   2              irqEn |= 0x20;                     
 500   2              waitFor |= 0x20;      
 501   2              timecnt=1000;
 502   2              WriteIO(RegInterruptEn,irqEn | 0x80); 
 503   2              WriteIO(RegCommand,cmd);     
 504   2              while (!(MpIsrInfo->irqSource & waitFor||!(timecnt--)));
 505   2              WriteIO(RegInterruptEn,0x7F);         
 506   2              WriteIO(RegInterruptRq,0x7F);          
 507   2              SetBitMask(RegControl,0x04);          
 508   2              WriteIO(RegCommand,PCD_IDLE);         
 509   2              if (!(MpIsrInfo->irqSource & waitFor))   
 510   2              {                               
 511   3                  status = MI_ACCESSTIMEOUT;
 512   3              }
 513   2              else
 514   2              {
 515   3                  status = MpIsrInfo->status;           
 516   3              }
 517   2              if (status == MI_OK)                    
 518   2              {
 519   3                  if (tmpStatus = (ReadIO(RegErrorFlag) & 0x17))
 520   3                  {
 521   4                      if (tmpStatus & 0x01)
 522   4                      {
 523   5                          info->collPos = ReadIO(RegCollPos);
 524   5                          status = MI_COLLERR;
 525   5                      }
 526   4                      else
 527   4                      {
 528   5                          info->collPos = 0;
 529   5                          if (tmpStatus & 0x02)  
 530   5                          {
 531   6                              status = MI_PARITYERR;
 532   6                          }
 533   5                      }
 534   4                      if (tmpStatus & 0x04)   
 535   4                      {
 536   5                          status = MI_FRAMINGERR;
 537   5                      }
 538   4                      if (tmpStatus & 0x10)   
 539   4                      {
 540   5                          FlushFIFO();
 541   5                          status = MI_OVFLERR;
 542   5                      }
 543   4       	            if (tmpStatus & 0x08) 
 544   4      	            {
 545   5                          status = MI_CRCERR;
 546   5      	            }	
 547   4                      if (status == MI_OK)
 548   4                          status = MI_NY_IMPLEMENTED;
 549   4                  }
 550   3                  if (cmd == PCD_TRANSCEIVE)
 551   3                  {
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 10  

 552   4                      lastBits = ReadIO(RegSecondaryStatus) & 0x07;
 553   4                      if (lastBits)
 554   4                          info->nBitsReceived += (info->nBytesReceived-1) * 8 + lastBits;
 555   4                      else
 556   4                          info->nBitsReceived += info->nBytesReceived * 8;
 557   4                  }
 558   3              }
 559   2              else
 560   2              {
 561   3                  info->collPos = 0x00;
 562   3              }
 563   2          }
 564   1          MpIsrInfo = 0;         
 565   1          MpIsrOut  = 0;
 566   1          return status;
 567   1      }   
 568          
 569          ///////////////////////////////////////////////////////////////////////
 570          // 置一个bit
 571          ///////////////////////////////////////////////////////////////////////
 572          char SetBitMask(uchar reg,uchar mask)  
 573          {
 574   1          char idata tmp = 0x00;
 575   1      

⌨️ 快捷键说明

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