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

📄 +

📁 高集成度TYPEA读写器芯片MFRC500及其应用
💻
字号:
我见过philips用89c51rd2单片机编写的typeB程序。代码如下:

#include <string.h>
#include <stdio.h>
#include <main.h>
#include <m500a.h>
#include <p89c51rx.h>
#include <Mfreg531.h>
#include <EvalOs.h>

#define LED    P3_5

unsigned char ReadBuf[64];
unsigned char nkey[6]={0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
//unsigned char nkey[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char FWI=5;
unsigned char BLK=0;

void init(void);
void Blink_LED(void);
char TCL_Protocol(unsigned char CID);
char TCL_Protocol2(unsigned char CID);
void delay_50us(unsigned int _50us);
char CL_Deselect(unsigned char CID);
char CL_GetChallenge(unsigned char CID, unsigned char *Buf);
char CL_SelectFile(unsigned char CID, 
           unsigned char P1, 
           unsigned char P2,
           unsigned char *file, 
           unsigned char *Buf);
void PrepareValue (unsigned long value, unsigned char *buf);

void main(void)
{
    char idata status,temp;
    unsigned char i,atq[2],snr[10],sak,snr_len,TA;
    unsigned char buffer[16],mfout=4,reg=0x3a,regvalue=0x04;
    unsigned char reg2=0x3d,regvalue2=0x0,reg3,regvalue3,regvalue4=0X89;
        
    init();                  // Initialise system
    status = M500PcdConfig();      // Initialise the RC500
                                    // must be call in the initialisation
        M500PcdMfOutSelect(mfout);      // Route serial output to pin, for test

    LED=0;
    delay_50us(10000);
    LED=1;

    // LOADING KEYS
    for(i=0; i<16; i++)
    {
      M500PcdLoadMk(PICC_AUTHENT1A,i,nkey);      // load RAM
      //M500PcdLoadKeyE2(PICC_AUTHENT1A,i,nkey);      // load EEPROM
    }
        
    while(1)
    {
      //M500PcdRfReset(10);
      //delay_50us(1000);
      
      // IS014443B
      M500PcdConfigISOType(TYPEB); 
      status = M531PiccRequestB(PICC_REQBWUP,0,0,ReadBuf); // ReqB with 1 slot
      if(status==MI_OK || status==MI_COLLERR)
      {
        if(status == MI_COLLERR) // more then 1 card
        { 
          // Anti-collision by probabilistic    
          /*for(i=0; i<4; i++)  
          {
            status = M531PiccRequestB(PICC_REQBSTD,0,2,ReadBuf); // ReqB with 4 slots
        if(status==MI_OK) break; 
          }
          if(status!=MI_OK) continue; //error occur
          FWI = ReadBuf[11]>>4; // Set the FWT according to the protocol info         
          status = M531PiccAttrib(&ReadBuf[1],0,0,0);     // Select PICC 
              */
    
          // Anti-collision using slot-marker
          if((status=M531PiccRequestB(PICC_REQBSTD,0,2,ReadBuf)) != MI_OK) // ReqB with 4 slot
          {
        for (i=1; i<4; i++)
              if((status=M531PiccSlotMarker(i,ReadBuf)) == MI_OK) break;    
          }

          if (status != MI_OK) continue;
            }
        Blink_LED();        
        
        FWI = ReadBuf[11]>>4; // Set the FWT according to the protocol info         
            if((status=M531PiccAttrib(&ReadBuf[1],0,0,0,ReadBuf[10]&0x0f))!=MI_OK) continue; // Select PICC 
        status = TCL_Protocol(0);
      }    

      // IS014443A
      M500PcdConfigISOType(TYPEA); 
      while(1)
      { 
        if((status=M500PiccActivateAll(0,atq,&sak,snr,&snr_len))==MI_OK)
        {
          if((sak&0x24) == 0x20)     /* ISO1443A commands, T=CL */
          {
            ReadBuf[0]=0xE0;     // RATS commands
            ReadBuf[1]=0x50;    // FSD=64, CID=0
            if(M500PiccExchangeBlock(ReadBuf,4,ReadBuf,&temp,1,4)==MI_OK) // length incl CRC
            {
              /* ATS received in ReadBuf        
                 START EXCHANGING TRANSPARENT DATA */
          i = 2;
          if(ReadBuf[1]&0x40) TA=ReadBuf[i++];
          else TA=0;
          if(ReadBuf[1]&0x20) FWI=ReadBuf[i++]>>4;
          
          if(!TA)
          {
            ReadBuf[0]=0xd0;     // PPSS commands
                ReadBuf[1]=0x11;    // PPS0
                ReadBuf[2]=0x33;    // PPS1, DRI=8, DSI=8
                if(M500PiccExchangeBlock(ReadBuf,5,ReadBuf,&temp,1,FWI)==MI_OK) // length incl CRC
                {
              M500PcdWriteRegister(0x14,0x01); // ISO14443A, 848KBaud    
                M500PcdWriteRegister(0x19,0x13); // Rcv 848KBaud             
            }    
              }        
              Blink_LED();        
        
              TCL_Protocol(0);
                CL_Deselect(0);

            }
          }
          else               /* Mifare Classic commands */
          {
            if((status=M500PiccAuth(PICC_AUTHENT1A,snr,0,0))!=MI_OK) continue;    
            PrepareValue(100,ReadBuf);
            if(M500PiccWrite(62,ReadBuf)!=MI_OK) continue;
                
            if(M500PiccRead(0,ReadBuf)!=MI_OK) continue;
            PrepareValue(1,ReadBuf);
            if(M500PiccValue(PICC_DECREMENT,62,ReadBuf,62)!=MI_OK) continue;
            if(M500PiccValue(PICC_RESTORE,62,ReadBuf,61)!=MI_OK) continue;
            if(M500PiccRead(61,ReadBuf)!=MI_OK) continue;
            status = M500PiccHalt();
            Blink_LED();        
          }
        }
        break;
      }
    }  
}

void init(void)
{
    LED = 0;
    P2_7 = 0;    // Enable the CS for RC500
    PMR = 0x41;     // For emulation
    IT0 = 1;        // Config ext0 as edge trigger for RC500
    EX0 = 1;     // Enable ext0 interrupt for RC500
    EA = 1;        // Enable global interrupt    
}

char TCL_Protocol(unsigned char CID)
{
    char idata status,temp;
    unsigned char buffer[16];
    unsigned long duration;

    LED = 1;
    buffer[0] = 0x11;
    buffer[1] = 0x22;
    buffer[2] = 0x33;
    buffer[4] = 0x44;
    buffer[5] = 0x55;
    buffer[6] = 0x66;
    WriteInfineonCard (buffer, 0, 6);
     ReadInfineonCard (ReadBuf, 0, 7);
         
    buffer[0]=0x3f;
    buffer[1]=0x00;
    M500PcdStopWatch(1); // start RC500 Timer Stop-watch    
    if(CL_SelectFile(CID,0,0,buffer,ReadBuf)==MI_OK)
    { 
     //    LED = 1;    
    //    LED = 0;
    }
        duration = M500PcdStopWatch(0); // stop RC500 Timer Stop-watch                
    M500PcdStopWatch(1); // start RC500 Timer Stop-watch    
    if(CL_GetChallenge(CID,ReadBuf)==MI_OK)
    { 
     //    LED = 1;    
    //    LED = 0;
    }
    duration = M500PcdStopWatch(0); // stop RC500 Timer Stop-watch    
    delay_50us(1000);
    LED = 0;
    
    return(status);
}

char CL_Deselect(unsigned char CID)
{
    unsigned char len,Buf[6];

    Buf[0]=0xCA;     // S-block PCB with CID
    Buf[1]=CID;      // CID
    if(M500PiccExchangeBlock(Buf,4,Buf,&len,1,FWI)!=MI_OK) return(!MI_OK);
    if(Buf[0]==0x90 && Buf[1]==0x00) return(MI_OK);
    else return(!MI_OK);      
}

char CL_GetChallenge(unsigned char CID, unsigned char *Buf)
{
    unsigned char len;

    Buf[0]=0x0A;     // I-block PCB with CID
    Buf[1]=CID;      // CID
    Buf[2]=0xb8;    // INF:CLA
    Buf[3]=0x84;    // INF:INS
    Buf[4]=0x00;    // INF:P1
    Buf[5]=0x00;    // INF:P2
    Buf[6]=0x04;    // INF:Le
    if(M500PiccExchangeBlock(Buf,9,Buf,&len,1,FWI)!=MI_OK) return(!MI_OK);
    if(Buf[len-4]==0x90) return(MI_OK);
    else return(!MI_OK);      
}

char CL_SelectFile(unsigned char CID, 
           unsigned char P1, 
           unsigned char P2,
           unsigned char *file, 
           unsigned char *Buf)
{
    unsigned char len;
        
    Buf[0]=0x0A;     // I-block PCB with CID
    Buf[1]=CID;      // CID
    Buf[2]=0x00;    // INF:CLA
    Buf[3]=0xA4;    // INF:INS
    Buf[4]=P1;    // INF:P1
    Buf[5]=P2;    // INF:P2

    if(P1&0x04) // Selection by DF name
    {
      len=0;
      while(file[len]!=0)
        Buf[7+len] = file[len++];
      Buf[6]=len;
      len += 9;
    }
    else // Selection by FID
     {
      Buf[6]=2;        // INF:Lc
      Buf[7]=file[0];    // INF:Data
      Buf[8]=file[1];    // INF:Data 
      len = 11;
    }
    if(M500PiccExchangeBlock(Buf,len,Buf,&len,1,FWI)!=MI_OK) return(!MI_OK);
    if(Buf[len-4]==0x90) return(MI_OK);
    else return(!MI_OK);      
}

void PrepareValue (unsigned long value, unsigned char *buf)
{
    unsigned char i;

    for (i=0; i<4; i++)
    {
        buf[i] = buf[8+i] = (unsigned char)(value>>(8*i));
        buf[4+i] = ~buf[i];
    }
    buf[12] = 0;
    buf[13] = 255;
    buf[14] = 0;
    buf[15] = 255;
}

void Blink_LED(void)
{
    LED=0;
    delay_50us(2500);    
    LED=1;
}

⌨️ 快捷键说明

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