aa

Dependencies:   mbed

Committer:
kantapon501
Date:
Sat Nov 14 08:28:43 2015 +0000
Revision:
1:486ccde79dc6
Parent:
0:3d4bd1fdeb2e
Digi

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) {
kantapon501 1:486ccde79dc6 30 char uidcheck[ 100 ];
mustwillza 0:3d4bd1fdeb2e 31 pc.printf("starting...\n");
mustwillza 0:3d4bd1fdeb2e 32
mustwillza 0:3d4bd1fdeb2e 33 // Init. RC522 Chip
mustwillza 0:3d4bd1fdeb2e 34 RfChip.PCD_Init();
mustwillza 0:3d4bd1fdeb2e 35
mustwillza 0:3d4bd1fdeb2e 36 while (true) {
mustwillza 0:3d4bd1fdeb2e 37 LedGreen = 1;
mustwillza 0:3d4bd1fdeb2e 38
mustwillza 0:3d4bd1fdeb2e 39 // Look for new cards
mustwillza 0:3d4bd1fdeb2e 40 if ( ! RfChip.PICC_IsNewCardPresent())
mustwillza 0:3d4bd1fdeb2e 41 {
mustwillza 0:3d4bd1fdeb2e 42 wait_ms(500);
mustwillza 0:3d4bd1fdeb2e 43 continue;
mustwillza 0:3d4bd1fdeb2e 44 }
mustwillza 0:3d4bd1fdeb2e 45
mustwillza 0:3d4bd1fdeb2e 46 // Select one of the cards
mustwillza 0:3d4bd1fdeb2e 47 if ( ! RfChip.PICC_ReadCardSerial())
mustwillza 0:3d4bd1fdeb2e 48 {
mustwillza 0:3d4bd1fdeb2e 49 wait_ms(500);
mustwillza 0:3d4bd1fdeb2e 50 continue;
mustwillza 0:3d4bd1fdeb2e 51 }
mustwillza 0:3d4bd1fdeb2e 52
mustwillza 0:3d4bd1fdeb2e 53 LedGreen = 0;
mustwillza 0:3d4bd1fdeb2e 54
mustwillza 0:3d4bd1fdeb2e 55 // Print Card UID
kantapon501 1:486ccde79dc6 56
mustwillza 0:3d4bd1fdeb2e 57 pc.printf("Card UID: ");
mustwillza 0:3d4bd1fdeb2e 58 for (uint8_t i = 0; i < RfChip.uid.size; i++)
mustwillza 0:3d4bd1fdeb2e 59 {
mustwillza 0:3d4bd1fdeb2e 60 pc.printf(" %X02", RfChip.uid.uidByte[i]);
kantapon501 1:486ccde79dc6 61 uidcheck[i] = RfChip.uid.uidByte[i];
mustwillza 0:3d4bd1fdeb2e 62 }
mustwillza 0:3d4bd1fdeb2e 63 pc.printf("\n\r");
mustwillza 0:3d4bd1fdeb2e 64
mustwillza 0:3d4bd1fdeb2e 65 // Print Card type
mustwillza 0:3d4bd1fdeb2e 66 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
mustwillza 0:3d4bd1fdeb2e 67 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
mustwillza 0:3d4bd1fdeb2e 68 wait_ms(1000);
mustwillza 0:3d4bd1fdeb2e 69 }
kantapon501 1:486ccde79dc6 70
mustwillza 0:3d4bd1fdeb2e 71 }