毕业设计我要做一个基于rfid的巡更系统,为此我要连接至少两个的RFID读写模块。
但网上的示例都是只用一个的,引脚的插得位置也是一样的,我不太清楚引脚都是怎么定义的。
我用的是mfrc522的模块。
以下面代码为例,我只知道#define RST_PIN 9 和#define SS_PIN 10
是定义RST和SS引脚的,但剩下的SCK MOSI MISO 是怎么定义的。本来我以为剩下的是可以公用的,就用一个面包板连接起来,但发现不行。
现在可能是要定义这些的引脚,但我不知道哪部分代码是定义这些引脚的。
求大神指点一下。
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // 可配置,见典型引脚布局以上
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
/* Set your new UID here! */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC 初始化与PC串行通信
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) 如果没有串口打开,什么也不做(添加基于atmega32u4 Arduinos)
SPI.begin(); // Init SPI bus 初始化SPI总线
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!")); //警告:这个例子改写你的UID变卡UID,小心使用!
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. 准备关键所有按键都设置在芯片ffffffffffff H从工厂交货。
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
// Setting the UID can be as simple as this: 设置UID可以这么简单:
//void loop() {
// byte newUid[] = NEW_UID;
// if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
// Serial.println("Wrote new UID to card.");
// }
// delay(1000);
//}
// But of course this is a more proper approach 当然,这是一个更恰当的方法
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.现在选择卡片。UID和SAK是mfrc522.uid。
// Dump UID转储的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();
// Dump PICC type 转为PICC类型
// MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Serial.print(F("PICC type: "));
// Serial.print(mfrc522.PICC_GetTypeName(piccType));
// Serial.print(F(" (SAK "));
// Serial.print(mfrc522.uid.sak);
// Serial.print(")\r\n");
// if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
// && piccType != MFRC522::PICC_TYPE_MIFARE_1K
// && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
// Serial.println(F("This sample only works with MIFARE Classic cards."));
// return;
// }
// Set new UID 设置新的UID
byte newUid[] = NEW_UID;
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println(F("Wrote new UID to card."));
}
// Halt PICC and re-select it so DumpToSerial doesn't get confused / /停止PICC和重新选择那么DumpToSerial不混乱
mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
return;
}
// Dump the new memory contents 转为新的内容储存
Serial.println(F("New UID and contents:"));
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(2000);
}