RFID_RC522.ino 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Description: Use the RFID Unit to read the Fudan card ID and display the ID on the screen.
  3. */
  4. #include <Wire.h>
  5. #include "MFRC522_I2C.h"
  6. #include <M5Stack.h>
  7. // 0x28 is i2c address on SDA. Check your address with i2cscanner if not match.
  8. MFRC522 mfrc522(0x28); // Create MFRC522 instance.
  9. void setup() {
  10. M5.begin();
  11. M5.Power.begin();
  12. M5.Lcd.fillScreen( BLACK );
  13. M5.Lcd.setCursor(0, 0);
  14. M5.Lcd.setTextColor(YELLOW);
  15. M5.Lcd.setTextSize(2);
  16. M5.Lcd.fillScreen( BLACK );
  17. M5.Lcd.setCursor(0, 0);
  18. M5.Lcd.println("M5StackFire MFRC522");
  19. Serial.begin(115200); // Initialize serial communications with the PC
  20. Wire.begin(); // Initialize I2C
  21. mfrc522.PCD_Init(); // Init MFRC522
  22. ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
  23. Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
  24. M5.Lcd.println("Scan PICC to see UID, type, and data blocks...");
  25. }
  26. void loop() {
  27. // Look for new cards, and select one if present
  28. if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
  29. delay(50);
  30. return;
  31. }
  32. // Now a card is selected. The UID and SAK is in mfrc522.uid.
  33. // Dump UID
  34. Serial.print(F("Card UID:"));
  35. M5.Lcd.println(" ");
  36. for (byte i = 0; i < mfrc522.uid.size; i++) {
  37. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  38. Serial.print(mfrc522.uid.uidByte[i], HEX);
  39. M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  40. M5.Lcd.print(mfrc522.uid.uidByte[i], HEX);
  41. }
  42. Serial.println();
  43. }
  44. void ShowReaderDetails() {
  45. // Get the MFRC522 software version
  46. byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  47. Serial.print(F("MFRC522 Software Version: 0x"));
  48. Serial.print(v, HEX);
  49. if (v == 0x91)
  50. Serial.print(F(" = v1.0"));
  51. else if (v == 0x92)
  52. Serial.print(F(" = v2.0"));
  53. else
  54. Serial.print(F(" (unknown)"));
  55. Serial.println("");
  56. // When 0x00 or 0xFF is returned, communication probably failed
  57. if ((v == 0x00) || (v == 0xFF)) {
  58. Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
  59. }
  60. }