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

📄 main.lst

📁 51控制RC500读写程序源代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
1160   1          return status;
1161   1      }
1162          
1163          ////////////////////////////////////////////////////////////////
1164          // Read the mifare card
1165          // 读卡
1166          ////////////////////////////////////////////////////////////////
1167          char M500PiccRead(uchar addr,uchar *_data)
1168          {
1169   1          char idata status = MI_OK;
1170   1          char idata tmp    = 0;
1171   1      
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 20  

1172   1          FlushFIFO();    
1173   1      
1174   1          M500PcdSetTmo(3);  
1175   1          WriteIO(RegChannelRedundancy,0x0F);
1176   1          ResetInfo(MInfo);   
1177   1          SerBuffer[0] = PICC_READ;   
1178   1          SerBuffer[1] = addr;
1179   1          MInfo.nBytesToSend   = 2;   
1180   1          status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
1181   1      
1182   1          if (status != MI_OK)
1183   1          {
1184   2              if (status != MI_NOTAGERR ) 
1185   2              {
1186   3                  if (MInfo.nBitsReceived == 4)  
1187   3                  {
1188   4                      SerBuffer[0] &= 0x0f;  
1189   4                      if ((SerBuffer[0] & 0x0a) == 0)
1190   4                      {
1191   5                          status = MI_NOTAUTHERR;
1192   5                      }
1193   4                      else
1194   4                      {
1195   5                          status = MI_CODEERR;
1196   5                      }
1197   4                  }
1198   3              }
1199   2              memcpy(_data,"0000000000000000",16); 
1200   2          }
1201   1          else                // Response Processing
1202   1          {
1203   2              if (MInfo.nBytesReceived != 16)
1204   2              {
1205   3                  status = MI_BYTECOUNTERR;
1206   3                  memcpy(_data,"0000000000000000",16);
1207   3              }
1208   2              else
1209   2              {
1210   3                  memcpy(_data,SerBuffer,16);
1211   3              }
1212   2          }
1213   1          M500PcdSetTmo(1);             
1214   1          return status; 
1215   1      }
1216          
1217          ////////////////////////////////////////////////////////////////
1218          // Write the mifare card
1219          // 写卡  下载密码
1220          ////////////////////////////////////////////////////////////////
1221          char M500PiccWrite( uchar addr,uchar *_data)
1222          {
1223   1          char idata status = MI_OK;
1224   1          
1225   1          ResetInfo(MInfo);   
1226   1          SerBuffer[0] = PICC_WRITE; 
1227   1          SerBuffer[1] = addr;            
1228   1          MInfo.nBytesToSend   = 2;
1229   1          status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
1230   1      
1231   1          if (status != MI_NOTAGERR)   
1232   1          {
1233   2              if (MInfo.nBitsReceived != 4)  
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 21  

1234   2              {
1235   3                  status = MI_BITCOUNTERR;
1236   3              }
1237   2              else                     
1238   2              {
1239   3                  SerBuffer[0] &= 0x0f; 
1240   3                  if ((SerBuffer[0] & 0x0a) == 0)
1241   3                  {
1242   4                      status = MI_NOTAUTHERR;                   
1243   4                  }
1244   3                  else
1245   3                  {
1246   4                      if (SerBuffer[0] == 0x0a)
1247   4                      {
1248   5                          status = MI_OK;
1249   5                      }
1250   4                      else 
1251   4                      {
1252   5                          status = MI_CODEERR;
1253   5                      }
1254   4                  }
1255   3              }
1256   2          }
1257   1      
1258   1          if ( status == MI_OK)
1259   1          {
1260   2              M500PcdSetTmo(3);    
1261   2      
1262   2              ResetInfo(MInfo);   
1263   2              memcpy(SerBuffer,_data,16);
1264   2              MInfo.nBytesToSend   = 16;
1265   2              status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
1266   2              
1267   2              if (status & 0x80)   
1268   2              {
1269   3                  status = MI_NOTAGERR;
1270   3              }
1271   2              else
1272   2              {
1273   3                  if (MInfo.nBitsReceived != 4) 
1274   3                  {
1275   4                      status = MI_BITCOUNTERR;
1276   4                  }
1277   3                  else                  
1278   3                  {
1279   4                      SerBuffer[0] &= 0x0f; 
1280   4                      if ((SerBuffer[0] & 0x0a) == 0)
1281   4                      {
1282   5                          status = MI_WRITEERR;
1283   5                      }
1284   4                      else
1285   4                      {
1286   5                          if (SerBuffer[0] == 0x0a)
1287   5                          {
1288   6                              status = MI_OK;
1289   6                          }
1290   5                          else 
1291   5                          {
1292   6                              status = MI_CODEERR;
1293   6                          }
1294   5                      }     
1295   4                  }
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 22  

1296   3              }        
1297   2              M500PcdSetTmo(1);  
1298   2          }
1299   1          return status;
1300   1      }
1301          
1302          ///////////////////////////////////////////////////////////////////////
1303          // Reset Rf Card
1304          ///////////////////////////////////////////////////////////////////////
1305          char M500PcdRfReset(uchar ms)
1306          {
1307   1          char idata status = MI_OK;
1308   1      	
1309   1          if(ms)
1310   1          {
1311   2              ClearBitMask(RegTxControl,0x03); 
1312   2              delay_1ms(2);                
1313   2              SetBitMask(RegTxControl,0x03); 
1314   2          }
1315   1          else
1316   1          {
1317   2              ClearBitMask(RegTxControl,0x03);  
1318   2          }
1319   1          return status;
1320   1      }
1321          
1322          #pragma noaregs
1323          
1324          ///////////////////////////////////////////////////////////////////////
1325          // Delay 50us
1326          ///////////////////////////////////////////////////////////////////////
1327          void delay_50us(uchar _50us)
1328          {
1329   1        RCAP2LH = RCAP2_50us;
1330   1        T2LH    = RCAP2_50us;
1331   1        ET2 = 0; 	// Disable timer2 interrupt
1332   1        T2CON = 0x04;	// 16-bit auto-reload, clear TF2, start timer
1333   1        
1334   1        while (_50us--)
1335   1        {
1336   2      	while (!TF2);
1337   2      	TF2 = FALSE;
1338   2        }
1339   1      
1340   1        TR2 = FALSE;
1341   1      
1342   1      }
1343          
1344          ///////////////////////////////////////////////////////////////////////
1345          // Delay 1ms
1346          ///////////////////////////////////////////////////////////////////////
1347          void delay_1ms(uchar _1ms)
1348          {
1349   1        RCAP2LH = RCAP2_1ms;
1350   1        T2LH    = RCAP2_1ms;
1351   1        ET2 = 0; 	// Disable timer2 interrupt
1352   1        T2CON = 0x04;	// 16-bit auto-reload, clear TF2, start timer
1353   1        
1354   1        while (_1ms--)
1355   1        {
1356   2      	while (!TF2);
1357   2      	TF2 = FALSE;
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 23  

1358   2        }
1359   1        TR2 = FALSE;
1360   1      }
1361          
1362          ///////////////////////////////////////////////////////////////////////
1363          // Delay 10ms
1364          ///////////////////////////////////////////////////////////////////////
1365          void delay_10ms(uint _10ms)
1366          {
1367   1        RCAP2LH = RCAP2_10ms;
1368   1        T2LH    = RCAP2_10ms;
1369   1        ET2 = 0; 	// Disable timer2 interrupt
1370   1        T2CON = 0x04;	// 16-bit auto-reload, clear TF2, start timer
1371   1        
1372   1        while (_10ms--)
1373   1        {
1374   2      	while (!TF2)
1375   2      	{
1376   3      	  if (bCmd)
1377   3      	  {
1378   4      		TR2 = FALSE;
1379   4      		TF2 = FALSE;
1380   4      		return;
1381   4      	  }
1382   3      	}
1383   2      	TF2 = FALSE;
1384   2        }
1385   1        TR2 = FALSE;
1386   1      
1387   1      }
1388          
1389          ///////////////////////////////////////////////////////////////////////
1390          ///////////////////////////////////////////////////////////////////////
1391          void RC500ISR (void) interrupt 0 using 1    
1392          {
1393   1          static uchar idata irqBits;
1394   1          static uchar idata irqMask;            
1395   1          static uchar idata nbytes;
1396   1          static uchar idata cnt;
1397   1      	
1398   1          IE0 = 0; 	
1399   1          WriteRawIO(0,0x80);
1400   1          if (MpIsrInfo && MpIsrOut)
1401   1          {
1402   2              while( ReadRawIO(RegPrimaryStatus) & 0x08) 
1403   2              {
1404   3                  irqMask = ReadRawIO(RegInterruptEn);
1405   3                  irqBits = ReadRawIO(RegInterruptRq) & irqMask;
1406   3                  MpIsrInfo->irqSource |= irqBits; 
1407   3                  if (irqBits & 0x01)   
1408   3                  {  
1409   4                      nbytes = 64 - ReadRawIO(RegFIFOLength);
1410   4                      if ((MpIsrInfo->nBytesToSend - MpIsrInfo->nBytesSent) <= nbytes)
1411   4                      {
1412   5                          nbytes = MpIsrInfo->nBytesToSend - MpIsrInfo->nBytesSent;
1413   5                          WriteRawIO(RegInterruptEn,0x01);
1414   5                      }   
1415   4                      for ( cnt = 0;cnt < nbytes;cnt++)
1416   4                      {
1417   5                          WriteRawIO(RegFIFOData,MpIsrOut[MpIsrInfo->nBytesSent]);
1418   5                          MpIsrInfo->nBytesSent++;
1419   5                      }
C51 COMPILER V6.12  MAIN                                                                   08/18/2007 23:20:49 PAGE 24  

1420   4                      WriteRawIO(RegInterruptRq,0x01);  
1421   4                  }      
1422   3                  if (irqBits & 0x10)    
1423   3                  {
1424   4                      WriteRawIO(RegInterruptRq,0x10);    
1425   4                      WriteRawIO(RegInterruptEn,0x82);   
1426   4                      if (MpIsrInfo->cmd == PICC_ANTICOLL1) 	
1427   4      	            {                                        
1428   5                          WriteIO(RegChannelRedundancy,0x02);
1429   5                          WriteRawIO(0,0x80);
1430   5      	            }	
1431   4                  }
1432   3                  if (irqBits & 0x0E) 
1433   3                  {
1434   4                      nbytes = ReadRawIO(RegFIFOLength);
1435   4                      for ( cnt = 0; cnt < nbytes; cnt++)               
1436   4                      {
1437   5                          MpIsrOut[MpIsrInfo->nBytesReceived] = ReadRawIO(RegFIFOData);
1438   5                          MpIsrInfo->nBytesReceived++;
1439   5                      }
1440   4                      WriteRawIO(RegInterruptRq,0x0A & irqBits); 
1441   4                  }   
1442   3                  if (irqBits & 0x04) 
1443   3                  {
1444   4                      WriteRawIO(RegInterruptEn,0x20); 
1445   4                      WriteRawIO(RegInterruptRq,0x20); 
1446   4                      irqBits &= ~0x20; 
1447   4                      MpIsrInfo->irqSource &= ~0x20;
1448   4                      WriteRawIO(RegInterruptRq,0x04);
1449   4                  }
1450   3                  if (irqBit

⌨️ 快捷键说明

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