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

📄 rc500.lst

📁 RC500的全部操作,读卡
💻 LST
📖 第 1 页 / 共 4 页
字号:
C51 COMPILER V7.06   RC500                                                                 12/15/2008 16:23:27 PAGE 6   

 304   3                  if (tmpStatus = (ReadIO(RegErrorFlag) & 0x17))
 305   3                  {
 306   4                      if (tmpStatus & 0x01)
 307   4                      {
 308   5                          info->collPos = ReadIO(RegCollPos);
 309   5                          status = MI_COLLERR;
 310   5                      }
 311   4                      else
 312   4                      {
 313   5                          info->collPos = 0;
 314   5                          if (tmpStatus & 0x02)  
 315   5                          {
 316   6                              status = MI_PARITYERR;
 317   6                          }
 318   5                      }
 319   4                      if (tmpStatus & 0x04)   
 320   4                      {
 321   5                          status = MI_FRAMINGERR;
 322   5                      }
 323   4                      if (tmpStatus & 0x10)   
 324   4                      {
 325   5                          FlushFIFO();
 326   5                          status = MI_OVFLERR;
 327   5                      }
 328   4                          if (tmpStatus & 0x08) 
 329   4                          {
 330   5                          status = MI_CRCERR;
 331   5                          }   
 332   4                      if (status == MI_OK)
 333   4                          status = MI_NY_IMPLEMENTED;
 334   4                  }
 335   3                  if (cmd == PCD_TRANSCEIVE)
 336   3                  {
 337   4                      lastBits = ReadIO(RegSecondaryStatus) & 0x07;
 338   4                      if (lastBits)
 339   4                          info->nBitsReceived += (info->nBytesReceived-1) * 8 + lastBits;
 340   4                      else
 341   4                          info->nBitsReceived += info->nBytesReceived * 8;
 342   4                  }
 343   3              }
 344   2              else
 345   2              {
 346   3                  info->collPos = 0x00;
 347   3              }
 348   2          }
 349   1          MpIsrInfo = 0;         
 350   1          MpIsrOut  = 0;
 351   1          return status;
 352   1      }   
 353          
 354          ///////////////////////////////////////////////////////////////////////
 355          // 置一个bit
 356          ///////////////////////////////////////////////////////////////////////
 357          char SetBitMask(unsigned char reg,unsigned char mask)  
 358          {
 359   1          char idata tmp = 0x00;
 360   1      
 361   1          tmp = ReadIO(reg);
 362   1          WriteIO(reg,tmp | mask);  // set bit mask
 363   1          return 0x00;
 364   1      }
 365          
C51 COMPILER V7.06   RC500                                                                 12/15/2008 16:23:27 PAGE 7   

 366          ///////////////////////////////////////////////////////////////////////
 367          // 清一个bit
 368          ///////////////////////////////////////////////////////////////////////
 369          char ClearBitMask(unsigned char reg,unsigned char mask)  
 370          {
 371   1          char idata tmp = 0x00;
 372   1      
 373   1          tmp = ReadIO(reg);
 374   1          WriteIO(reg,tmp & ~mask);  // clear bit mask
 375   1          return 0x00;
 376   1      }
 377          
 378          ///////////////////////////////////////////////////////////////////////
 379          //清除FIFO
 380          ///////////////////////////////////////////////////////////////////////
 381          void FlushFIFO(void)
 382          {  
 383   1          SetBitMask(RegControl,0x01);
 384   1      }
 385          
 386          ///////////////////////////////////////////////////////////////////////
 387          // Value format operations for Mifare Standard card ICs
 388          ///////////////////////////////////////////////////////////////////////
 389          /*char M500PiccValue(unsigned char dd_mode, 
 390                             unsigned char addr, 
 391                             unsigned char *value,
 392                             unsigned char trans_addr)
 393          {
 394              char status = MI_OK;
 395          
 396              M500PcdSetTmo(1); 
 397              ResetInfo(MInfo);   
 398              SerBuffer[0] = dd_mode;      
 399              SerBuffer[1] = addr;
 400              MInfo.nBytesToSend = 2;
 401              status = M500PcdCmd(PCD_TRANSCEIVE,
 402                                 SerBuffer,
 403                                 &MInfo);
 404          
 405              if (status != MI_NOTAGERR)   
 406              {
 407                  if (MInfo.nBitsReceived != 4)  
 408                  {
 409                      status = MI_BITCOUNTERR;
 410                  }
 411                  else                    
 412                  {
 413                      SerBuffer[0] &= 0x0f; 
 414                      switch(SerBuffer[0])
 415                      {
 416                          case 0x00: 
 417                              status = MI_NOTAUTHERR;
 418                              break;
 419                          case 0x0a:
 420                              status = MI_OK;
 421                              break;
 422                          case 0x01:
 423                              status = MI_VALERR;
 424                              break;
 425                          default:
 426                              status = MI_CODEERR;
 427                              break;
C51 COMPILER V7.06   RC500                                                                 12/15/2008 16:23:27 PAGE 8   

 428                      }
 429                  }
 430              }
 431          
 432              if ( status == MI_OK)
 433              {
 434                  M500PcdSetTmo(3);    
 435                  ResetInfo(MInfo);   
 436                  memcpy(SerBuffer,value,4);
 437                  MInfo.nBytesToSend   = 4;
 438                  status = M500PcdCmd(PCD_TRANSCEIVE,
 439                                      SerBuffer,
 440                                      &MInfo);
 441                  
 442                  if (status == MI_OK)    
 443                  {
 444                      if (MInfo.nBitsReceived != 4)   
 445                      {
 446                          status = MI_BITCOUNTERR;
 447                      }
 448                      else                    
 449                      {
 450                          SerBuffer[0] &= 0x0f; 
 451                          switch(SerBuffer[0])
 452                          {
 453                              case 0x00: 
 454                                  status = MI_NOTAUTHERR;
 455                                  break;
 456                              case 0x01:
 457                                  status = MI_VALERR;
 458                                  break;
 459                              default:
 460                                  status = MI_CODEERR;
 461                                  break;
 462                          }
 463                      }
 464                  }        
 465                  else
 466                  {
 467                      if (status == MI_NOTAGERR )
 468                          status = MI_OK;  
 469                  }
 470              }
 471              if (status == MI_OK)
 472              {
 473                  ResetInfo(MInfo);   
 474                  SerBuffer[0] = PICC_TRANSFER;      
 475                  SerBuffer[1] = trans_addr;
 476                  MInfo.nBytesToSend   = 2;
 477                  status = M500PcdCmd(PCD_TRANSCEIVE,
 478                                      SerBuffer,
 479                                      &MInfo);
 480                  if (status != MI_NOTAGERR)  
 481                  {
 482                      if (MInfo.nBitsReceived != 4)
 483                      {
 484                          status = MI_BITCOUNTERR;
 485                      }
 486                      else                   
 487                      {
 488                          SerBuffer[0] &= 0x0f; 
 489                          switch(SerBuffer[0])
C51 COMPILER V7.06   RC500                                                                 12/15/2008 16:23:27 PAGE 9   

 490                          {
 491                              case 0x00: 
 492                                  status = MI_NOTAUTHERR;
 493                                  break;
 494                              case 0x0a:
 495                                  status = MI_OK;
 496                                  break;
 497                              case 0x01:
 498                                  status = MI_VALERR;
 499                                  break;
 500                              default:
 501                                  status = MI_CODEERR;
 502                                  break;
 503                          }
 504                      }
 505                  }        
 506              }
 507              return status;
 508          }*/
 509          
 510          ///////////////////////////////////////////////////////////////////////
 511          // Set card in HALT-state
 512          // 终止卡的操作
 513          ///////////////////////////////////////////////////////////////////////
 514          char M500PiccHalt(void)
 515          {
 516   1          char idata status = MI_CODEERR;
 517   1      
 518   1          // ************* Cmd Sequence ********************************** 
 519   1          ResetInfo(MInfo);   
 520   1          SerBuffer[0] = PICC_HALT ;      // Halt command code
 521   1          SerBuffer[1] = 0x00;            // dummy address
 522   1          MInfo.nBytesToSend = 2;
 523   1          status = M500PcdCmd(PCD_TRANSCEIVE,
 524   1                             SerBuffer,
 525   1                             &MInfo);   
 526   1          if (status)
 527   1          {
 528   2              // timeout error ==> no NAK received ==> OK
 529   2              if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
 530   2              {
 531   3                  status = MI_OK;
 532   3              }
 533   2          }
 534   1          //reset command register - no response from tag
 535   1          WriteIO(RegCommand,PCD_IDLE);       //启动和停止命令的执行
 536   1          return status; 
 537   1      }
 538          
 539          ///////////////////////////////////////////////////////////////////////
 540          // Reset the MF RC500
 541          //重启RC500 
 542          ///////////////////////////////////////////////////////////////////////
 543          char M500PcdReset(void)
 544          {
 545   1          char idata status = MI_OK;
 546   1          unsigned int idata timecnt=0;
 547   1          
 548   1          RC500RST = 0;  
 549   1          delay_ms(25); 
 550   1          RC500RST = 1;  
 551   1          delay_ms(10); 
C51 COMPILER V7.06   RC500                                                                 12/15/2008 16:23:27 PAGE 10  

 552   1          RC500RST = 0;  
 553   1          delay_ms(10);
 554   1          timecnt=1000;
 555   1          while ((ReadIO(RegCommand) & 0x3F) && timecnt--);
 556   1          if(!timecnt)
 557   1          {
 558   2              status = MI_RESETERR;
 559   2          }
 560   1          if (status == MI_OK)
 561   1          {
 562   2              //WriteIO(RegPage,0x80);
 563   2              if (ReadIO(RegCommand) != 0x00)
 564   2              {                           
 565   3                  status = MI_INTERFACEERR;
 566   3              }
 567   2          }
 568   1          return status;
 569   1      }
 570          
 571          ///////////////////////////////////////////////////////////////////////
 572          // Configures the MF RC500
 573          ///////////////////////////////////////////////////////////////////////
 574          char M500PcdConfig(void)
 575          {
 576   1          char idata status;
 577   1      
 578   1          if ((status = M500PcdReset()) == MI_OK)
 579   1          {
 580   2              WriteIO(RegClockQControl,0x00);
 581   2              WriteIO(RegClockQControl,0x40);
 582   2              delay_100us();  
 583   2              ClearBitMask(RegClockQControl,0x40);
 584   2              WriteIO(RegBitPhase,0xAD);      
 585   2              WriteIO(RegRxThreshold,0xFF);   
 586   2              WriteIO(RegRxControl2,0x01);
 587   2              WriteIO(RegFIFOLevel,0x1A);
 588   2              WriteIO(RegTimerControl,0x02);
 589   2              WriteIO(RegIRqPinConfig,0x03); 
 590   2              M500PcdRfReset(1);               
 591   2          }
 592   1          return status;
 593   1      }
 594          
 595          ///////////////////////////////////////////////////////////////
 596          // Key loading into the MF RC500's EEPROM
 597          //将一个密匙从E2PROM 复制到密匙缓冲区
 598          ///////////////////////////////////////////////////////////////                     
 599          /*char M500PcdLoadKeyE2(unsigned char key_type,
 600                                 unsigned char sector,
 601                                 unsigned char *uncoded_keys)
 602          {
 603              signed char status = MI_OK;
 604              unsigned int e2addr = 0x80 + sector * 0x18;
 605              unsigned char coded_keys[12];
 606          
 607              if (key_type == PICC_AUTHENT1B)
 608              {
 609                  e2addr += 12;           // key B offset
 610              }
 611              if ((status = M500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
 612              {
 613                  status = PcdWriteE2(e2addr,12,coded_keys);

⌨️ 快捷键说明

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