极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12237|回复: 0

ENC28J60+RC522 ,webserver与RFID共同使用问题。

[复制链接]
发表于 2016-11-11 15:37:46 | 显示全部楼层 |阅读模式
本帖最后由 xvanzhou 于 2016-11-14 17:21 编辑

我现在在尝试让ENC28J60 与RC522 同时使用,用SPI模式。但是始终不成功,只有RC522运行,web一直刷不出来。分开来是都是正常运行的。

源程序如下,RFID片选为4,enc28J60片选为10;

  1. #include <EtherCard.h>
  2. #include <SPI.h>
  3. #include <MFRC522.h>

  4. #define SS_PIN 4
  5. #define RST_PIN 9

  6. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
  7. static byte myip[] = { 192,168,150,203 };

  8. byte Ethernet::buffer[500];
  9. BufferFiller bfill;

  10. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

  11. MFRC522::MIFARE_Key key;

  12. // Init array that will store new NUID
  13. byte nuidPICC[3];

  14. void setup () {
  15.   if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0)
  16.     Serial.println(F("Failed to access Ethernet controller"));
  17.   ether.staticSetup(myip);
  18.   
  19.   Serial.begin(9600);
  20.   SPI.begin(); // Init SPI bus
  21.   rfid.PCD_Init(); // Init MFRC522

  22.   for (byte i = 0; i < 6; i++) {
  23.     key.keyByte[i] = 0xFF;
  24.    
  25.   Serial.println(F("This code scan the MIFARE Classsic NUID."));
  26.   Serial.print(F("Using the following key:"));
  27.   printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  28.   }
  29. pinMode (SS_PIN,OUTPUT);
  30.     digitalWrite (SS_PIN,HIGH);
  31.   pinMode (10,OUTPUT);
  32.     digitalWrite (10,HIGH);

  33. }

  34. static word homePage() {
  35.   long t = millis() / 1000;
  36.   word h = t / 3600;
  37.   byte m = (t / 60) % 60;
  38.   byte s = t % 60;
  39.   bfill = ether.tcpOffset();
  40.   bfill.emit_p(PSTR(
  41.     "HTTP/1.0 200 OK\r\n"
  42.     "Content-Type: text/html\r\n"
  43.     "Pragma: no-cache\r\n"
  44.     "\r\n"
  45.     "<meta http-equiv='refresh' content='1'/>"
  46.     "<title>RBBB server</title>"
  47.     "<h1>$D$D:$D$D:$D$D</h1>"),
  48.       h/10, h%10, m/10, m%10, s/10, s%10);
  49.   return bfill.position();
  50.   
  51.   }



  52. void loop () {
  53.   
  54.   digitalWrite (10,LOW);
  55.   digitalWrite (SS_PIN,HIGH);
  56.   word len = ether.packetReceive();
  57.   word pos = ether.packetLoop(len);
  58.   
  59.   if (pos)  // check if valid tcp data is received
  60.     ether.httpServerReply(homePage()); // send web page data
  61.    
  62.     delay (100);
  63.    //RFID部分
  64.   digitalWrite (10,HIGH);
  65.   digitalWrite (SS_PIN,LOW);
  66.   // Look for new cards
  67.   if ( ! rfid.PICC_IsNewCardPresent())
  68.     return;

  69.   // Verify if the NUID has been readed
  70.   if ( ! rfid.PICC_ReadCardSerial())            //先查到这里的库函数1
  71.     return;

  72.   Serial.print(F("PICC type: "));
  73.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  74.   Serial.println(rfid.PICC_GetTypeName(piccType));

  75.   // Check is the PICC of Classic MIFARE type
  76.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
  77.     piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  78.     piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  79.     Serial.println(F("Your tag is not of type MIFARE Classic."));
  80.     return;
  81.   }

  82.   if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  83.     rfid.uid.uidByte[1] != nuidPICC[1] ||
  84.     rfid.uid.uidByte[2] != nuidPICC[2] ||
  85.     rfid.uid.uidByte[3] != nuidPICC[3] ) {
  86.     Serial.println(F("A new card has been detected."));

  87.     // Store NUID into nuidPICC array
  88.     for (byte i = 0; i < 4; i++) {
  89.       nuidPICC[i] = rfid.uid.uidByte[i];
  90.       
  91.       delay (100);
  92.     }
  93.    
  94.    
  95.     Serial.println(F("The NUID tag is:"));
  96.     Serial.print(F("In hex: "));
  97.     printHex(rfid.uid.uidByte, rfid.uid.size);
  98.     Serial.println();
  99.     Serial.print(F("In dec: "));
  100.     printDec(rfid.uid.uidByte, rfid.uid.size);
  101.     Serial.println();
  102.   }
  103.   else Serial.println(F("Card read previously."));

  104.   // Halt PICC
  105.   rfid.PICC_HaltA();
  106.   delay (100);
  107.   // Stop encryption on PCD
  108.   rfid.PCD_StopCrypto1();
  109.   delay (100);
  110. }


  111. /**
  112. * Helper routine to dump a byte array as hex values to Serial.
  113. */
  114. void printHex(byte *buffer, byte bufferSize) {
  115.   for (byte i = 0; i < bufferSize; i++) {
  116.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  117.     Serial.print(buffer[i], HEX);
  118.   delay (100);
  119.   }
  120. }

  121. /**
  122. * Helper routine to dump a byte array as dec values to Serial.
  123. */
  124. void printDec(byte *buffer, byte bufferSize) {
  125.   for (byte i = 0; i < bufferSize; i++) {
  126.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  127.     Serial.print(buffer[i], DEC);
  128.     delay (100);
  129.   }
  130. }
复制代码




如上,确实已经片选置高置低了,怕是因为buffer的原因,也用了delay,但是还是没法同时运行。具体是什么情况?怎么解决,可QQ私聊,酬谢
QQ:305515099             困扰我很久了 真心烦死


今天再来试,发现我ping地址192.168.150.177 结果是192.168.150.180(主机地址)给我回复无法访问,这是个什么道理?如图3

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-3 09:02 , Processed in 0.040763 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表