E2PRO2 / Mbed 2 deprecated RFID-demo-team2

Dependencies:   mbed

main.cpp

Committer:
janusboandersen
Date:
2019-04-30
Revision:
4:60f9b48d9eb6
Parent:
3:2507fc42675c
Child:
5:b1ac24b6f25d

File content as of revision 4:60f9b48d9eb6:

/*
 * File       : main.cpp
 * License    : MIT License
 * Version    : 0.9
 * Author     : Janus Bo Andersen (JA67494)
 * Last edit  : Janus
 * Created on : 27 Apr 2019
 * Last change: 30 Apr 2019
 * Description: Demo program for Mifare RC-522 RFID reader/writer chip.
 *            : Communicates with RFID reader, transmits card numbers to terminal
 *            : and toggles a digital pin upon reading a card.
 * Structure  : Uses SPI protocol to communicate with the RFID
 *            : Uses U(S)ART to communicate with connected computer/terminal.
 * Adapted for: STM32 Nucleo L432KC, but can be adapted for NXP KL25Z.
 */

/*Connect as follows:
  (should be connected to board standard pins)
  RFID pins          ->   Nucleo header CN5 (Arduino-compatible header)
  ----------------------------------------
  RFID IRQ  = pin5   ->   Not used. Leave open
  RFID MISO = pin4   ->   Nucleo SPI_MISO = PA_6 = D12
  RFID MOSI = pin3   ->   Nucleo SPI_MOSI = PA_7 = D11
  RFID SCK  = pin2   ->   Nucleo SPI_SCK  = PA_5 = D13
  RFID SDA  = pin1   ->   Nucleo SPI_CS   = PB_6 = D10
  RFID RST  = pin7   ->   Nucleo          = PA_9 = D8

Power supply MUST BE 3.3V and GND to the respective pins.
*/                              
#include "mbed.h"
#include "MFRC522.h"

// FRDM KL25Z Pins for MFRC522
#define MF_RESET  D8  //Or anything else
#define SERIAL_TX D0  //UART0
#define SERIAL_RX D1  //UART0
#define SPI_MOSI  D11 //PTC17
#define SPI_MISO  D12 //PTA16
#define SPI_SCK   D13 //PTA17
#define SPI_CS    D10 //Chip select can be any digital pin

DigitalOut LedGreen(LED1); //for showing active signal on dev board
DigitalOut LedPower(A2); //for toggling MOSFET

//Serial connection to PC for output
Serial pc(SERIAL_TX, SERIAL_RX);

//RFID object instantiated using board standard pins (find in your library)
MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);

int main(void) {
  pc.printf("starting...\n");
  LedPower = 0;

  // Init. RC522 Chip
  RfChip.PCD_Init();

  while (true) {
    LedGreen = 1;

    // Look for new cards
    if ( ! RfChip.PICC_IsNewCardPresent())
    {
      wait_ms(500);
      continue;
    }

    // Select one of the cards (avoiding collisions of multiple cards)
    if ( ! RfChip.PICC_ReadCardSerial())
    {
      wait_ms(500);
      continue;
    }

    LedGreen = 0; //blink off

    //Toggle signal to the MOSFET
    
    LedPower = !LedPower;    
    // Print Card UID to serial interface
    pc.printf("Card UID: ");
    for (uint8_t i = 0; i < RfChip.uid.size; i++)
    {
      pc.printf(" %X02", RfChip.uid.uidByte[i]);
    }
    pc.printf("\n\r");

    // Print Card type
    uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
    pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
    wait_ms(1000);
  }
}