Source Code for RFID

Hi all, I have been trying to interface the MiFare RFID reader with Arduino but am quite unsuccessful. I have the code in C, but am not sure how to implement it. Here is the code:

#define __SRC

#include "main.h"

#undef __SRC

#include "Mifare500.h"

#define MF1_S50 0

#define MF1_S70 1

#define MF0_ULIGHT 2

#define MF1_LIGHT 3

sbit Mifare500_RST=P1^4;

uchar baud_num;

uchar card_snr[8];

void serial_init(void);

uchar code Nkey_a[6] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};

uchar code Nkey_b[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

main()

{

uchar cardtype=MF1_S50;

uchar i,j=40;

uchar databuf[16];

long value=1;

for(i=255;i>0;i--)

for(j=10;j>0;j--);

Mifare500_RST=0;

for(i=255;i>0;i--)

for(j=20;j>0;j--);

baud_num=2;

serial_init();

EA=1;

//i=mifs_config();

i=mifs_get_info(databuf);

// i=mifs_close();

// i=mifs_config();

// i=mifs_load_key(KEYA,15,Nkey_a);

i=mifs_load_key(KEYA,5,Nkey_b);

// i=mifs_read_E2(0x10,16,databuf);

// i=mifs_read_E2(0x20,16,databuf);

// databuf[0]=0x58;

// i=mifs_write_E2(0x11,1,databuf);

i=mifs_write_reg(0x11,0x59);

i=mifs_write_reg(0x12,0x01); //'?¡[ch937][ch8719]?[ch8747]Ø [ch733]ø?[ch937]´[ch8706]¡ø®æ?¿Î[ch9674]Ó[ch8706]ã¨[ch960]¶[ch8747]?[ch9674]ÓµÕ

while(1)

{

while(mifs_request(IDLE,databuf)!=MI_OK);

if(mifs_cascanticoll(ANTICOLL1,0,card_snr)!=MI_OK) continue;

if(mifs_cascselect(ANTICOLL1,card_snr,&i)!=MI_OK) continue;

if(i&0x04)

{

if(mifs_cascanticoll(ANTICOLL2,0,card_snr+4)!=MI_OK) continue;

if(mifs_cascselect(ANTICOLL2,card_snr+4,&i)!=MI_OK) continue;

}

if(cardtype == MF1_S50)

{

if(mifs_authentication(KEYA,5)!=0) continue;

databuf[0]=0x10;

databuf[4]=~0x10;

databuf[8]=0x10;

for(i=1;i<4;i++)

{

databuf*=0x00;*

  • databuf[4+i]=0xff;*

  • databuf[8+i]=0x00;*

  • }*

  • databuf[12]=0x14;*

  • databuf[13]=~0x14;*

  • databuf[14]=0x14;*

  • databuf[15]=~0x14;*

  • if(mifs_write(0x14,databuf)!=0) continue;*

  • databuf[0]=0;*

  • if(mifs_read(0x14,databuf)!=0) continue;*

  • value=1;*

  • if(mifs_value(PICC_DECREMENT,0x14,&value,0x15)!=0) continue;*

  • if(mifs_read(0x15,databuf)!=0) continue;*

  • if(mifs_value(PICC_INCREMENT,0x14,&value,0x16)!=0) continue;*

  • if(mifs_read(0x16,databuf)!=0) continue;*

  • }*

  • else if(cardtype == MF1_S70)*

  • {*

  • if(mifs_authentication2(KEYA,39,5)!=0) continue;*

  • if(mifs_read(240,databuf)!=0) continue;*

  • }*

  • else if(cardtype == MF0_ULIGHT)*

  • {*

  • databuf[0]=0x11;*

  • databuf[1]=0x22;*

  • databuf[2]=0x33;*

  • databuf[3]=0x44;*

  • if(mifs_ULwrite(15,databuf)!=0) continue;*

  • databuf[0]=0x00;*

  • databuf[1]=0x00;*

  • databuf[2]=0x00;*

  • databuf[3]=0x00;*

  • if(mifs_read(15,databuf)!=0) continue;*

  • }*

  • else if(cardtype == MF1_LIGHT)*

  • {*

  • if(mifs_authKey(KEYA,2,Nkey_b)!=0) continue;*

  • databuf[0]=0x10;*

  • databuf[1]=0x00;*

  • databuf[2]=0xef;*

  • databuf[3]=0xff;*

  • if(mifs_write(4,databuf)!=0) continue;*

  • databuf[0]=0xff;*

  • databuf[1]=0xff;*

  • databuf[2]=0xff;*

  • databuf[3]=0xff;*

  • if(mifs_write(5,databuf)!=0) continue;*

  • if(mifs_read(4,databuf)!=0) continue;*

  • if(databuf[0]!=~databuf[2] || databuf[1]!=~databuf[3])*

  • continue;*

  • value=1;*

  • if(mifs_valuedebit(0xc0,4,&value)!=0) continue;*

  • if(mifs_read(4,databuf)!=0) continue;*

  • }*

  • mifs_halt();*
    // mifs_reset(1);

  • // i=mifs_write_E2(0x30,16,databuf);*

  • // if(i!=0) continue;*

  • mifs_clr_control_bit();*

  • mifs_buzzer(250,20,10,3);*

  • while(mifs_set_control_bit()!=0);*

  • }*
    }
    /****************************************************************************
    * *
    * Function: serial_init
    _
    _
    _
    Input: - _
    _
    Output: - _
    _
    _
    _
    Description: _
    _
    _
    _
    _
    _
    ***************************************************************************/_
    void serial_init (void)
    {

  • PCON = 0x80; // SMOD = 1;*

  • SCON = 0x50; // Mode 1, 8-bit UART, enable receiption*

  • TMOD&=0x0f;*

  • TMOD|=0x20; // Timer 1, mode 2, 8-bit auto reload,*

  • switch(baud_num)*

  • { *

  • case 0: TH1=BAUD_2400; break;*

  • case 1: TH1=BAUD_4800; break;*

  • case 2: TH1=BAUD_9600; break;*

  • case 3: TH1=BAUD_19200; break;*

  • case 4: TH1=BAUD_28800; break;*

  • case 5: TH1=BAUD_57600; break;*

  • }*

  • CON_485=0;*

  • ES = 1;*

  • ET1 = 0;*

  • TR1 = 1;*
    }
    #pragma noaregs
    Any help would be great!