کد:
//Read And Write by Tim Tolar
//enjoy and fell free to change code. Thanks!
//RFID pinout
//Reset RST 9
//SS SDA 10
//MOSI MOSI 11
//MISO MISO 12
//SCK SCK 13
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h> //library for lcd
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int BackLight = 8;
void setup() {
pinMode(BackLight, OUTPUT);
digitalWrite(BackLight, HIGH);
lcd.begin(16,2);
delay(100);
lcd.setCursor(0,0);
lcd.println(F("Read And Write "));
lcd.setCursor(0,1);
lcd.println(F("by TIM TOLAR :) "));
delay(2000);
lcd.clear();
SPI.begin();
mfrc522.PCD_Init();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;}
lcd.setCursor(0,0);
lcd.println(F("Scan a MIFARE !"));
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("Using key A or B!"));
delay(1000);
lcd.clear();
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
lcd.setCursor(0,0);
lcd.println(";)Files will be ");
lcd.setCursor(0,1);
lcd.println(F("writen in sector"));
delay(1000);
lcd.clear();
}
void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
if ( ! mfrc522.PICC_ReadCardSerial())
return;
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.println(F("Card UID: "));
lcd.setCursor(0,1);
delay(1000);
lcd.println("MIFARE 522 ");
delay(1000);
lcd.clear();
dump_byte_array(mfrc522.uid.uidByte,mfrc522.uid.size);
delay(1000);
lcd.setCursor(0,0);
lcd.println(F("PICC type: "));
lcd.setCursor(0,1);
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
lcd.print(mfrc522.PICC_GetTypeName(piccType));
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println(F("only works with MIFARE."));
return;}
delay(1000);
lcd.clear();
byte sector = 1;
byte blockAddr = 4;
byte dataBlock[] = {
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x08, 0x09, 0xff, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f
};
byte trailerBlock = 7;
byte status;
byte buffer[18];
byte size = sizeof(buffer);
lcd.clear();
lcd.setCursor(0,0);
lcd.println(F("checking key A !"));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
lcd.setCursor(0,1);
lcd.print(F("checking failed: "));}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(mfrc522.GetStatusCodeName(status));
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println(F("Current data in sector:"));
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("Reading data "));
lcd.setCursor(0,1);
lcd.print(F("from block "));
lcd.print(blockAddr);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
lcd.setCursor(0,0);
lcd.print(F("failed"));
delay(1000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(mfrc522.GetStatusCodeName(status));}
lcd.setCursor(0,0);
lcd.print(F("Data in block "));
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(blockAddr);
lcd.setCursor(0,1);
dump_byte_array(buffer, 16);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("singing key B..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
lcd.setCursor(0,1);
lcd.print(F("PCD_Authenticate() failed: "));
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println(mfrc522.GetStatusCodeName(status));
return;}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("Writing data "));
lcd.setCursor(0,1);
lcd.print(blockAddr);
dump_byte_array(dataBlock, 16);
delay(1000);
lcd.clear();
status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) {
lcd.setCursor(0,0);
lcd.print(F("MIFARE_Write() failed: "));
lcd.print(mfrc522.GetStatusCodeName(status));}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("Reading data from block "));
lcd.setCursor(0,1);
lcd.print(blockAddr);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
lcd.print(F("failed"));
lcd.setCursor(0,1);
lcd.println(mfrc522.GetStatusCodeName(status));}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("Data in block "));
lcd.setCursor(0,1);
lcd.print(blockAddr);
dump_byte_array(buffer, 16);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println(F("Checking result..."));
byte count = 0;
for (byte i = 0; i < 16; i++) {
if (buffer[i] == dataBlock[i])
count++;}
lcd.setCursor(0,1);
lcd.print(F("Number of bytes that match = "));
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(count);
if (count == 16) {
lcd.setCursor(0,1);
lcd.print(F("Success :-)"));
} else {
lcd.print(F("Failure, no match :-("));
lcd.print(F(" perhaps the write didn't work properly..."));}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("thanks see you");
digitalWrite(BackLight, LOW);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
lcd.print(buffer[i] < 0x10 ? " 0" : " ");
}
}
رفرنس: