Romain Ardino / Mbed 2 deprecated RFID_RC522Rayan

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include "mbed.h"
00007 #include "MFRC522.h"
00008 
00009 #define SLEEP_TIME                  500 // (msec)
00010 #define PRINT_AFTER_N_LOOPS         20
00011 
00012 // K64F Pin for "MFRC522 (Chip select, Clock, MOSI, MISO,  IRQ, Ground, Reset, 3.3V)"
00013 #define MF_RESET    PB_12     //Reset
00014 #define SPI_MOSI    PB_15  //MOSI
00015 #define SPI_MISO    PB_14 //MISO
00016 #define SPI_SCK     PB_13 //Clock
00017 #define SPI_CS      PB_8  //Chip Select
00018 
00019 DigitalOut LedGreen(LED1);
00020 Serial pc(USBTX, USBRX);
00021 MFRC522 RfChip  (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
00022 
00023 
00024 
00025 
00026 
00027 // main() runs in its own thread in the OS
00028 int main()
00029 {
00030     pc.baud(9600);
00031     pc.printf("\r\n*********************Initialization**********************************");
00032     RfChip.PCD_Init();           /* Init. RC522 Chip*/
00033     pc.printf("\r\n**********************Starting Reading Tags**************************");   
00034        
00035     while (true) {
00036         LedGreen = 1;
00037     if ( ! RfChip.PICC_IsNewCardPresent())
00038     {
00039       wait_ms(500);
00040       continue;
00041     }
00042 
00043     // Select one of the cards
00044     if (!RfChip.PICC_ReadCardSerial())
00045     {
00046       wait_ms(500);
00047       pc.printf("\r\nReading a card");
00048       continue;
00049     }
00050 
00051     LedGreen = 0;
00052 
00053     // Print Card UID
00054     pc.printf("\r\nCard UID: ");
00055     for (uint8_t i = 0; i < RfChip.uid.size; i++)
00056     {
00057       pc.printf(" %X02", RfChip.uid.uidByte[i]);
00058     }
00059     pc.printf("\r\n");
00060 
00061     // Print Card type
00062     uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00063     pc.printf("PICC Type: %s \r\n", RfChip.PICC_GetTypeName(piccType));
00064     wait_ms(1000);
00065     }
00066 }