ESP32 + RC522 測試SPI Mode RC522 模組 (利用ESP32 multitasking )
/* Wiring RFID RC522 module
==============================================================
GND = GND 3.3V = 3.3V
The following table shows the typical pin layout used:
* MFRC522 ESP32
* Reader/PCD
* Signal Pin Pin
* ------------------------------------
* RST/Reset RST GPIO27
* SPI SS SDA(SS) GPIO5
* SPI MOSI MOSI GPIO23
* SPI MISO MISO GPIO19
* SPI SCK SCK GPIO18
//RC522 SPI Mode
#include <SPI.h>
#include <MFRC522.h>
//GPIO 2 D1 Build in LED
#define LED 2
bool ledState = false;
/* Wiring RFID RC522 module
=============================================================================
GND = GND 3.3V = 3.3V
The following table shows the typical pin layout used:
* MFRC522 ESP32 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST GPIO27 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) GPIO5 10 53 D10 10 10
* SPI MOSI MOSI GPIO23 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO GPIO19 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK GPIO18 13 / ICSP-3 52 D13 ICSP-3 15
*
[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.
[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.
[3] The SDA pin might be labeled SS on some/older MFRC522 boards
=============================================================================
*/
#define SS_PIN 5 // ESP32 pin GIOP5
#define RST_PIN 27 // ESP32 pin GIOP27
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance
//宣告任務Task1
TaskHandle_t Task1;
//===========================================================
//任務1副程式Task1_senddata
void Task1_senddata(void * pvParameters ) {
//無窮迴圈
for (;;) {
//偵測上傳旗標是否為true
Serial.println("Reading RFID tag");
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.print("RFID/NFC Tag Type: ");
Serial.println(rfid.PICC_GetTypeName(piccType));
// print UID in Serial Monitor in the hex format
Serial.print("UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
rfid.PICC_HaltA(); // halt PICC
rfid.PCD_StopCrypto1(); // stop encryption on PCD
}
}
else
{
// RC522/PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
//Task1休息,delay(X)不可省略
delay(1000);
}
}
//===========================================================
//===========================================================
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
delay(1000); // Delay for stability
// Setting LED pin as output
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // Turn off the LED initially
//======================================================
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
Serial.println(F("Ready!"));
Serial.println(F("======================================================"));
Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");
//在核心0啟動任務1
xTaskCreatePinnedToCore(
Task1_senddata, /*任務實際對應的Function*/
"Task1", /*任務名稱*/
10000, /*堆疊空間*/
NULL, /*無輸入值*/
0, /*優先序0*/
&Task1, /*對應的任務變數位址*/
0); /*指定在核心0執行 */
}
//===========================================================
void loop()
{
delay(1000);
digitalWrite(LED, !digitalRead(LED));
}
//===========================================================
Tools -> Serial Monitor 115200 Baud
沒有留言:
張貼留言