Michael Sapozhnikov
/
RFID-RC522
123
main.cpp@3:f781ace811f1, 2022-01-25 (annotated)
- Committer:
- graf2112
- Date:
- Tue Jan 25 08:57:42 2022 +0000
- Revision:
- 3:f781ace811f1
- Parent:
- 2:a0c7513fb634
123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kirchnet | 2:a0c7513fb634 | 1 | //Test of cheap 13.56 Mhz RFID-RC522 module from eBay |
kirchnet | 2:a0c7513fb634 | 2 | //This code is based on Martin Olejar's MFRC522 library. Minimal changes |
kirchnet | 2:a0c7513fb634 | 3 | //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too |
kirchnet | 2:a0c7513fb634 | 4 | |
kirchnet | 2:a0c7513fb634 | 5 | //Connect as follows: |
kirchnet | 2:a0c7513fb634 | 6 | //RFID pins -> Nucleo header CN5 (Arduino-compatible header) |
kirchnet | 2:a0c7513fb634 | 7 | //---------------------------------------- |
kirchnet | 2:a0c7513fb634 | 8 | //RFID IRQ=pin5 -> Not used. Leave open |
kirchnet | 2:a0c7513fb634 | 9 | //RFID MISO=pin4 -> Nucleo SPI_MISO=PA_6=D12 |
kirchnet | 2:a0c7513fb634 | 10 | //RFID MOSI=pin3 -> Nucleo SPI_MOSI=PA_7=D11 |
kirchnet | 2:a0c7513fb634 | 11 | //RFID SCK=pin2 -> Nucleo SPI_SCK =PA_5=D13 |
kirchnet | 2:a0c7513fb634 | 12 | //RFID SDA=pin1 -> Nucleo SPI_CS =PB_6=D10 |
kirchnet | 2:a0c7513fb634 | 13 | //RFID RST=pin7 -> Nucleo =PA_9=D8 |
kirchnet | 2:a0c7513fb634 | 14 | //3.3V and Gnd to the respective pins |
kirchnet | 2:a0c7513fb634 | 15 | |
kirchnet | 2:a0c7513fb634 | 16 | #include "mbed.h" |
kirchnet | 2:a0c7513fb634 | 17 | #include "MFRC522.h" |
kirchnet | 2:a0c7513fb634 | 18 | |
kirchnet | 2:a0c7513fb634 | 19 | // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) |
graf2112 | 3:f781ace811f1 | 20 | #define MF_RESET D9 |
kirchnet | 2:a0c7513fb634 | 21 | |
kirchnet | 2:a0c7513fb634 | 22 | DigitalOut LedGreen(LED1); |
kirchnet | 2:a0c7513fb634 | 23 | |
kirchnet | 2:a0c7513fb634 | 24 | //Serial connection to PC for output |
kirchnet | 2:a0c7513fb634 | 25 | Serial pc(SERIAL_TX, SERIAL_RX); |
kirchnet | 2:a0c7513fb634 | 26 | |
kirchnet | 2:a0c7513fb634 | 27 | MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
kirchnet | 2:a0c7513fb634 | 28 | |
kirchnet | 2:a0c7513fb634 | 29 | int main(void) { |
kirchnet | 2:a0c7513fb634 | 30 | pc.printf("starting...\n"); |
kirchnet | 2:a0c7513fb634 | 31 | |
kirchnet | 2:a0c7513fb634 | 32 | // Init. RC522 Chip |
kirchnet | 2:a0c7513fb634 | 33 | RfChip.PCD_Init(); |
kirchnet | 2:a0c7513fb634 | 34 | |
kirchnet | 2:a0c7513fb634 | 35 | while (true) { |
kirchnet | 2:a0c7513fb634 | 36 | LedGreen = 1; |
kirchnet | 2:a0c7513fb634 | 37 | |
kirchnet | 2:a0c7513fb634 | 38 | // Look for new cards |
kirchnet | 2:a0c7513fb634 | 39 | if ( ! RfChip.PICC_IsNewCardPresent()) |
kirchnet | 2:a0c7513fb634 | 40 | { |
kirchnet | 2:a0c7513fb634 | 41 | wait_ms(500); |
kirchnet | 2:a0c7513fb634 | 42 | continue; |
kirchnet | 2:a0c7513fb634 | 43 | } |
kirchnet | 2:a0c7513fb634 | 44 | |
kirchnet | 2:a0c7513fb634 | 45 | // Select one of the cards |
kirchnet | 2:a0c7513fb634 | 46 | if ( ! RfChip.PICC_ReadCardSerial()) |
kirchnet | 2:a0c7513fb634 | 47 | { |
kirchnet | 2:a0c7513fb634 | 48 | wait_ms(500); |
kirchnet | 2:a0c7513fb634 | 49 | continue; |
kirchnet | 2:a0c7513fb634 | 50 | } |
kirchnet | 2:a0c7513fb634 | 51 | |
kirchnet | 2:a0c7513fb634 | 52 | LedGreen = 0; |
kirchnet | 2:a0c7513fb634 | 53 | |
kirchnet | 2:a0c7513fb634 | 54 | // Print Card UID |
kirchnet | 2:a0c7513fb634 | 55 | pc.printf("Card UID: "); |
kirchnet | 2:a0c7513fb634 | 56 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
kirchnet | 2:a0c7513fb634 | 57 | { |
kirchnet | 2:a0c7513fb634 | 58 | pc.printf(" %X02", RfChip.uid.uidByte[i]); |
kirchnet | 2:a0c7513fb634 | 59 | } |
kirchnet | 2:a0c7513fb634 | 60 | pc.printf("\n\r"); |
kirchnet | 2:a0c7513fb634 | 61 | |
kirchnet | 2:a0c7513fb634 | 62 | // Print Card type |
kirchnet | 2:a0c7513fb634 | 63 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
kirchnet | 2:a0c7513fb634 | 64 | pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
kirchnet | 2:a0c7513fb634 | 65 | wait_ms(1000); |
kirchnet | 2:a0c7513fb634 | 66 | } |
kirchnet | 2:a0c7513fb634 | 67 | } |