//main.c File 
#include <16F887.h> 
#FUSES NOWDT                    //No Watch Dog Timer 
#FUSES NOBROWNOUT               //No brownout reset 
#FUSES NOLVP                    //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O 
#FUSES XT 
#use delay(crystal=4000000)//Set frequency at 4Mhz 
#include <C_LCD.h>//2x16 Character LCD driver 

//Note:Connect MFRC522 module to 5v PIC without any Level converter .It'll work fine. 
//Note:mfrc522 work in SPI slave mode(PIC is MASTER) 
//mfrc522 connection PIN 
#define CS PIN_A1//mfrc 522 chip select pin(SS) to PIC pin3 
#define E PIN_A0
//MFRC522 module RST  pin to +3.3v 
//MFRC522 module MOSI pin to PIC16f877 pin 24(RC5/SDO) 
//MFRC522 module MISO pin to PIC16f877 pin 23(RC4/SDI) 
//MFRC522 module SCK  pin to PIC16f877 pin 18(RC3/SCK) 

#include <MFRC522_driver.h>//mfrc 522 Driver 

//spi modes 
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H) 
#define SPI_MODE_1  (SPI_L_TO_H) 
#define SPI_MODE_2  (SPI_H_TO_L) 
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H) 

byte FoundTag; // Variable used to check if Tag was found 
byte ReadTag; // Variable used to store anti-collision value to read Tag information 
byte TagData[16]; // Variable used to store Full Tag Data 
byte GoodTagSerialNumber[4] = {0xB1, 0xC4, 0x35, 0x31}; // The Tag Serial number we are looking for 
int i=0; 
byte version; 
int GoodTag=0; // Variable used to confirm good Tag Detected 



void Get_UID() 
{ 
    // Get Tag UID 
   ReadTag = antiCollision(TagData); 
   lcd_gotoxy(1,1); 
   /// display Tag UID in hexadecimal 
   for(i=0;i<=2;i++)printf(lcd_putc,"%2X,",TagData[i]); 
   printf(lcd_putc,"%2X",(TagData[i]));                
} 

void Search_for_matching_Tag() 
{ 
 GoodTag=0; 
 for(int i=0; i <= 3; i++) 
   { 
    if (GoodTagSerialNumber[i] != TagData[i]) 
       { 
         GoodTag=0; 
          break; // if not equal, then break out of the "for" loop 
       } 
   } 
    
   if (TagData[3]==GoodTagSerialNumber[3]) 
   { 
   // if we made it to 4 loops then the Tag Serial numbers are matching 
   GoodTag=1; 
   } 
   lcd_gotoxy(1,2); 
   if (GoodTag == 1) 
   { 
     printf(lcd_putc,"Access granted  ");        
   } 
   else 
   { 
     printf(lcd_putc,"Access Denied   ");    
     GoodTag=0;      
   } 
} 
void Get_Chip_Version(){ 
while(True) 
     { 
       version = readFromRegister(VersionReg); 

       if(!version) 
         { 
           lcd_gotoxy(1,1); 
           printf(lcd_putc,"NO DEVICE"); 
  
         } 
       if(version) 
         { 
          lcd_gotoxy(1,1); 
          printf(lcd_putc,"DEVICE found");  
          lcd_gotoxy(1, 2); 
          printf(lcd_putc,"ver %2X",version); 
          break; 
         } 
     }    
} 

void main() 
{ 
   setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_64); 
   lcd_init(); 
   lcd_putc('\f'); 
   delay_ms(50); 
   MFRC_522_init(); //initialize MFRC522 
   Get_Chip_Version(); //print Firmwareverion on LCD 
   delay_ms(2000); 
   lcd_putc('\f'); 
 
   while(TRUE) 
   {      
     //  Check to see if a Tag was detected 
     // If yes, then the variable FoundTag will contain "MI_OK" 
    FoundTag = requestTag(MF1_REQIDL, TagData); 
    lcd_gotoxy(1,1); 
    printf(lcd_putc,"Checking for Tag \r\n");      
    
     if (FoundTag == MI_OK) 
     { 
        lcd_gotoxy(1,2);      
        printf(lcd_putc,"Encontrado       "); 
        delay_ms(1000); 
        lcd_putc('\f'); 
        Get_UID(); 
        Search_for_matching_Tag(); 
        delay_ms(1000); 
     } 
      
     else 
         { 
         
         lcd_gotoxy(1,2);          
         printf(lcd_putc,"No encontrado    "); 
         output_high(E);
         delay_ms(1500);
         output_low(E);
         } 
   } 
} 
