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

📄 card.lst

📁 此程序为13.56MHz的RFID底层读写卡程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V4.01,  CARD                                                                  19/08/04  14:33:21  PAGE 1   


DOS C51 COMPILER V4.01, COMPILATION OF MODULE CARD
OBJECT MODULE PLACED IN D:\13505B\105B\CARD.OBJ
COMPILER INVOKED BY: C:\COMP51\C51.EXE D:\13505B\105B\CARD.C DB SB OE

stmt level    source

   1          #include <string.h>
   2          #include "Hardware.h"
   3          #include <absacc.h>
   4          #include "RcComm.h"
   5          #include "PcdShare.h"
   6          #include "PcdUtils.h"
   7          #include "MfReg.h"
   8          #include "MfRc500.h"
   9          #include "MfErrNo.h"
  10          #include <intrins.h>
  11          extern void   W_DOG(void);
  12          extern void   SendMsError(void);
  13          extern void   SendResponse(uchar status,uchar len,uchar *datapr);
  14          extern uchar  picc_b_rats (uchar *ats_part);
  15          extern uchar xdata Auth_code[48];
  16          extern uchar xdata MADMessage[32];
  17          extern uchar xdata ComBuffer[MAXLEN];
  18          extern uchar idata GPB;
  19          extern uchar idata snr[5];
  20          uchar  Block_Index;
  21          uchar  Open_next(void);
  22          
  23          
  24          
  25          
  26          
  27          /********************************************************************
  28          * Function:		OpenCard								*
  29          *																	*
  30          * input:														*
  31          * output:		snr													*
  32          ********************************************************************/
  33          
  34          
  35          
  36          uchar OpenCard(uchar ins_type)
  37          {
  38   1          uchar Status,buf[4],i0,ins_mark;
  39   1           W_DOG();
  40   1          if(ins_type==1)
  41   1          {
  42   2          	ins_mark=PICC_REQALL;
  43   2          }
  44   1          else ins_mark=PICC_REQIDL;
  45   1      
  46   1          for(i0=0;i0<6;i0++)
  47   1          {
  48   2      
  49   2            Status=Mf500PiccRequest(ins_mark,buf);    //  ins_mark
  50   2      
  51   2            if(Status==MI_OK) break;
  52   2          }
  53   1      
  54   1          if(Status!=MI_OK)
  55   1          {
  56   2             SendResponse(0x00,1,buf);  /* no card */
  57   2             return Status;
  58   2          }
  59   1      
C51 COMPILER V4.01,  CARD                                                                  19/08/04  14:33:21  PAGE 2   

  60   1          Status=Mf500PiccAnticoll (0,snr);
  61   1          if(Status==MI_OK)
  62   1          {
  63   2            Status=Mf500PiccSelect(snr,buf);
  64   2            if(Status==MI_OK)
  65   2            {
  66   3            	snr[4]=buf[0];
  67   3            	if(snr[4]==0x20)     //mifare pro
  68   3            	{
  69   4            	   Status=picc_b_rats (buf);        //comand request ATS 2002.11.26
  70   4                 if(Status!=MI_OK)
  71   4                 {
  72   5                    SendResponse(0x80,1,buf);
  73   5                    return Status;
  74   5                 }
  75   4                 Block_Index=0;
  76   4      
  77   4                 SendResponse(0x01,6,snr);
  78   4                 return MIF_PRO;
  79   4              }
  80   3      
  81   3              if(ins_type==0xF0) return MI_OK;
  82   3              Status=Mf500PiccHalt();
  83   3              for(i0=0;i0<6;i0++)
  84   3              {
  85   4                 Status=Mf500PiccRequest(PICC_REQIDL,buf);
  86   4                 if(Status==MI_OK)
  87   4                 {
  88   5                   Status=Mf500PiccAnticoll (0,buf);
  89   5                   if(Status==MI_OK)
  90   5                   {
  91   6                     if(memcmp(snr,buf,4)!=0) break;   /*if snr(4byte) is different from
  92   6                                                              buf(4byte)  break*/
  93   6                     Status=Mf500PiccSelect(buf,buf);
  94   6                     Status=Mf500PiccHalt();
  95   6                     Status=MI_SINGLE_CARD;
  96   6                   }
  97   5                 }
  98   4              }
  99   3              if(Status==MI_OK)
 100   3              {
 101   4      
 102   4                 SendResponse(0x02,1,buf); /* any card */
 103   4                 return MI_ANY_CARD;
 104   4              }
 105   3              else
 106   3              {
 107   4                 Status=Open_next();
 108   4                 if(Status!=MI_OK) SendResponse(0x00,1,buf);
 109   4                 return Status;
 110   4              }
 111   3            }
 112   2          }
 113   1          SendResponse(0x80,1,buf);
 114   1          return Status;
 115   1      }
 116          
 117          /********************************************************************
 118          * Function:		Open_next
 119          *																	*
 120          * input:														*
 121          * output:		status													*
 122          ********************************************************************/
 123          uchar Open_next(void)
 124          {
 125   1         uchar Status,buf[4],i5;
C51 COMPILER V4.01,  CARD                                                                  19/08/04  14:33:21  PAGE 3   

 126   1         W_DOG();
 127   1         Status=Mf500PiccHalt();
 128   1         for(i5=0;i5<6;i5++)
 129   1         {
 130   2            Status=Mf500PiccRequest(PICC_REQALL,buf);
 131   2            if(Status==MI_OK) break;
 132   2         }
 133   1         if(Status==MI_OK)
 134   1         {
 135   2            Status=Mf500PiccSelect(snr,buf);
 136   2            if(Status==MI_OK)   return MI_OK;
 137   2         }
 138   1         return MI_OPEN_ERROR;
 139   1      }
 140          
 141          
 142          
 143          uchar MF1_auth(uchar keytype_set,uchar sector_no,uchar block_no)
 144          {
 145   1         uchar Status;
 146   1          W_DOG();
 147   1          block_no=sector_no<<2;
 148   1         Status=Mf500PiccAuth(keytype_set,sector_no,block_no);//key in RAM to auth
 149   1         if(Status==MI_OK)  return MI_OK;
 150   1      
 151   1         Status=Open_next();
 152   1         if(Status!=MI_OK)
 153   1         {
 154   2               return MI_OPEN_ERROR;
 155   2         }
 156   1      
 157   1         Status=Mf500PiccAuth(keytype_set,sector_no,block_no);
 158   1      
 159   1         if(Status!=MI_OK) return  MI_AUTH_ERROR;
 160   1         return MI_OK;
 161   1      }
 162          
 163          uchar MF1_authkey(char key_type,char *key,char block_n)
 164          {
 165   1         uchar idata Status,buf4[12];
 166   1      
 167   1         W_DOG();
 168   1       
 169   1         Status=Mf500HostCodeKey(key,buf4);
 170   1         if(Status!=MI_OK)
 171   1          {
 172   2           SendResponse(0x81,1,buf4);
 173   2           return MI_HOSTCODE_ERROR;
 174   2         }
 175   1         Status=Mf500PiccAuthKey(key_type,snr,buf4,block_n);
 176   1         if(Status==MI_OK)  return MI_OK;
 177   1      
 178   1         Status=Open_next();
 179   1         if(Status!=MI_OK)
 180   1         {
 181   2               return MI_OPEN_ERROR;
 182   2         }
 183   1      
 184   1         Status=Mf500PiccAuthKey(key_type,snr,buf4,block_n);
 185   1      
 186   1         if(Status!=MI_OK) return  MI_AUTH_ERROR;
 187   1         return MI_OK;
 188   1      }
 189          
 190          uchar MF1_read_key(uchar keytype_set,uchar block_n,uchar *databuf,char *key)
 191          {
C51 COMPILER V4.01,  CARD                                                                  19/08/04  14:33:21  PAGE 4   

 192   1         uchar Status,sector_n,i2;
 193   1         W_DOG();
 194   1         sector_n=(block_n>>2);
 195   1         Status=Mf500PiccRead(block_n,databuf);
 196   1         if(Status!=MI_OK)
 197   1         {
 198   2         	for(i2=0;i2<3;i2++)
 199   2         	{
 200   3         	   Status=Open_next();
 201   3         	   if(Status!=MI_OK)
 202   3         	   {
 203   4         	      Status=MI_OPEN_ERROR;
 204   4      	      continue;
 205   4      	   }
 206   3      
 207   3                 Status=MF1_authkey(keytype_set,key,block_n);
 208   3      	   if(Status!=MI_OK)
 209   3      	   {
 210   4      	      Status=MI_AUTH_ERROR;
 211   4      	      continue;
 212   4      	   }
 213   3                Status=Mf500PiccRead(block_n,databuf);
 214   3      	   if(Status==MI_OK) break;
 215   3      	}
 216   2         }
 217   1      
 218   1         return Status;
 219   1      }
 220          
 221          uchar MF1_write_key(uchar keytype_set,uchar block_n,uchar *databuf,char *key)
 222          {
 223   1         uchar Status,sector_n,i3;
 224   1         W_DOG();
 225   1         sector_n=(block_n>>2);
 226   1      
 227   1         Status=Mf500PiccWrite(block_n,databuf);
 228   1         if(Status!=MI_OK)
 229   1         {
 230   2         	for(i3=0;i3<3;i3++)
 231   2         	{
 232   3         	   Status=Open_next();
 233   3         	   if(Status!=MI_OK)
 234   3         	   {
 235   4         	      Status=MI_OPEN_ERROR;
 236   4      	      continue;
 237   4      	   }
 238   3      
 239   3                 Status=MF1_authkey(keytype_set,key,block_n);
 240   3      	   if(Status!=MI_OK)
 241   3      	   {
 242   4      	      Status=MI_AUTH_ERROR;
 243   4      	      continue;
 244   4      	   }
 245   3      
 246   3                Status=Mf500PiccWrite(block_n,databuf);
 247   3      	   if(Status==MI_OK) break;
 248   3      	}
 249   2         }
 250   1      
 251   1         return Status;
 252   1      }
 253          
 254          uchar MF1_read(uchar keytype_set,uchar block_n,uchar *databuf)
 255          {
 256   1         uchar Status,sector_n,i2;
 257   1         W_DOG();
C51 COMPILER V4.01,  CARD                                                                  19/08/04  14:33:21  PAGE 5   

 258   1         sector_n=(block_n>>2);
 259   1         Status=Mf500PiccRead(block_n,databuf);
 260   1         if(Status!=MI_OK)
 261   1         {
 262   2         	for(i2=0;i2<3;i2++)
 263   2         	{
 264   3         	   Status=Open_next();
 265   3         	   if(Status!=MI_OK)
 266   3         	   {
 267   4         	      Status=MI_OPEN_ERROR;
 268   4      	      continue;
 269   4      	   }
 270   3      	   Status=Mf500PiccAuth(keytype_set,sector_n,block_n);
 271   3      	   if(Status!=MI_OK)
 272   3      	   {
 273   4      	      Status=MI_AUTH_ERROR;
 274   4      	      continue;
 275   4      	   }
 276   3                Status=Mf500PiccRead(block_n,databuf);
 277   3      	   if(Status==MI_OK) break;
 278   3      	}
 279   2         }
 280   1      
 281   1         return Status;
 282   1      }
 283          
 284          uchar MF1_write(uchar keytype_set,uchar block_n,uchar *databuf)
 285          {
 286   1         uchar Status,sector_n,i3;
 287   1         W_DOG();
 288   1         sector_n=(block_n>>2);
 289   1      
 290   1         Status=Mf500PiccWrite(block_n,databuf);
 291   1         if(Status!=MI_OK)
 292   1         {
 293   2         	for(i3=0;i3<3;i3++)
 294   2         	{
 295   3         	   Status=Open_next();
 296   3         	   if(Status!=MI_OK)
 297   3         	   {
 298   4         	      Status=MI_OPEN_ERROR;
 299   4      	      continue;
 300   4      	   }
 301   3      
 302   3                Status=Mf500PiccAuth(keytype_set,sector_n,block_n);
 303   3      	   if(Status!=MI_OK)
 304   3      	   {
 305   4      	      Status=MI_AUTH_ERROR;
 306   4      	      continue;
 307   4      	   }
 308   3      
 309   3                Status=Mf500PiccWrite(block_n,databuf);
 310   3      	   if(Status==MI_OK) break;
 311   3      	}

⌨️ 快捷键说明

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