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

📄 main.lst

📁 51控制RC500读写程序源代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
 867          ///////////////////////////////////////////////////////////////////////
 868          char M500PiccCommonRequest(uchar req_code,uchar *atq)
 869          {
 870   1          char idata status = MI_OK;
 871   1      	
 872   1          M500PcdSetTmo(3);
 873   1          WriteIO(RegChannelRedundancy,0x03);
 874   1          ClearBitMask(RegControl,0x08);
 875   1          WriteIO(RegBitFraming,0x07);     
 876   1          SetBitMask(RegTxControl,0x03);     
 877   1          ResetInfo(MInfo);
 878   1          SerBuffer[0] = req_code;
 879   1          MInfo.nBytesToSend = 1;   
 880   1          
 881   1          status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
 882   1          if (status)     
 883   1          {
 884   2              *atq = 0;
 885   2          } 
 886   1          else 
 887   1          {
 888   2              if (MInfo.nBitsReceived != 16) 
 889   2              {
 890   3                  *atq = 0;
 891   3                  status = MI_BITCOUNTERR;
 892   3              } 
 893   2              else 
 894   2              {
 895   3                  status = MI_OK;
 896   3                  memcpy(atq,SerBuffer,2);
 897   3              }
 898   2          }
 899   1          return status; 
 900   1      }
 901          
 902          ///////////////////////////////////////////////////////////////////
 903          // Cascaded Anti-Collision Command defined in ISO14443(MIFARE)
 904          // 防冲突 读卡的系列号 CardSnr
 905          ///////////////////////////////////////////////////////////////////
 906          char M500PiccCascAnticoll (uchar bcnt,uchar *snr)       
 907          {
 908   1          char idata status = MI_OK;
 909   1          char idata snr_in[4];    
 910   1          char idata nbytes = 0;
 911   1          char idata nbits = 0;
 912   1          char idata complete = 0;
 913   1          char idata i        = 0;
 914   1          char idata byteOffset = 0;
 915   1          uchar dummyShift1;
 916   1          uchar dummyShift2;
 917   1          
 918   1          M500PcdSetTmo(106);
 919   1          memcpy(snr_in,snr,4);   
 920   1         
 921   1          WriteIO(RegDecoderControl,0x28); 
 922   1          ClearBitMask(RegControl,0x08);   
 923   1          complete = 0;
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 16  

 924   1          while (!complete && (status == MI_OK) )
 925   1          {
 926   2              ResetInfo(MInfo);           
 927   2              WriteIO(RegChannelRedundancy,0x03);
 928   2              nbits = bcnt % 8;
 929   2              if(nbits)
 930   2              {
 931   3                  WriteIO(RegBitFraming,nbits << 4 | nbits);
 932   3                  nbytes = bcnt / 8 + 1;   
 933   3                  if (nbits == 7)
 934   3                  {
 935   4                      MInfo.cmd = PICC_ANTICOLL1;
 936   4                      WriteIO(RegBitFraming,nbits); 
 937   4                  }
 938   3              }  
 939   2              else
 940   2              {
 941   3                  nbytes = bcnt / 8;
 942   3              }
 943   2              SerBuffer[0] = 0x93;
 944   2              SerBuffer[1] = 0x20 + ((bcnt/8) << 4) + nbits;
 945   2                     
 946   2              for (i = 0; i < nbytes; i++)  
 947   2              {
 948   3                  SerBuffer[i + 2] = snr_in[i];
 949   3              }
 950   2              MInfo.nBytesToSend   = 2 + nbytes;   
 951   2       
 952   2              status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
 953   2              if (nbits == 7)
 954   2              {
 955   3                  dummyShift1 = 0x00;
 956   3                  for (i = 0; i < MInfo.nBytesReceived; i++)
 957   3                  {
 958   4                      dummyShift2 = SerBuffer[i];
 959   4                      SerBuffer[i] = (dummyShift1 >> (i+1)) | (SerBuffer[i] << (7-i));
 960   4                      dummyShift1 = dummyShift2;
 961   4                  }
 962   3                  MInfo.nBitsReceived -= MInfo.nBytesReceived;       
 963   3                  if ( MInfo.collPos ) MInfo.collPos += 7 - (MInfo.collPos + 6) / 9;
 964   3              }
 965   2              if ( status == MI_OK || status == MI_COLLERR)    
 966   2              {
 967   3                  if ( MInfo.nBitsReceived != (40 - bcnt) )
 968   3                  {
 969   4                      status = MI_BITCOUNTERR; 
 970   4                  } 
 971   3                  else 
 972   3                  {
 973   4                      byteOffset = 0;
 974   4                      if( nbits != 0 ) 
 975   4                      {
 976   5                          snr_in[nbytes - 1] = snr_in[nbytes - 1] | SerBuffer[0];
 977   5                          byteOffset = 1;
 978   5                      }
 979   4      
 980   4                      for ( i =0; i < (4 - nbytes); i++)     
 981   4                      {
 982   5                          snr_in[nbytes + i] = SerBuffer[i + byteOffset];
 983   5                      }
 984   4        
 985   4                      if (status != MI_COLLERR ) 
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 17  

 986   4                      {
 987   5                          dummyShift2 = snr_in[0] ^ snr_in[1] ^ snr_in[2] ^ snr_in[3];
 988   5                          dummyShift1 = SerBuffer[MInfo.nBytesReceived - 1];
 989   5                          if (dummyShift2 != dummyShift1)
 990   5                          {
 991   6                              status = MI_SERNRERR;
 992   6                          } 
 993   5                          else   
 994   5                          {
 995   6                              complete = 1;
 996   6                          }
 997   5                      }
 998   4                      else                 
 999   4                      {
1000   5                          bcnt = bcnt + MInfo.collPos - nbits;
1001   5                          status = MI_OK;
1002   5                      }
1003   4                  }
1004   3              }
1005   2          }
1006   1          if (status == MI_OK)
1007   1          {
1008   2              memcpy(snr,snr_in,4);
1009   2          }
1010   1          else
1011   1          {
1012   2              memcpy(snr,"0000",4);
1013   2          }
1014   1          ClearBitMask(RegDecoderControl,0x20); 
1015   1         
1016   1          return status;  
1017   1      }
1018          
1019          //////////////////////////////////////////////////////////////////
1020          // Cascaded Select command defined in ISO14443(MIFARE)
1021          // 选择卡 Select Card
1022          //////////////////////////////////////////////////////////////////
1023          char M500PiccCascSelect(uchar *snr,uchar *sak)
1024          {
1025   1          char idata status = MI_OK; 
1026   1       
1027   1          M500PcdSetTmo(106);
1028   1      	
1029   1          WriteIO(RegChannelRedundancy,0x0F); 
1030   1          ClearBitMask(RegControl,0x08);    
1031   1          ResetInfo(MInfo);   
1032   1          SerBuffer[0] = 0x93;
1033   1          SerBuffer[1] = 0x70;        
1034   1         
1035   1          memcpy(SerBuffer + 2,snr,4);
1036   1          SerBuffer[6] = SerBuffer[2] ^ SerBuffer[3] ^ SerBuffer[4] ^ SerBuffer[5];
1037   1          MInfo.nBytesToSend   = 7;
1038   1          status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
1039   1      
1040   1          *sak = 0;   
1041   1          if (status == MI_OK)  
1042   1          {
1043   2              if (MInfo.nBitsReceived != 8)   
1044   2              {
1045   3                  status = MI_BITCOUNTERR;
1046   3              }
1047   2              else
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 18  

1048   2              {
1049   3      	        *sak = SerBuffer[0];
1050   3              }	
1051   2          }
1052   1          return status;
1053   1      }
1054          
1055          ///////////////////////////////////////////////////////////////////////
1056          // Key loading into the MF RC500's EEPROM
1057          // 校验卡密码(E2)
1058          ///////////////////////////////////////////////////////////////////////
1059          char M500PiccAuthE2( uchar auth_mode,uchar *snr,      
1060                               uchar keynr,uchar block)     
1061                                                        
1062          {
1063   1          char idata status = MI_OK;
1064   1          unsigned int e2addr = 0x80 + keynr * 0x18;
1065   1          uchar *e2addrbuf;
1066   1          
1067   1          e2addrbuf = (uchar *)&e2addr;
1068   1          if (auth_mode == PICC_AUTHENT1B)
1069   1          {
1070   2              e2addr += 12; 
1071   2          }
1072   1          FlushFIFO();    
1073   1          ResetInfo(MInfo);
1074   1      
1075   1          memcpy(SerBuffer,e2addrbuf,2); 
1076   1          SerBuffer[2] = SerBuffer[0];      
1077   1          SerBuffer[0] = SerBuffer[1]; 
1078   1          SerBuffer[1] = SerBuffer[2];
1079   1          MInfo.nBytesToSend   = 2;
1080   1          if ((status=M500PcdCmd(PCD_LOADKEYE2,SerBuffer,&MInfo)) == MI_OK)
1081   1          {  
1082   2              status = M500PiccAuthState(auth_mode,snr,block);  
1083   2          }
1084   1          return status;
1085   1      }                        
1086          
1087          ///////////////////////////////////////////////////////////////////////
1088          // Authentication key coding
1089          ///////////////////////////////////////////////////////////////////////
1090          char M500HostCodeKey(  uchar *uncoded, uchar *coded)   
1091          {
1092   1          char idata status = MI_OK;
1093   1          uchar idata cnt = 0;
1094   1          uchar idata ln  = 0;     
1095   1          uchar idata hn  = 0;      
1096   1          for (cnt = 0; cnt < 6; cnt++)
1097   1          {
1098   2              ln = uncoded[cnt] & 0x0F;
1099   2              hn = uncoded[cnt] >> 4;
1100   2              coded[cnt * 2 + 1] = (~ln << 4) | ln;
1101   2              coded[cnt * 2 ] = (~hn << 4) | hn;
1102   2          }
1103   1          return MI_OK;
1104   1      }
1105          
1106          ///////////////////////////////////////////////////////////////////////
1107          //校验状态
1108          ///////////////////////////////////////////////////////////////////////
1109          char M500PiccAuthState( uchar auth_mode,uchar *snr,uchar block)
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 19  

1110          {
1111   1          char idata status = MI_OK;
1112   1          uchar idata i = 0;
1113   1         
1114   1          status = ReadIO(RegErrorFlag);                                     
1115   1          if (status != MI_OK)
1116   1          {
1117   2              if (status & 0x40)           
1118   2              {
1119   3                  status = MI_KEYERR;
1120   3              }
1121   2              else
1122   2              {
1123   3                  status = MI_AUTHERR;    
1124   3              }
1125   2          }
1126   1          else
1127   1          {
1128   2              SerBuffer[0] = auth_mode;    
1129   2      
1130   2              SerBuffer[1] = block;  
1131   2              memcpy(SerBuffer + 2,snr,4); 
1132   2              ResetInfo(MInfo);
1133   2              MInfo.nBytesToSend = 6;
1134   2              if ((status = M500PcdCmd(PCD_AUTHENT1,SerBuffer,&MInfo)) == MI_OK)
1135   2              {
1136   3                  if (ReadIO(RegSecondaryStatus) & 0x07) 
1137   3                  {
1138   4                      status = MI_BITCOUNTERR;
1139   4                  }
1140   3                  else
1141   3                  {
1142   4                      ResetInfo(MInfo);
1143   4                      MInfo.nBytesToSend = 0;
1144   4                      if ((status = M500PcdCmd(PCD_AUTHENT2,
1145   4                                           SerBuffer,
1146   4                                           &MInfo)) == MI_OK) 
1147   4                      {
1148   5                          if ( ReadIO(RegControl) & 0x08 ) 
1149   5                          {
1150   6                              status = MI_OK;
1151   6                          }
1152   5                          else
1153   5                          {
1154   6                              status = MI_AUTHERR;
1155   6                          }
1156   5                      }
1157   4                  }
1158   3              }
1159   2          }

⌨️ 快捷键说明

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