Access Control Using RFID MIFARE CARD

Dependencies:   MFRC522 NextionLCD

Committer:
shivanandgowdakr
Date:
Tue Aug 21 11:18:56 2018 +0000
Revision:
0:a5d3db2f2625
Access Control Using MIFARE RFID Card, and with details displayed on Nextion LCD;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shivanandgowdakr 0:a5d3db2f2625 1
shivanandgowdakr 0:a5d3db2f2625 2 #include "MFRC522.h"
shivanandgowdakr 0:a5d3db2f2625 3 #include "Card.h"
shivanandgowdakr 0:a5d3db2f2625 4
shivanandgowdakr 0:a5d3db2f2625 5 #define MIFARE_MOSI PA_7
shivanandgowdakr 0:a5d3db2f2625 6 #define MIFARE_MISO PA_6
shivanandgowdakr 0:a5d3db2f2625 7 #define MIFARE_SCLK PA_5
shivanandgowdakr 0:a5d3db2f2625 8 #define MIFARE_CS PA_4
shivanandgowdakr 0:a5d3db2f2625 9 #define MIFARE_RESET PB_4
shivanandgowdakr 0:a5d3db2f2625 10
shivanandgowdakr 0:a5d3db2f2625 11 MFRC522 mfrc522(MIFARE_MOSI,MIFARE_MISO,MIFARE_SCLK,MIFARE_CS,MIFARE_RESET);
shivanandgowdakr 0:a5d3db2f2625 12 //MFRC522 mfrc522 (PA_7, PA_6, PA_5, PD_14, PD_15);
shivanandgowdakr 0:a5d3db2f2625 13
shivanandgowdakr 0:a5d3db2f2625 14 bool New_Card=false,Read_Card=false;
shivanandgowdakr 0:a5d3db2f2625 15
shivanandgowdakr 0:a5d3db2f2625 16 extern char CARD_UID= {'\0'};
shivanandgowdakr 0:a5d3db2f2625 17 MFRC522::MIFARE_Key key;
shivanandgowdakr 0:a5d3db2f2625 18 void MIFARE_Init(void)
shivanandgowdakr 0:a5d3db2f2625 19
shivanandgowdakr 0:a5d3db2f2625 20 {
shivanandgowdakr 0:a5d3db2f2625 21 mfrc522.PCD_Init();
shivanandgowdakr 0:a5d3db2f2625 22
shivanandgowdakr 0:a5d3db2f2625 23 }
shivanandgowdakr 0:a5d3db2f2625 24
shivanandgowdakr 0:a5d3db2f2625 25
shivanandgowdakr 0:a5d3db2f2625 26
shivanandgowdakr 0:a5d3db2f2625 27 void Read_Card_Details(char *CARD_UID)
shivanandgowdakr 0:a5d3db2f2625 28 {
shivanandgowdakr 0:a5d3db2f2625 29 memset(CARD_UID,'\0',9);
shivanandgowdakr 0:a5d3db2f2625 30 for (uint8_t i = 0; i < 6; i++)
shivanandgowdakr 0:a5d3db2f2625 31 key.keyByte[i] = 0xFF;
shivanandgowdakr 0:a5d3db2f2625 32
shivanandgowdakr 0:a5d3db2f2625 33 // Look for new cards
shivanandgowdakr 0:a5d3db2f2625 34
shivanandgowdakr 0:a5d3db2f2625 35 if ( mfrc522.PICC_IsNewCardPresent()) {
shivanandgowdakr 0:a5d3db2f2625 36 wait_ms(50);
shivanandgowdakr 0:a5d3db2f2625 37 New_Card=true;
shivanandgowdakr 0:a5d3db2f2625 38 }
shivanandgowdakr 0:a5d3db2f2625 39
shivanandgowdakr 0:a5d3db2f2625 40 // Select one of the cards
shivanandgowdakr 0:a5d3db2f2625 41
shivanandgowdakr 0:a5d3db2f2625 42 if ( mfrc522.PICC_ReadCardSerial()) {
shivanandgowdakr 0:a5d3db2f2625 43
shivanandgowdakr 0:a5d3db2f2625 44 wait_ms(50);
shivanandgowdakr 0:a5d3db2f2625 45 Read_Card=true;
shivanandgowdakr 0:a5d3db2f2625 46 }
shivanandgowdakr 0:a5d3db2f2625 47
shivanandgowdakr 0:a5d3db2f2625 48
shivanandgowdakr 0:a5d3db2f2625 49 if(Read_Card==true && New_Card==true) {
shivanandgowdakr 0:a5d3db2f2625 50 char str1[3],str2[3],str3[3],str4[3];
shivanandgowdakr 0:a5d3db2f2625 51 getHexa(mfrc522.uid.uidByte[0], str1);
shivanandgowdakr 0:a5d3db2f2625 52 getHexa(mfrc522.uid.uidByte[1], str2);
shivanandgowdakr 0:a5d3db2f2625 53 getHexa(mfrc522.uid.uidByte[2], str3);
shivanandgowdakr 0:a5d3db2f2625 54 getHexa(mfrc522.uid.uidByte[3], str4);
shivanandgowdakr 0:a5d3db2f2625 55 CARD_UID[0]=str4[0];
shivanandgowdakr 0:a5d3db2f2625 56 CARD_UID[1]=str4[1];
shivanandgowdakr 0:a5d3db2f2625 57 CARD_UID[2]=str3[0];
shivanandgowdakr 0:a5d3db2f2625 58 CARD_UID[3]=str3[1];
shivanandgowdakr 0:a5d3db2f2625 59 CARD_UID[4]=str2[0];
shivanandgowdakr 0:a5d3db2f2625 60 CARD_UID[5]=str2[1];
shivanandgowdakr 0:a5d3db2f2625 61 CARD_UID[6]=str1[0];
shivanandgowdakr 0:a5d3db2f2625 62 CARD_UID[7]=str1[1];
shivanandgowdakr 0:a5d3db2f2625 63 CARD_UID[8]='\0';
shivanandgowdakr 0:a5d3db2f2625 64 printf("CARD_UID : %s\r\n",CARD_UID);
shivanandgowdakr 0:a5d3db2f2625 65 Read_Card=false;
shivanandgowdakr 0:a5d3db2f2625 66 New_Card=false;
shivanandgowdakr 0:a5d3db2f2625 67 }
shivanandgowdakr 0:a5d3db2f2625 68 }
shivanandgowdakr 0:a5d3db2f2625 69
shivanandgowdakr 0:a5d3db2f2625 70 void getHexa(char checkSum, char *str)
shivanandgowdakr 0:a5d3db2f2625 71 {
shivanandgowdakr 0:a5d3db2f2625 72 char strtemp[3];
shivanandgowdakr 0:a5d3db2f2625 73
shivanandgowdakr 0:a5d3db2f2625 74 memset(strtemp, 0x00, sizeof(strtemp));
shivanandgowdakr 0:a5d3db2f2625 75 sprintf(strtemp, "%02X", checkSum);
shivanandgowdakr 0:a5d3db2f2625 76 memcpy(str, strtemp, 2);
shivanandgowdakr 0:a5d3db2f2625 77
shivanandgowdakr 0:a5d3db2f2625 78 return;
shivanandgowdakr 0:a5d3db2f2625 79 }
shivanandgowdakr 0:a5d3db2f2625 80