Arduino Read a card using a mfrc522 reader
參考
1) http://playground.arduino.cc/Learning/MFRC522
2) http://blog.davidou.org/archives/684
3) http://www.dotblogs.com.tw/aliceyeh/archive/2014/11/12/147272.aspx
非接触式通信中高集成度的读写卡芯片。支持14443A兼容应答器信号。数字部分处理 ISO14443A 帧和错误检测。支持快速 CRYPTO1 加密算法,用语验证MIFARE系列产品。支持 MIFARE 系列更高速的非接触式通信,双向数据传输速率高达 424 kb/s。 与MF RC500 和 MF RC530 有不少相似之处,同时也具备许多特点和差异。它与主机间通信采用SPI模式,有利于减少连线,缩小PCB板体积,降低成本。
◦ Module Name: MF522-ED
◦ Working current:13—26mA/ DC 3.3V
◦ Working frequency:13.56MHz
◦ Card reading distance :0~60mm(mifare1 card)
◦ Protocol:SPI
◦ Data communication speed:Maximum 10Mbit/s
◦ Card types supported:mifare1 S50、mifare1 S70、mifare UltraLight、mifare Pro、mifare Desfire
◦ NXP RC522 Datasheet
◦ Module Name: MF522-ED
◦ Working current:13—26mA/ DC 3.3V
◦ Working frequency:13.56MHz
◦ Card reading distance :0~60mm(mifare1 card)
◦ Protocol:SPI
◦ Data communication speed:Maximum 10Mbit/s
◦ Card types supported:mifare1 S50、mifare1 S70、mifare UltraLight、mifare Pro、mifare Desfire
◦ NXP RC522 Datasheet
然後板上有VCC、RST、GND、MISO、MOSI、SCK、NSS、IRQ這八個腳位
VCC要插到ARDUINO的3V腳位 (注意!不是5V)
然後RST接 數位接腳5(DIGITAL 5)
GND就是接GND
MISO->數位12
MOSI->數位11
SCK->數位13
NSS->數位10
IRQ-> 我沒接
※ MFRC522
Mifare 1K=1024bytes, 4K=4096bytes, Mini(S20)=320bytes, Ultralight=64bytes
◎ Arduino Playground -- Mifare MFRC522 RFID Reader/Writer
▪ Dumping Mifare's TAGs
/**
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 9
*
* Script is based on the script of Miguel Balboa.
* Serial number is shown on a HD44780 compatible display
*
* The circuit:
* LCD RS pin to digital pin (7)
* LCD Enable pin to digital pin (6)
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*
* @version 0.1
* @author Henri de Jong
* @since 27-01-2013
*/
#include <SPI.h>
#include <RFID.h>
//I2C + I2C LCD Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*==========================================
SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V
==========================================*/
#define RFID_OUT 7
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN, RST_PIN);
// Setup variables:
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.init();
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
// 輸出初始化文字
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("RFIDReader RC522"); //RFID Reader RC522 + Access Control System
Serial.println("RFID Reader RC522 + Access Control System");
pinMode(RFID_OUT, OUTPUT); // sets the digital pin as output
digitalWrite(RFID_OUT, HIGH); //Set RFID OUT OFF
}
void loop()
{
if (rfid.isCard()) {
if (rfid.readCardSerial()) {
if (rfid.serNum[0] != serNum0
&& rfid.serNum[1] != serNum1
&& rfid.serNum[2] != serNum2
&& rfid.serNum[3] != serNum3
&& rfid.serNum[4] != serNum4
) {
/* With a new cardnumber, show it. */
Serial.println(" ");
Serial.println("Card found");
serNum0 = rfid.serNum[0];
serNum1 = rfid.serNum[1];
serNum2 = rfid.serNum[2];
serNum3 = rfid.serNum[3];
serNum4 = rfid.serNum[4];
//Serial.println(" ");
Serial.println("Cardnumber:");
Serial.print("Dec: ");
Serial.print(rfid.serNum[0],DEC);
Serial.print(", ");
Serial.print(rfid.serNum[1],DEC);
Serial.print(", ");
Serial.print(rfid.serNum[2],DEC);
Serial.print(", ");
Serial.print(rfid.serNum[3],DEC);
Serial.print(", ");
Serial.print(rfid.serNum[4],DEC);
Serial.println(" ");
Serial.print("Hex: ");
Serial.print(rfid.serNum[0],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[1],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[2],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[3],HEX);
Serial.print(", ");
Serial.print(rfid.serNum[4],HEX);
Serial.println(" ");
/* Write the HEX code to the display */
lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Cardno (hex):");
lcd.setCursor(0,1);
lcd.print(rfid.serNum[0], HEX);
lcd.print(',');
lcd.print(rfid.serNum[1], HEX);
lcd.print(',');
lcd.print(rfid.serNum[2], HEX);
lcd.print(',');
lcd.print(rfid.serNum[3], HEX);
lcd.print(',');
lcd.print(rfid.serNum[4], HEX);
if (rfid.serNum[0]==68 && //RFID Code Correct Set OUT ON
rfid.serNum[1]==81 && // Card found Cardnumber:
rfid.serNum[2]==187 && //Dec: 68, 81, 187, 150, 56
rfid.serNum[3]==150 && //Hex: 44, 51, BB, 96, 38
rfid.serNum[4]==56 ) {
digitalWrite(RFID_OUT, LOW);
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("RFID SET ON "); //Set RFID OUT ON
}
if (rfid.serNum[0]==112 && //RFID Code Correct Set OUT OFF
rfid.serNum[1]==33 && // Card found Cardnumber:
// Dec: 112, 33, 237, 16, 172
// Hex: 70, 21, ED, 10, AC
rfid.serNum[2]==237 &&
rfid.serNum[3]==16 &&
rfid.serNum[4]==172 ) {
digitalWrite(RFID_OUT, HIGH); //Set RFID OUT OFF
lcd.setCursor(0, 0); // 設定游標位置在第1行行首
lcd.print("RFID SET OFF ");
}
} else {
/* If we have the same ID, just write a dot. */
Serial.print(".");
}
}
}
rfid.halt();
}
沒有留言:
張貼留言