E2PRO2 / Mbed 2 deprecated RFID-demo-team2

Dependencies:   mbed

main.cpp

Committer:
janusboandersen
Date:
2019-05-02
Revision:
5:b1ac24b6f25d
Parent:
4:60f9b48d9eb6
Child:
6:11cf998faa50

File content as of revision 5:b1ac24b6f25d:

/*
 * File       : main.cpp
 * License    : MIT License
 * Version    : 0.91
 * Author     : Janus Bo Andersen (JA67494)
 * Last edit  : Janus
 * Created on : 27 Apr 2019
 * Last change: 2 May 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.
 * Changelog  : 27 Apr 2019: Created demo program
              : 02 May 2019: Update pin mappings for FRDM-KL25Z Board
              : 02 May 2019: Added set and reset for a latched circuit
 */

/*Connect as follows:
  (should be connected to board standard pins)
  RFID pins          ->   Board pinName   L432KC       FRDM-KL25Z
  ---------------------------------------------------------------------
  RFID IRQ  = pin5   ->   Not used. Leave open
  RFID MISO = pin4   ->   SPI_MISO        PA_6=D12     PTD3
  RFID MOSI = pin3   ->   SPI_MOSI        PA_7=D11     PTD2
  RFID SCK  = pin2   ->   SPI_SCK         PA_5=D13     PTD1
  RFID SDA  = pin1   ->   SPI_CS          PB_6=D10     PTD0
  RFID RST  = pin7   ->                   PA_9=D8      PTA13 (or map sth. else)

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

// FRDM KL25Z Pins
#define MF_RESET  PTA13 //D8  //Or anything else
#define SERIAL_TX PTE0  //UART0
#define SERIAL_RX PTE1  //UART0
#define SPI_MOSI  PTD2 //PTC17
#define SPI_MISO  PTD3 //PTA16
#define SPI_SCK   PTD1 //PTA17
#define SPI_CS    PTD0 //Chip select can be any digital pin

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

//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) {
  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);
    
    //Turn off latch (Jan's addition)
    wait_ms(500);
    LedPowerOff = !LedPower;
  }
}