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

📄 main.lst

📁 51控制RC500读写程序源代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
 576   1          tmp = ReadIO(reg);
 577   1          WriteIO(reg,tmp | mask);  // set bit mask
 578   1          return 0x00;
 579   1      }
 580          
 581          ///////////////////////////////////////////////////////////////////////
 582          // 清一个bit
 583          ///////////////////////////////////////////////////////////////////////
 584          char ClearBitMask(uchar reg,uchar mask)  
 585          {
 586   1          char idata tmp = 0x00;
 587   1      
 588   1          tmp = ReadIO(reg);
 589   1          WriteIO(reg,tmp & ~mask);  // clear bit mask
 590   1          return 0x00;
 591   1      }
 592          
 593          ///////////////////////////////////////////////////////////////////////
 594          //清除FIFO
 595          ///////////////////////////////////////////////////////////////////////
 596          void FlushFIFO(void)
 597          {  
 598   1          SetBitMask(RegControl,0x01);
 599   1      }
 600          
 601          ///////////////////////////////////////////////////////////////////////
 602          // Value format operations for Mifare Standard card ICs
 603          // 块值操作:加.减
 604          ///////////////////////////////////////////////////////////////////////
 605          char M500PiccValue(uchar dd_mode, 
 606                             uchar addr, 
 607                             uchar *value,
 608                             uchar trans_addr)
 609          {
 610   1          char status = MI_OK;
 611   1      
 612   1          M500PcdSetTmo(1); 
 613   1          ResetInfo(MInfo);   
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 11  

 614   1          SerBuffer[0] = dd_mode;      
 615   1          SerBuffer[1] = addr;
 616   1          MInfo.nBytesToSend = 2;
 617   1          status = M500PcdCmd(PCD_TRANSCEIVE,
 618   1                             SerBuffer,
 619   1                             &MInfo);
 620   1      
 621   1          if (status != MI_NOTAGERR)   
 622   1          {
 623   2              if (MInfo.nBitsReceived != 4)  
 624   2              {
 625   3                  status = MI_BITCOUNTERR;
 626   3              }
 627   2              else                    
 628   2              {
 629   3                  SerBuffer[0] &= 0x0f; 
 630   3                  switch(SerBuffer[0])
 631   3                  {
 632   4                      case 0x00: 
 633   4                          status = MI_NOTAUTHERR;
 634   4                          break;
 635   4                      case 0x0a:
 636   4                          status = MI_OK;
 637   4                          break;
 638   4                      case 0x01:
 639   4                          status = MI_VALERR;
 640   4                          break;
 641   4                      default:
 642   4                          status = MI_CODEERR;
 643   4                          break;
 644   4                  }
 645   3              }
 646   2          }
 647   1      
 648   1          if ( status == MI_OK)
 649   1          {
 650   2              M500PcdSetTmo(3);    
 651   2              ResetInfo(MInfo);   
 652   2              memcpy(SerBuffer,value,4);
 653   2              MInfo.nBytesToSend   = 4;
 654   2              status = M500PcdCmd(PCD_TRANSCEIVE,
 655   2                                  SerBuffer,
 656   2                                  &MInfo);
 657   2              
 658   2              if (status == MI_OK)    
 659   2              {
 660   3                  if (MInfo.nBitsReceived != 4)   
 661   3                  {
 662   4                      status = MI_BITCOUNTERR;
 663   4                  }
 664   3                  else                    
 665   3                  {
 666   4                      SerBuffer[0] &= 0x0f; 
 667   4                      switch(SerBuffer[0])
 668   4                      {
 669   5                          case 0x00: 
 670   5                              status = MI_NOTAUTHERR;
 671   5                              break;
 672   5                          case 0x01:
 673   5                              status = MI_VALERR;
 674   5                              break;
 675   5                          default:
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 12  

 676   5                              status = MI_CODEERR;
 677   5                              break;
 678   5                      }
 679   4                  }
 680   3              }        
 681   2              else
 682   2              {
 683   3                  if (status == MI_NOTAGERR )
 684   3                      status = MI_OK;  
 685   3              }
 686   2          }
 687   1          if (status == MI_OK)
 688   1          {
 689   2              ResetInfo(MInfo);   
 690   2              SerBuffer[0] = PICC_TRANSFER;      
 691   2              SerBuffer[1] = trans_addr;
 692   2              MInfo.nBytesToSend   = 2;
 693   2              status = M500PcdCmd(PCD_TRANSCEIVE,
 694   2                                  SerBuffer,
 695   2                                  &MInfo);
 696   2              if (status != MI_NOTAGERR)  
 697   2              {
 698   3                  if (MInfo.nBitsReceived != 4)
 699   3                  {
 700   4                      status = MI_BITCOUNTERR;
 701   4                  }
 702   3                  else                   
 703   3                  {
 704   4                      SerBuffer[0] &= 0x0f; 
 705   4                      switch(SerBuffer[0])
 706   4                      {
 707   5                          case 0x00: 
 708   5                              status = MI_NOTAUTHERR;
 709   5                              break;
 710   5                          case 0x0a:
 711   5                              status = MI_OK;
 712   5                              break;
 713   5                          case 0x01:
 714   5                              status = MI_VALERR;
 715   5                              break;
 716   5                          default:
 717   5                              status = MI_CODEERR;
 718   5                              break;
 719   5                      }
 720   4                  }
 721   3              }        
 722   2          }
 723   1          return status;
 724   1      }
 725          
 726          ///////////////////////////////////////////////////////////////////////
 727          //HALT the card
 728          // 终止卡的操作
 729          ///////////////////////////////////////////////////////////////////////
 730          char M500PiccHalt(void)
 731          {
 732   1          char idata status = MI_CODEERR;
 733   1      
 734   1          // ************* Cmd Sequence ********************************** 
 735   1          ResetInfo(MInfo);   
 736   1          SerBuffer[0] = PICC_HALT ;      // Halt command code
 737   1          SerBuffer[1] = 0x00;            // dummy address
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 13  

 738   1          MInfo.nBytesToSend = 2;
 739   1          status = M500PcdCmd(PCD_TRANSCEIVE,
 740   1                             SerBuffer,
 741   1                             &MInfo);   
 742   1          if (status)
 743   1          {
 744   2              // timeout error ==> no NAK received ==> OK
 745   2              if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
 746   2              {
 747   3                  status = MI_OK;
 748   3              }
 749   2          }
 750   1          //reset command register - no response from tag
 751   1          WriteIO(RegCommand,PCD_IDLE);
 752   1          return status; 
 753   1      }
 754          
 755          ///////////////////////////////////////////////////////////////////////
 756          // Reset the MF RC500 
 757          ///////////////////////////////////////////////////////////////////////
 758          char M500PcdReset(void)
 759          {
 760   1          char idata status = MI_OK;
 761   1          unsigned int idata timecnt=0;
 762   1          
 763   1          RC500RST = 0;  
 764   1          delay_1ms(25); 
 765   1          RC500RST = 1;  
 766   1          delay_50us(200); 
 767   1          RC500RST = 0;  
 768   1          delay_50us(50);
 769   1          timecnt=1000;
 770   1          while ((ReadIO(RegCommand) & 0x3F) && timecnt--);
 771   1          if(!timecnt)
 772   1          {
 773   2              status = MI_RESETERR;
 774   2          }
 775   1          if (status == MI_OK)
 776   1          {
 777   2              //WriteIO(RegPage,0x80);
 778   2              if (ReadIO(RegCommand) != 0x00)
 779   2              {                           
 780   3                  status = MI_INTERFACEERR;
 781   3              }
 782   2          }
 783   1          return status;
 784   1      }
 785          
 786          ///////////////////////////////////////////////////////////////////////
 787          // Configures the MF RC500  配置RC500内部寄存器函数
 788          ///////////////////////////////////////////////////////////////////////
 789          char M500PcdConfig(void)
 790          {
 791   1          char idata status;
 792   1      
 793   1          if ((status = M500PcdReset()) == MI_OK)
 794   1          {
 795   2              WriteIO(RegClockQControl,0x00);
 796   2              WriteIO(RegClockQControl,0x40);
 797   2              delay_50us(2);  
 798   2              ClearBitMask(RegClockQControl,0x40);
 799   2              WriteIO(RegBitPhase,0xAD);      
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 14  

 800   2              WriteIO(RegRxThreshold,0xFF);   
 801   2              WriteIO(RegRxControl2,0x01);
 802   2              WriteIO(RegFIFOLevel,0x1A);
 803   2              WriteIO(RegTimerControl,0x02);
 804   2              WriteIO(RegIRqPinConfig,0x03); 
 805   2              M500PcdRfReset(1);               
 806   2          }
 807   1          return status;
 808   1      }
 809          
 810          ///////////////////////////////////////////////////////////////
 811          // Key loading into the MF RC500's EEPROM
 812          ///////////////////////////////////////////////////////////////		            
 813          char M500PcdLoadKeyE2(uchar key_type,
 814                                 uchar sector,
 815                                 uchar *uncoded_keys)
 816          {
 817   1          signed char status = MI_OK;
 818   1          unsigned int e2addr = 0x80 + sector * 0x18;
 819   1          uchar coded_keys[12];
 820   1      
 821   1          if (key_type == PICC_AUTHENT1B)
 822   1          {
 823   2              e2addr += 12;           // key B offset
 824   2          }
 825   1          if ((status = M500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
 826   1          {
 827   2              status = PcdWriteE2(e2addr,12,coded_keys);
 828   2          }
 829   1          return status;
 830   1      }
 831          
 832          ///////////////////////////////////////////////////////////////////////
 833          // Write the MF RC500's EEPROM
 834          // 向RC500 EEPROM写数据
 835          ///////////////////////////////////////////////////////////////////////
 836          char PcdWriteE2(unsigned int startaddr,
 837                          uchar length,
 838                          uchar* _data)
 839          {
 840   1          char status = MI_OK;
 841   1          ResetInfo(MInfo);
 842   1          SerBuffer[0] = startaddr & 0xFF;
 843   1          SerBuffer[1] = (startaddr >> 8) & 0xFF;
 844   1          memcpy(SerBuffer + 2,_data,length);
 845   1      
 846   1          MInfo.nBytesToSend   = length + 2;
 847   1               
 848   1          status = M500PcdCmd(PCD_WRITEE2,
 849   1                             SerBuffer,
 850   1                             &MInfo); 
 851   1          return status;
 852   1      }
 853          
 854          ///////////////////////////////////////////////////////////////////////
 855          // Select Command defined in ISO14443(MIFARE)
 856          ///////////////////////////////////////////////////////////////////////
 857          char M500PcdMfOutSelect(uchar type)
 858          {
 859   1          WriteIO(RegMfOutSelect,type&0x7);
 860   1          return MI_OK;
 861   1      }
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 15  

 862          
 863          ///////////////////////////////////////////////////////////////////////
 864          // Request Command defined in ISO14443(MIFARE)
 865          // Request
 866          // 寻卡

⌨️ 快捷键说明

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