Read RFID by Arduino

From Imperial College Robotics Society
Jump to: navigation, search


Written by Thomas Branch, Adeline Zeng and Harry Eakins

  1. #include <Wire.h>
  2.  
  3.  
  4. #define ADDRESS 0x50
  5. #define RESETPIN 5
  6. #define INTERUPT 0 /* Digital Pin 2*/
  7.  
  8.  
  9. /* Transmit Macro e.g: trans({0x02,0x20,0x22}); */
  10. #define trans(...) \
  11.  byte arr[] = __VA_ARGS__;\
  12.  Wire.beginTransmission(ADDRESS);\
  13.  Wire.send(arr,sizeof(arr));\
  14.  Wire.endTransmission();\
  15.  delay(50);
  16.  
  17. void readCardSerial(){
  18.    trans({0x02,0x20,0x22});
  19.    Wire.requestFrom(ADDRESS, 7);
  20.    while(Wire.available())    // slave may send less than requested
  21.    {
  22.      byte b = Wire.receive();
  23.      printhex(b);
  24.    }
  25. }
  26.  
  27. void cardfound(){
  28.  Serial.print("cardfound ^_^\n");
  29.  interrupts();
  30.  readCardSerial();
  31.  Serial.print("\n");
  32. }
  33.  
  34. void setup() {
  35.  Serial.begin(9600);
  36.  Wire.begin();
  37.  Serial.print("Setup Finished\n");
  38.  attachInterrupt(INTERUPT,cardfound, FALLING);
  39.  
  40.  pinMode(RESETPIN /*reset pin*/, OUTPUT);
  41.  digitalWrite(RESETPIN, HIGH);
  42. }
  43.  
  44. void hardwareReset(){
  45.  Serial.print("Resetting(hardware):\n");
  46.  digitalWrite(RESETPIN, LOW);
  47.  delay(10);
  48.  digitalWrite(RESETPIN, HIGH);
  49. }
  50.  
  51. void hardwareSleep(){
  52.  Serial.print("Sleeping(hardware):\n");
  53.  trans({0x02,0x03,0x01});
  54.  Wire.requestFrom(ADDRESS, 3);
  55.  while(Wire.available())    // slave may send less than requested
  56.  {
  57.    byte b = Wire.receive();
  58.    //Serial.print(b, HEX);
  59.    printhex(b);
  60.  }
  61. }
  62.  
  63. void printhex(byte b){
  64.  if(b < 0x10){
  65.    Serial.print(0, HEX);
  66.  }
  67.  Serial.print(b, HEX);
  68. }
  69.  
  70. void printSerialNumber(){
  71.  trans({0x02,0x01,0x03});
  72.  Wire.requestFrom(ADDRESS, 11);
  73.  while(Wire.available())    // slave may send less than requested
  74.  {
  75.    byte b = Wire.receive();
  76.    //Serial.print(b, HEX);
  77.    printhex(b);
  78.  }
  79. }
  80.  
  81. void printModelNumber(){
  82.  trans({0x02,0x02,0x00});
  83.  Wire.requestFrom(ADDRESS, 7);
  84.  while(Wire.available())    // slave may send less than requested
  85.  {
  86.    byte b = Wire.receive();
  87.    //Serial.print(b, HEX);
  88.    printhex(b);
  89.  }
  90. }
  91.  
  92. void printFirmwareNumber(){
  93.  trans({0x02,0x10,0x12});
  94.  Wire.requestFrom(ADDRESS, 7);
  95.  while(Wire.available())    // slave may send less than requested
  96.  {
  97.    byte b = Wire.receive();
  98.    //Serial.print(b, HEX);
  99.    printhex(b);
  100.  }
  101. }
  102.  
  103. void softwareSleep(){
  104.  Serial.print("Sleeping(Software):\n");
  105.  trans({0x03,0x11,0x00,0x12});
  106. }
  107.  
  108. void softwareReset(){
  109.  Serial.print("Resetting(software):\n");
  110.  trans({0x03,0x11,0xFF, 0x03^0x11^0xFF});
  111. }
  112.  
  113.  
  114. void loop(){
  115.  while(true){
  116.    Serial.print("Loop:\n");
  117.    Serial.print("SerialNumber:");
  118.    printSerialNumber();
  119.    Serial.print("\n");
  120.  
  121.    Serial.print("ModelNumber:");
  122.    printModelNumber();
  123.    Serial.print("\n");
  124.  
  125.    hardwareSleep();
  126.    delay(2000);
  127.    hardwareReset();
  128.    delay(2000);
  129.  
  130.    Serial.print("FirmwareNumber:");
  131.    printFirmwareNumber();
  132.    Serial.print("\n");
  133.  
  134.    softwareSleep();
  135.    delay(2000);
  136.    softwareReset();
  137.    delay(2000);
  138.  
  139.    Serial.print("AutoCardSearch:\n");
  140.  
  141.    trans({0x03,0x13,0x00,0x10});
  142.    Wire.requestFrom(ADDRESS, 3);
  143.    while(Wire.available())    // slave may send less than requested
  144.    {
  145.      byte b = Wire.receive();
  146.      //Serial.print(b, HEX);
  147.      printhex(b);
  148.    }
  149.    delay(2000);
  150.  
  151.    delay(5000);
  152.  }
  153. }