aa

Dependencies:   mbed

Committer:
mustwillza
Date:
Fri Oct 23 11:01:44 2015 +0000
Revision:
0:3d4bd1fdeb2e
Child:
1:486ccde79dc6
Worked

Who changed what in which revision?

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