My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
CupRFIDReaderCode  
Updated Dec 1, 2013 by rijadsul...@gmail.com

Code


/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/
  
#if defined(__XC) 
    #include <xc.h>        /* XC8 General Include File */ 
#elif defined(HI_TECH_C) 
    #include <htc.h>       /* HiTech General Include File */ 
#elif defined(__18CXX) 
    #include <p18cxxx.h>   /* C18 General Include File */ 
#endif 
  
#if defined(__XC) || defined(HI_TECH_C) 
  
#include <stdint.h>        /* For uint8_t definition */ 
#include <stdbool.h>       /* For true/false definition */ 
#include <string.h> 
  
#endif 
  
#include "system.h"        /* System funct/params, like osc/peripheral config */ 
#include "user.h"          /* User funct/params, such as InitApp */ 
#include <usart.h> 
#include <stdlib.h> 
#include <string.h> 
#include <delays.h> 
  
  
  
  
  
//#define FOUNT_NUM1 
#define FOUNT_NUM2 
  
#if defined(FOUNT_NUM1) 
    #define CARD card1 
#elif defined(FOUNT_NUM2) 
    #define CARD card2 
#endif 
  
/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/
  
unsigned char card1[13] = "6A003E4FA7BC";   // Card number 1 
unsigned char card2[13] = "6A003E714366";   // Card number 2 
  
// No idea why, but 17 is the magic number 
unsigned char scanned[17];                  // Scanned in char array 
unsigned char formated[12];                 // Formated char array 
  
// Again, no idea why it works with these random values, but it does 
int RFID_SIZE = 16; 
int counter = 0; 
  
int i; 
  
int rfidStatus( void ); 
  
/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
  
  
  
void main(void) 
{ 
    // Setting OSC 
    OSCCON = 0b01110111; 
  
    // PORTC to outputs 
    TRISC = 0; 
  
    // Global Interupts 
    INTCONbits.GIE  = 1; 
    INTCONbits.PEIE = 1; 
  
    // Initalize USART 
    Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF 
            & USART_ASYNCH_MODE & USART_EIGHT_BIT 
            & USART_SINGLE_RX & USART_BRGH_LOW, 25);  // try 12, 51, 207 
  
    RCSTA1bits.SPEN = 1;    // Enable USART 
    RCSTA1bits.RX9 = 0;     // Nine Bit mode off 
    RCSTA1bits.CREN = 1;    // Something 
    RCSTA1bits.ADDEN = 0;   // Something 
  
    TRISCbits.TRISC6 = 0;   //TX1 
    TRISCbits.TRISC7 = 1;   //RX1 
  
    Delay1KTCYx(4); 
  
    while(1) 
    { 
  
        if(rfidStatus()) 
        { 
            // Stuff for correct card 
            while(Busy1USART()); 
            Write1USART('Y'); 
            while(Busy1USART()); 
            Write1USART('U'); 
            while(Busy1USART()); 
            Write1USART('P'); 
        } 
        else
        { 
            // Stuff for incorrect card 
            while(Busy1USART()); 
            Write1USART('N'); 
            while(Busy1USART()); 
            Write1USART('0'); 
            while(Busy1USART()); 
            Write1USART('P'); 
            while(Busy1USART()); 
            Write1USART('E'); 
        }        
    } 
  
  
} 
  
  
  
int rfidStatus( void ) 
{ 
    INTCONbits.GIE  = 1; 
    PIR1bits.RC1IF = 0; 
  
    // Read in card number 
    while (counter < RFID_SIZE) 
    { 
        while (!PIR1bits.RC1IF);        // Wait for recieve flag to be set 
        scanned[counter] = RCREG1;      // Store character in array 
        counter++;                      // Increment counter 
        RCREG1 = 0x00;                  // Clear register 
    } 
    // clear counter for next scan 
    counter = 0; 
  
    // Remove nonsense characters 
    for (i = 0; i <= RFID_SIZE; i++) 
    { 
        // Accept 0-9 and A-F 
        if ( (scanned[i] >= 0x30 && scanned[i] <= 0x39) 
                    || (scanned[i] >= 0x41 && scanned[i] <= 0x46) ) 
        { 
            formated[counter] = scanned[i]; 
            counter++; 
        } 
    } 
    // Clear counter again 
    counter = 0; 
  
    // 
    // Handle scanned input 
    // 
    if (formated[10] == CARD[10] && formated[11] == CARD[11]) 
        // Scanned matches Card1 
        return 1; 
    else
        // Scanned doesn't match Card1 
        return 0; 
  
}

Powered by Google Project Hosting