mirror of
https://github.com/semaf/MFRC522_I2C_Library.git
synced 2025-07-30 05:17:14 +02:00
Rewrite of Basic Functions to work with TwoWire
This commit is contained in:
54
examples/MFRC522_i2c.ino
Normal file
54
examples/MFRC522_i2c.ino
Normal file
@ -0,0 +1,54 @@
|
||||
#include <Wire.h>
|
||||
#include "MFRC522_I2C.h"
|
||||
|
||||
#define RST_PIN 6 // Arduino UNO Pin
|
||||
// #define RST_PIN 14 // D5 Pin on NodeMCU
|
||||
|
||||
|
||||
// 0x28 is i2c address on SDA. Check your address with i2cscanner if not match.
|
||||
MFRC522 mfrc522(0x28, RST_PIN); // Create MFRC522 instance.
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Initialize serial communications with the PC
|
||||
Wire.begin(); // Initialize I2C
|
||||
mfrc522.PCD_Init(); // Init MFRC522
|
||||
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
||||
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Look for new cards, and select one if present
|
||||
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
|
||||
delay(50);
|
||||
return;
|
||||
}
|
||||
|
||||
// Now a card is selected. The UID and SAK is in mfrc522.uid.
|
||||
|
||||
// Dump UID
|
||||
Serial.print(F("Card UID:"));
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
||||
Serial.print(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void ShowReaderDetails() {
|
||||
// Get the MFRC522 software version
|
||||
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||
Serial.print(F("MFRC522 Software Version: 0x"));
|
||||
Serial.print(v, HEX);
|
||||
if (v == 0x91)
|
||||
Serial.print(F(" = v1.0"));
|
||||
else if (v == 0x92)
|
||||
Serial.print(F(" = v2.0"));
|
||||
else
|
||||
Serial.print(F(" (unknown)"));
|
||||
Serial.println("");
|
||||
// When 0x00 or 0xFF is returned, communication probably failed
|
||||
if ((v == 0x00) || (v == 0xFF)) {
|
||||
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
||||
}
|
||||
}
|
||||
|
113
examples/dual_i2c_scanner.ino
Normal file
113
examples/dual_i2c_scanner.ino
Normal file
@ -0,0 +1,113 @@
|
||||
#include <Wire.h>
|
||||
#include "MFRC522_I2C.h"
|
||||
|
||||
// I2C GPIOs
|
||||
#define IIC_CLK 32 // internal
|
||||
#define IIC_DATA 33 // internal
|
||||
|
||||
// 2nd I2C GPIOs
|
||||
#define RST_PIN 12
|
||||
#define ext_IIC_CLK 23 // 14-pin-header
|
||||
#define ext_IIC_DATA 18 // 14-pin-header
|
||||
|
||||
TwoWire i2cBusOne = TwoWire(0);
|
||||
TwoWire i2cBusTwo = TwoWire(1);
|
||||
MFRC522 mfrc522(RST_PIN , 0x28, i2cBusTwo);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Initialize serial communications with the PC
|
||||
i2cBusOne.begin(IIC_DATA, IIC_CLK, 40000);
|
||||
i2cBusTwo.begin(ext_IIC_DATA, ext_IIC_CLK, 40000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
scanBus1();
|
||||
delay(3000);
|
||||
scanBus2();
|
||||
mfrc522.PCD_Init(); // Init MFRC522
|
||||
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
||||
|
||||
}
|
||||
|
||||
void scanBus1 () {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
Serial.println("Scanning...");
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ ) {
|
||||
i2cBusOne.beginTransmission(address);
|
||||
error = i2cBusOne.endTransmission();
|
||||
if (error == 0) {
|
||||
Serial.print("Bus1: I2C device found at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4) {
|
||||
Serial.print("Unknow error at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0) {
|
||||
Serial.println("No I2C devices found\n");
|
||||
}
|
||||
else {
|
||||
Serial.println("done\n");
|
||||
}
|
||||
}
|
||||
|
||||
void scanBus2 () {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
Serial.println("Scanning...");
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ ) {
|
||||
i2cBusTwo.beginTransmission(address);
|
||||
error = i2cBusTwo.endTransmission();
|
||||
if (error == 0) {
|
||||
Serial.print("Bus2: I2C device found at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4) {
|
||||
Serial.print("Unknow error at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0) {
|
||||
Serial.println("No I2C devices found\n");
|
||||
}
|
||||
else {
|
||||
Serial.println("done\n");
|
||||
}
|
||||
}
|
||||
|
||||
void ShowReaderDetails() {
|
||||
// Get the MFRC522 software version
|
||||
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||
Serial.print(F("MFRC522 Software Version: 0x"));
|
||||
Serial.print(v, HEX);
|
||||
if (v == 0x91)
|
||||
Serial.print(F(" = v1.0"));
|
||||
else if (v == 0x92)
|
||||
Serial.print(F(" = v2.0"));
|
||||
else
|
||||
Serial.print(F(" (unknown)"));
|
||||
Serial.println("");
|
||||
// When 0x00 or 0xFF is returned, communication probably failed
|
||||
if ((v == 0x00) || (v == 0xFF)) {
|
||||
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
||||
}
|
||||
}
|
||||
|
113
examples/dual_i2c_scanner/dual_i2c_scanner.ino
Normal file
113
examples/dual_i2c_scanner/dual_i2c_scanner.ino
Normal file
@ -0,0 +1,113 @@
|
||||
#include <Wire.h>
|
||||
#include "MFRC522_I2C.h"
|
||||
|
||||
// I2C GPIOs
|
||||
#define IIC_CLK 32 // internal
|
||||
#define IIC_DATA 33 // internal
|
||||
|
||||
// 2nd I2C GPIOs
|
||||
#define RST_PIN 12
|
||||
#define ext_IIC_CLK 23 // 14-pin-header
|
||||
#define ext_IIC_DATA 18 // 14-pin-header
|
||||
|
||||
TwoWire i2cBusOne = TwoWire(0);
|
||||
TwoWire i2cBusTwo = TwoWire(1);
|
||||
MFRC522 mfrc522(RST_PIN , 0x28, i2cBusTwo);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Initialize serial communications with the PC
|
||||
i2cBusOne.begin(IIC_DATA, IIC_CLK, 40000);
|
||||
i2cBusTwo.begin(ext_IIC_DATA, ext_IIC_CLK, 40000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
scanBus1();
|
||||
delay(3000);
|
||||
scanBus2();
|
||||
mfrc522.PCD_Init(); // Init MFRC522
|
||||
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
||||
|
||||
}
|
||||
|
||||
void scanBus1 () {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
Serial.println("Scanning...");
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ ) {
|
||||
i2cBusOne.beginTransmission(address);
|
||||
error = i2cBusOne.endTransmission();
|
||||
if (error == 0) {
|
||||
Serial.print("Bus1: I2C device found at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4) {
|
||||
Serial.print("Unknow error at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0) {
|
||||
Serial.println("No I2C devices found\n");
|
||||
}
|
||||
else {
|
||||
Serial.println("done\n");
|
||||
}
|
||||
}
|
||||
|
||||
void scanBus2 () {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
Serial.println("Scanning...");
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ ) {
|
||||
i2cBusTwo.beginTransmission(address);
|
||||
error = i2cBusTwo.endTransmission();
|
||||
if (error == 0) {
|
||||
Serial.print("Bus2: I2C device found at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4) {
|
||||
Serial.print("Unknow error at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0) {
|
||||
Serial.println("No I2C devices found\n");
|
||||
}
|
||||
else {
|
||||
Serial.println("done\n");
|
||||
}
|
||||
}
|
||||
|
||||
void ShowReaderDetails() {
|
||||
// Get the MFRC522 software version
|
||||
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||
Serial.print(F("MFRC522 Software Version: 0x"));
|
||||
Serial.print(v, HEX);
|
||||
if (v == 0x91)
|
||||
Serial.print(F(" = v1.0"));
|
||||
else if (v == 0x92)
|
||||
Serial.print(F(" = v2.0"));
|
||||
else
|
||||
Serial.print(F(" (unknown)"));
|
||||
Serial.println("");
|
||||
// When 0x00 or 0xFF is returned, communication probably failed
|
||||
if ((v == 0x00) || (v == 0xFF)) {
|
||||
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
||||
}
|
||||
}
|
||||
|
131
examples/dual_i2c_scanner_new/dual_i2c_scanner_new.ino
Normal file
131
examples/dual_i2c_scanner_new/dual_i2c_scanner_new.ino
Normal file
@ -0,0 +1,131 @@
|
||||
#include <Wire.h>
|
||||
#include "MFRC522_I2C.h"
|
||||
|
||||
// I2C GPIOs
|
||||
#define IIC_CLK 32 // internal
|
||||
#define IIC_DATA 33 // internal
|
||||
|
||||
// 2nd I2C GPIOs
|
||||
#define RST_PIN 12
|
||||
#define ext_IIC_CLK 23 // 14-pin-header
|
||||
#define ext_IIC_DATA 18 // 14-pin-header
|
||||
|
||||
byte chipAddress = 0x28;
|
||||
|
||||
TwoWire i2cBusOne = TwoWire(0);
|
||||
TwoWire i2cBusTwo = TwoWire(1);
|
||||
MFRC522 mfrc522(chipAddress, RST_PIN, i2cBusTwo);
|
||||
|
||||
void setup() {
|
||||
byte version;
|
||||
byte value;
|
||||
Serial.begin(115200); // Initialize serial communications with the PC
|
||||
Serial.println("TwoWire Bus 1 initiieren");
|
||||
i2cBusOne.begin(IIC_DATA, IIC_CLK, 40000);
|
||||
Serial.println("TwoWire Bus 2 initiieren");
|
||||
i2cBusTwo.begin(ext_IIC_DATA, ext_IIC_CLK, 40000);
|
||||
Serial.println("MFRC522 initiieren");
|
||||
mfrc522.PCD_Init();
|
||||
Serial.println("MFRC522 Selbsttest");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// scanBus1();
|
||||
scanBus2();
|
||||
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
||||
if (mfrc522.PCD_PerformSelfTest()) {
|
||||
Serial.println("Selftest OK");
|
||||
} else Serial.println("Selftest not OK");
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
void scanBus1 () {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
Serial.println("Scanning...");
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ ) {
|
||||
i2cBusOne.beginTransmission(address);
|
||||
error = i2cBusOne.endTransmission();
|
||||
if (error == 0) {
|
||||
Serial.print("Bus1: I2C device found at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4) {
|
||||
Serial.print("Unknow error at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
}
|
||||
}
|
||||
if (nDevices == 0) {
|
||||
Serial.println("No I2C devices found\n");
|
||||
}
|
||||
else {
|
||||
Serial.println("done\n");
|
||||
}
|
||||
}
|
||||
|
||||
void scanBus2 () {
|
||||
byte error, address;
|
||||
int nDevices;
|
||||
Serial.println("Scanning...");
|
||||
nDevices = 0;
|
||||
for(address = 1; address < 127; address++ ) {
|
||||
i2cBusTwo.beginTransmission(address);
|
||||
error = i2cBusTwo.endTransmission();
|
||||
if (error == 0) {
|
||||
Serial.print("Bus2: I2C device found at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
nDevices++;
|
||||
}
|
||||
else if (error==4) {
|
||||
Serial.print("Unknow error at address 0x");
|
||||
if (address<16) {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.println(address,HEX);
|
||||
}
|
||||
}
|
||||
/* if (nDevices == 0) {
|
||||
Serial.println("No I2C devices found\n");
|
||||
pinMode(RST_PIN, OUTPUT);
|
||||
if (digitalRead(RST_PIN) == LOW) { //The MFRC522 chip is in power down mode.
|
||||
digitalWrite(RST_PIN, HIGH); // Exit power down mode. This triggers a hard reset.
|
||||
// Section 8.8.2 in the datasheet says the oscillator start-up time is the start up time of the crystal + 37,74<37>s. Let us be generous: 50ms.
|
||||
delay(200);
|
||||
}
|
||||
else {
|
||||
Serial.println("done\n");
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
void ShowReaderDetails() {
|
||||
byte version = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||
|
||||
switch (version) {
|
||||
case 0x88: // Fudan Semiconductor FM17522 clone
|
||||
Serial.println("Fudan Semiconductor FM17522 clone");
|
||||
break;
|
||||
case 0x90: // Version 0.0
|
||||
Serial.println("Version 0.0");
|
||||
break;
|
||||
case 0x91: // Version 1.0
|
||||
Serial.println("Version 1.0");
|
||||
break;
|
||||
case 0x92: // Version 2.0
|
||||
Serial.println("Version 2.0");
|
||||
break;
|
||||
default: // Unknown version
|
||||
Serial.println("Unknown version");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user