"); //-->
前言
支持MIFARE标准器件,如S50、S70,UID卡,支持MIFARE Classic加密。支持MIFARE更高速的非接触式通信,双向数据传输速率高达424kbit/s。内部64字节的发送和接收FIFO缓冲区。10Mbit/s的SPI接口I2C接口,快速模式的速率为400kbit/s,高速模式的速率为3400kbit/s串行UART,传输速率高达1228.8kbit/s, 帧取决于RS232接口,电压电平取决于提供的管脚电压
ESP32开发板与MFRC522模块接线
GPIO5 | SDA引脚作为SPI通信时的CS片选 |
GPIO18 | SCK |
GPIO23 | MOSI |
GPIO19 | MISO |
不接 | IRQ |
GND | GND |
GPIO21 | RST |
3V3 | 3.3V |
读取S50 IC白卡与S50 IC钥匙卡扣原始数据
#include <MFRC522v2.h>#include <MFRC522DriverSPI.h>//#include <MFRC522DriverI2C.h>#include <MFRC522DriverPinSimple.h>#include <MFRC522Debug.h>// Learn more about using SPI/I2C or check the pin assigment for your board: https://github.com/OSSLibraries/Arduino_MFRC522v2#pin-layoutMFRC522DriverPinSimple ss_pin(5);MFRC522DriverSPI driver{ss_pin}; // Create SPI driver//MFRC522DriverI2C driver{}; // Create I2C driverMFRC522 mfrc522{driver}; // Create MFRC522 instancevoid setup() { Serial.begin(115200); // Initialize serial communication while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4). mfrc522.PCD_Init(); // Init MFRC522 board. MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial); // Show details of PCD - MFRC522 Card Reader details. Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));}void loop() { // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. if (!mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards. if (!mfrc522.PICC_ReadCardSerial()) { return; } // Dump debug info about the card; PICC_HaltA() is automatically called. MFRC522Debug::PICC_DumpToSerial(mfrc522, Serial, &(mfrc522.uid)); delay(2000);}
读写用户数据到指定的存储块
#include <MFRC522v2.h>#include <MFRC522DriverSPI.h>//#include <MFRC522DriverI2C.h>#include <MFRC522DriverPinSimple.h>#include <MFRC522Debug.h>// Learn more about using SPI/I2C or check the pin assigment for your board: https://github.com/OSSLibraries/Arduino_MFRC522v2#pin-layoutMFRC522DriverPinSimple ss_pin(5);MFRC522DriverSPI driver{ss_pin}; // Create SPI driver//MFRC522DriverI2C driver{}; // Create I2C driverMFRC522 mfrc522{driver}; // Create MFRC522 instanceMFRC522::MIFARE_Key key;byte blockAddress = 2;byte newBlockData[17] = {"www.yourcee.com"};//byte newBlockData[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // CLEAR DATAbyte bufferblocksize = 18;byte blockDataRead[18];void setup() { Serial.begin(115200); // Initialize serial communication while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4). mfrc522.PCD_Init(); // Init MFRC522 board. Serial.println(F("Warning: this example overwrites a block in your card, use with care!")); // Prepare key - all keys are set to FFFFFFFFFFFF at chip delivery from the factory. for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; }}void loop() { // Check if a new card is present if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) { delay(500); return; } // Display card UID Serial.print("----------------\nCard UID: "); MFRC522Debug::PrintUID(Serial, (mfrc522.uid)); Serial.println(); // Authenticate the specified block using KEY_A = 0x60 if (mfrc522.PCD_Authenticate(0x60, blockAddress, &key, &(mfrc522.uid)) != 0) { Serial.println("Authentication failed."); return; } // Write data to the specified block if (mfrc522.MIFARE_Write(blockAddress, newBlockData, 16) != 0) { Serial.println("Write failed."); } else { Serial.print("Data written successfully in block: "); Serial.println(blockAddress); } // Authenticate the specified block using KEY_A = 0x60 if (mfrc522.PCD_Authenticate(0x60, blockAddress, &key, &(mfrc522.uid)) != 0) { Serial.println("Authentication failed."); return; } // Read data from the specified block if (mfrc522.MIFARE_Read(blockAddress, blockDataRead, &bufferblocksize) != 0) { Serial.println("Read failed."); } else { Serial.println("Read successfully!"); Serial.print("Data in block "); Serial.print(blockAddress); Serial.print(": "); for (byte i = 0; i < 16; i++) { Serial.print((char)blockDataRead[i]); // Print as character } Serial.println(); } // Halt communication with the card mfrc522.PICC_HaltA(); mfrc522.PCD_StopCrypto1(); delay(2000); // Delay for readability}
总结
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。