Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of RFID-RC522 by
main.cpp@4:e0bc23d5134a, 2017-11-22 (annotated)
- Committer:
- mario_meh
- Date:
- Wed Nov 22 08:55:19 2017 +0000
- Revision:
- 4:e0bc23d5134a
- Parent:
- 3:30e31f03d156
Lilnija pogona
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" |
| mario_meh | 4:e0bc23d5134a | 18 | #include "TextLCD.h" |
| kirchnet | 2:a0c7513fb634 | 19 | |
| kirchnet | 2:a0c7513fb634 | 20 | // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8) |
| nielsvanmegen | 3:30e31f03d156 | 21 | #define MF_RESET p15 |
| nielsvanmegen | 3:30e31f03d156 | 22 | #define SPI_MOSI p5 |
| nielsvanmegen | 3:30e31f03d156 | 23 | #define SPI_MISO p6 |
| nielsvanmegen | 3:30e31f03d156 | 24 | #define SPI_SCK p7 |
| nielsvanmegen | 3:30e31f03d156 | 25 | #define SPI_CS p8 |
| kirchnet | 2:a0c7513fb634 | 26 | |
| kirchnet | 2:a0c7513fb634 | 27 | DigitalOut LedGreen(LED1); |
| kirchnet | 2:a0c7513fb634 | 28 | |
| mario_meh | 4:e0bc23d5134a | 29 | //LCD text display status |
| mario_meh | 4:e0bc23d5134a | 30 | TextLCD lcd(p10, p11, p27, p28, p29, p30); // rs, e, d4-d7 |
| mario_meh | 4:e0bc23d5134a | 31 | |
| kirchnet | 2:a0c7513fb634 | 32 | //Serial connection to PC for output |
| nielsvanmegen | 3:30e31f03d156 | 33 | Serial pc(USBTX, USBRX); |
| kirchnet | 2:a0c7513fb634 | 34 | |
| kirchnet | 2:a0c7513fb634 | 35 | MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
| kirchnet | 2:a0c7513fb634 | 36 | |
| mario_meh | 4:e0bc23d5134a | 37 | unsigned char tagRX[20]; //array to store incoming tag |
| mario_meh | 4:e0bc23d5134a | 38 | int tagRXindex=0; //keeps track of the index in tagRX |
| mario_meh | 4:e0bc23d5134a | 39 | bool tagIncomingFlag=false; //flag to indicate that a new tag is busy being read |
| mario_meh | 4:e0bc23d5134a | 40 | bool newtagFlag=false; //flag to indicate that a new tag was finished being read |
| mario_meh | 4:e0bc23d5134a | 41 | |
| kirchnet | 2:a0c7513fb634 | 42 | int main(void) { |
| mario_meh | 4:e0bc23d5134a | 43 | lcd.printf("starting...\r\n"); |
| kirchnet | 2:a0c7513fb634 | 44 | |
| kirchnet | 2:a0c7513fb634 | 45 | // Init. RC522 Chip |
| kirchnet | 2:a0c7513fb634 | 46 | RfChip.PCD_Init(); |
| kirchnet | 2:a0c7513fb634 | 47 | |
| mario_meh | 4:e0bc23d5134a | 48 | lcd.printf("init passed\r\n"); |
| nielsvanmegen | 3:30e31f03d156 | 49 | |
| nielsvanmegen | 3:30e31f03d156 | 50 | |
| kirchnet | 2:a0c7513fb634 | 51 | while (true) { |
| kirchnet | 2:a0c7513fb634 | 52 | LedGreen = 1; |
| kirchnet | 2:a0c7513fb634 | 53 | |
| kirchnet | 2:a0c7513fb634 | 54 | // Look for new cards |
| kirchnet | 2:a0c7513fb634 | 55 | if ( ! RfChip.PICC_IsNewCardPresent()) |
| kirchnet | 2:a0c7513fb634 | 56 | { |
| kirchnet | 2:a0c7513fb634 | 57 | wait_ms(500); |
| kirchnet | 2:a0c7513fb634 | 58 | continue; |
| kirchnet | 2:a0c7513fb634 | 59 | } |
| kirchnet | 2:a0c7513fb634 | 60 | |
| kirchnet | 2:a0c7513fb634 | 61 | // Select one of the cards |
| nielsvanmegen | 3:30e31f03d156 | 62 | if (!RfChip.PICC_ReadCardSerial()) |
| kirchnet | 2:a0c7513fb634 | 63 | { |
| kirchnet | 2:a0c7513fb634 | 64 | wait_ms(500); |
| mario_meh | 4:e0bc23d5134a | 65 | lcd.printf("card read\r\n"); |
| kirchnet | 2:a0c7513fb634 | 66 | continue; |
| kirchnet | 2:a0c7513fb634 | 67 | } |
| kirchnet | 2:a0c7513fb634 | 68 | |
| kirchnet | 2:a0c7513fb634 | 69 | LedGreen = 0; |
| kirchnet | 2:a0c7513fb634 | 70 | |
| mario_meh | 4:e0bc23d5134a | 71 | // Print Card UID - original |
| mario_meh | 4:e0bc23d5134a | 72 | //lcd.printf("Card UID: "); |
| mario_meh | 4:e0bc23d5134a | 73 | //for (uint8_t i = 0; i < RfChip.uid.size; i++) |
| mario_meh | 4:e0bc23d5134a | 74 | //{ |
| mario_meh | 4:e0bc23d5134a | 75 | // lcd.printf(" %X02", RfChip.uid.uidByte[i]); |
| mario_meh | 4:e0bc23d5134a | 76 | //} |
| mario_meh | 4:e0bc23d5134a | 77 | //lcd.printf("\n\r"); |
| mario_meh | 4:e0bc23d5134a | 78 | |
| mario_meh | 4:e0bc23d5134a | 79 | // Print Card UID - mario |
| mario_meh | 4:e0bc23d5134a | 80 | //lcd.cls(); |
| mario_meh | 4:e0bc23d5134a | 81 | //lcd.printf("Card UID: "); |
| kirchnet | 2:a0c7513fb634 | 82 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
| kirchnet | 2:a0c7513fb634 | 83 | { |
| mario_meh | 4:e0bc23d5134a | 84 | tagRX[i] = RfChip.uid.uidByte[i]; |
| kirchnet | 2:a0c7513fb634 | 85 | } |
| mario_meh | 4:e0bc23d5134a | 86 | lcd.printf("\n\r"); |
| kirchnet | 2:a0c7513fb634 | 87 | |
| kirchnet | 2:a0c7513fb634 | 88 | // Print Card type |
| mario_meh | 4:e0bc23d5134a | 89 | //uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
| mario_meh | 4:e0bc23d5134a | 90 | //lcd.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
| mario_meh | 4:e0bc23d5134a | 91 | |
| mario_meh | 4:e0bc23d5134a | 92 | lcd.cls(); |
| mario_meh | 4:e0bc23d5134a | 93 | lcd.printf("Card UID: "); |
| mario_meh | 4:e0bc23d5134a | 94 | lcd.printf(" %s", tagRX); |
| mario_meh | 4:e0bc23d5134a | 95 | |
| kirchnet | 2:a0c7513fb634 | 96 | wait_ms(1000); |
| kirchnet | 2:a0c7513fb634 | 97 | } |
| kirchnet | 2:a0c7513fb634 | 98 | } |
