Project5_Software / Mbed 2 deprecated RFID-RC522

Dependencies:   mbed

Fork of RFID-RC522 by Thomas Kirchner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay
00002 //This code is based on Martin Olejar's MFRC522 library. Minimal changes
00003 //Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too
00004 
00005 //Connect as follows:
00006 //RFID pins        ->  Nucleo header CN5 (Arduino-compatible header)
00007 //----------------------------------------
00008 //RFID IRQ=pin5    ->   Not used. Leave open
00009 //RFID MISO=pin4   ->   Nucleo SPI_MISO=PA_6=D12
00010 //RFID MOSI=pin3   ->   Nucleo SPI_MOSI=PA_7=D11
00011 //RFID SCK=pin2    ->   Nucleo SPI_SCK =PA_5=D13
00012 //RFID SDA=pin1    ->   Nucleo SPI_CS  =PB_6=D10
00013 //RFID RST=pin7    ->   Nucleo         =PA_9=D8
00014 //3.3V and Gnd to the respective pins                              
00015                               
00016 #include "mbed.h"
00017 #include "MFRC522.h"
00018 
00019 // Nucleo Pin for MFRC522 reset (pick another D pin if you need D8)
00020 #define MF_RESET    p15
00021 #define SPI_MOSI    p5
00022 #define SPI_MISO    p6
00023 #define SPI_SCK     p7
00024 #define SPI_CS      p8
00025 
00026 DigitalOut LedGreen(LED1);
00027 
00028 //Serial connection to PC for output
00029 Serial pc(USBTX, USBRX);
00030 
00031 MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
00032 
00033 int main(void) {
00034   pc.printf("starting...\r\n");
00035 
00036   // Init. RC522 Chip
00037   RfChip.PCD_Init();
00038 
00039     pc.printf("init passed\r\n");
00040 
00041 
00042   while (true) {
00043     LedGreen = 1;
00044 
00045     // Look for new cards
00046     if ( ! RfChip.PICC_IsNewCardPresent())
00047     {
00048       wait_ms(500);
00049       continue;
00050     }
00051 
00052     // Select one of the cards
00053     if (!RfChip.PICC_ReadCardSerial())
00054     {
00055       wait_ms(500);
00056       pc.printf("card read\r\n");
00057       continue;
00058     }
00059 
00060     LedGreen = 0;
00061 
00062     // Print Card UID
00063     pc.printf("Card UID: ");
00064     for (uint8_t i = 0; i < RfChip.uid.size; i++)
00065     {
00066       pc.printf(" %X02", RfChip.uid.uidByte[i]);
00067     }
00068     pc.printf("\n\r");
00069 
00070     // Print Card type
00071     uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00072     pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00073     wait_ms(1000);
00074   }
00075 }