-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFID_LCD.ino
92 lines (90 loc) · 2.24 KB
/
RFID_LCD.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
#define IRQ_PIN 2
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
volatile bool bNewInt = false;
byte regVal = 0x7F;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(F("Setup"));
delay(1000);
lcd.clear();
lcd.noBacklight();
delay(250);
lcd.backlight();
SPI.begin();
mfrc522.PCD_Init();
Serial.print(F("Ver:0x"));
lcd.setCursor(0, 0);
lcd.print(F("Ver:0x"));
byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.println(readReg, HEX);
lcd.setCursor(0, 1);
lcd.print(readReg, HEX);
delay(3000);
lcd.clear();
pinMode(IRQ_PIN, INPUT_PULLUP);
regVal = 0xA0;
mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg, regVal);
bNewInt = false;
attachInterrupt(digitalPinToInterrupt(IRQ_PIN), readCard, FALLING);
do {
;
} while (!bNewInt);
bNewInt = false;
Serial.println(F("End setup"));
lcd.setCursor(0, 0);
lcd.print(F("End setup"));
delay(1000);
lcd.clear();
}
void loop() {
if (bNewInt) {
Serial.print(F("Interrupt. "));
lcd.clear();
mfrc522.PICC_ReadCardSerial();
Serial.print(F("Card UID:"));
lcd.setCursor(0, 0);
lcd.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
clearInt(mfrc522);
mfrc522.PICC_HaltA();
bNewInt = false;
}
activateRec(mfrc522);
delay(100);
}
void readCard() {
bNewInt = true;
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
lcd.setCursor(i * 3, 1);
if (buffer[i] < 0x10) {
lcd.print(F(" 0"));
} else {
lcd.print(F(" "));
}
lcd.print(buffer[i], HEX);
}
}
void activateRec(MFRC522 mfrc522) {
mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);
}
void clearInt(MFRC522 mfrc522) {
mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
}