Arduino Mega2560 ENC28J60和RFID无法正常工作

时间:2015-04-28 18:15:02

标签: arduino

我是新来的,所以请不要为我的愚蠢而生气。我有一个项目,我必须阅读RFID UID代码(这部分就像一个魅力),然后将其发送到连接到C#应用程序的数据库,然后接收响应(0或1)。我的问题(目前)是arduino没有发送任何数据。也许有人可以帮助我?

这是我的arduino代码:



#include <EtherCard.h>

#include <SPI.h>      // RC522 Module uses SPI protocol
#include <MFRC522.h>   // Library for Mifare RC522 Devices
#include <DS1302RTC.h>
#include <Time.h>

static byte mymac[] = {0x74,0xDD,0xDD,0x00,0x00,0x01};
static byte server_IP[] = {192,168,0,102};
static int server_Port = 7745;
byte Ethernet::buffer[600];


// Init the DS1302 (external RTC)
// Set pins:  CE, IO,CLK
DS1302RTC RTC(45, 43, 41);

// Optional connection for RTC module
#define DS1302_GND_PIN 39
#define DS1302_VCC_PIN 37

#define COMMON_ANODE
#define LED_ON HIGH
#define LED_OFF LOW

#define redLed 3
#define greenLed 5
#define blueLed 7
#define relay 4


int successRead; // Variable integer to keep if we have Successful Read from Reader

byte readCard[4];           // Stores scanned ID read from RFID Module


/* We need to define MFRC522's pins and create instance
 * Pin layout should be as follows (on Arduino Mega 2560):
 * MOSI: Pin 51 / ICSP-4
 * MISO: Pin 50 / ICSP-1
 * SCK : Pin 52 / ICSP-3
 * SS : Pin 46 (Configurable)
 * RST : Pin 34 (Configurable)
 * look MFRC522 Library for
 * pin configuration for other Arduinos.
 */

#define SS_PIN1 46
#define RST_PIN1 34
MFRC522 mfrc522(SS_PIN1, RST_PIN1);	// Create MFRC522 instance.


///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
  Serial.begin(9600);	 // Initialize serial communications with PC
    Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();
  if (ether.begin(sizeof Ethernet::buffer, mymac, 53)==0) {
    Serial.println( "Failed to access Ethernet controller");
    //while(1); 
  } else Serial.println("Ethernet controller initialized");
 
  if (!ether.dhcpSetup()) {
    Serial.println("Failed to get configuration from DHCP");
    //while(1);
  } else Serial.println("DHCP configuration done:"); 
 
  ether.printIp("My IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.netmask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
  Serial.println();
  
  ether.copyIp(ether.hisip, server_IP);
  ether.hisport = server_Port;
  
  //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW); // Make sure door is locked
  digitalWrite(redLed, LED_OFF); // Make sure led is off
  digitalWrite(greenLed, LED_OFF); // Make sure led is off
  digitalWrite(blueLed, LED_OFF); // Make sure led is off

  //Protocol Configuration
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); //Set Antenna Gain to Max- this will increase reading distance

  // Activate RTC module
  digitalWrite(DS1302_GND_PIN, LOW);
  pinMode(DS1302_GND_PIN, OUTPUT);

  digitalWrite(DS1302_VCC_PIN, HIGH);
  pinMode(DS1302_VCC_PIN, OUTPUT);


  RTC.haltRTC(1); //clock enable (1), clock disable (0)
  RTC.writeEN(0); //Write protection OFF (1), write protection ON (0)

  //Time set
  //setTime(1,19,0,16,12,2014);
  //time_t t = now();
  //RTC.set(t);

  delay(200);
}

///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
  ether.packetLoop(ether.packetReceive());
  do {
    successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0

      normalModeOn(); // Normal mode, blue Power LED is on, all others are off
    
  }
  while (!successRead); //the program will not go further while you not get a successful read
  
  openDoor();

}

///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
int getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
    return 0;
  }
  //Serial.println("Scanned PICC's UID:");
  for (int i = 0; i < 4; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println(readCard[4],HEX);
  char string_temp[7];
  dtostrf (readCard[2], 4, 2, string_temp);
  Stash stash;
  byte sd = stash.create();
  stash.print(string_temp);
  stash.print('\r');
  stash.save();     
  Stash::prepare(PSTR("$H"), sd);
  ether.tcpSend();
    
  Serial.println("Packet sent!");
  
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

//////////////////////////////////////// Normal Mode Leds  ///////////////////////////////////
void normalModeOn () {
  digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
  digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
  digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
  digitalWrite(relay, LOW); // Make sure Door is Locked
}


///////////////////////////////////////// Unlock Door   ///////////////////////////////////
void openDoor() {
  digitalWrite(blueLed, LED_OFF); // Turn off blue LED
  digitalWrite(redLed, LED_OFF); // Turn off red LED
  digitalWrite(greenLed, LED_ON); // Turn on green LED
  digitalWrite(relay, HIGH); // Unlock door!
  delay(1000); // Hold door lock open for given seconds
  digitalWrite(relay, LOW); // Relock door
  delay(1000); // Hold green LED on for 2 more seconds
}

///////////////////////////////////////// Failed Access  ///////////////////////////////////
void failed() {
  digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  digitalWrite(redLed, LED_ON); // Turn on red LED
  delay(2000);

}
&#13;
&#13;
&#13;

我使用EtherCard.h库和ENC28J60以太网模块用于arduino。

1 个答案:

答案 0 :(得分:0)

ENC28J60与Arduino UNO不兼容,当与其他SPI设备一起使用时很可能与MEGA2560兼容。

您应该使用默认使用W5100的Arduino Ethernet Shield。

我遇到了与ENC28J60相同的问题,尝试了很多设置,最后得到了一个W5100,它可以直接使用默认的以太网库。