RFID Reader - VB.net Source code

RFID Reader - VB.net Source code




















Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frm_main
    Inherits System.Windows.Forms.Form
  
    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
        Dim port, i, baud As Integer
        Dim j As Short
        Dim buf1(200) As Byte
        Dim b1 As Byte
        Dim s1 As String
        port = cb_ckh.SelectedIndex + 1
        If (port = 0) Then
            lb_info.Text = "Please select COM Port!"
        End If
        baud = CInt(cb_btl.Text)
        If (baud = 0) Then
            lb_info.Text = "Please select Baud rate!"
        End If
        'Open Port
        i = rf_init_com(port, baud)
        If (i <> 0) Then
            lb_info.Text = "Open Port Fail!"
            Exit Sub
        End If
        'Request
        i = rf_request(0, &H52s, j)
        If (i <> 0) Then
            lb_info.Text = "Request Fail!"
            Exit Sub
        End If
        'Anticollision
        i = rf_anticoll(0, 4, buf1(0), b1)
        If (i <> 0) Then
            lb_info.Text = "Anticollision Fail!"
            Exit Sub
        End If
        s1 = ""
        For i = 0 To b1 - 1
            s1 = s1 & VB.Right("00" & Hex(buf1(i)), 2)
        Next i
        tx_kh.Text = s1
        'Select card
        i = rf_select(0, buf1(0), 4, b1)
        If (i <> 0) Then
            lb_info.Text = "Select card fail!"
            Exit Sub
        End If
        lb_info.Text = "Select card succeed!"
    End Sub
  
    Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
        Dim i, m As Integer
        Dim buf1(200) As Byte
        Dim buf2(200) As Byte
        Dim s1 As String
        Dim b2, b1, b3 As Byte
        s1 = Trim(tx_key.Text)
        If (Len(s1) <> 12) Then
            lb_info.Text = "Wrong Key Length!"
            tx_key.Focus()
            Exit Sub
        End If
        For i = 0 To 5
            buf1(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
        Next i
        m = cb_kh.SelectedIndex
        If (m = -1) Then
            lb_info.Text = "Select Block Please!"
            Exit Sub
        End If
        If (op_a.Checked) Then
            b1 = &H60s
        End If
        If (op_b.Checked) Then
            b1 = &H61s
        End If
        b3 = CByte(m)
        'Authentication
        i = rf_M1_authentication2(0, b1, b3, buf1(0))
        If (i <> 0) Then
            lb_info.Text = "Authentication Fail"
            Exit Sub
        End If
        'Read card
        i = rf_M1_read(0, b3, buf2(0), b2)
        If (i <> 0) Then
            lb_info.Text = "Read Card Fail!"
            Exit Sub
        End If
        s1 = ""
        For i = 0 To b2 - 1
            s1 = s1 & VB.Right("00" & Hex(buf2(i)), 2)
        Next i
        tx_sj.Text = s1
        lb_info.Text = "Read Succeed!"
    End Sub
  
    Private Sub Command3_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command3.Click
        Dim i, m As Integer
        Dim buf1(200) As Byte
        Dim buf2(200) As Byte
        Dim s1 As String
        Dim b2, b1, b3 As Byte
        s1 = Trim(tx_key.Text)
        If (Len(s1) <> 12) Then
            lb_info.Text = "Wrong Key Length!"
            tx_key.Focus()
            Exit Sub
        End If
        For i = 0 To 5
            buf1(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
        Next i
        m = cb_kh.SelectedIndex
        If (m = -1) Then
            lb_info.Text = "Select Block Please!"
            Exit Sub
        End If
        If (op_a.Checked) Then
            b1 = &H60s
        End If
        If (op_b.Checked) Then
            b1 = &H61s
        End If
      
        s1 = Trim(tx_sj.Text)
        If (Len(s1) <> 32) Then
            lb_info.Text = "Wrong Data length "
            tx_sj.Focus()
            Exit Sub
        End If
        For i = 0 To 15
            buf2(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
        Next i
        'Authentication
        b3 = CByte(m)
        i = rf_M1_authentication2(0, b1, b3, buf1(0))
        If (i <> 0) Then
            lb_info.Text = "Authentication Fail!"
            Exit Sub
        End If
        'Write card
        i = rf_M1_write(0, b3, buf2(0))
        If (i <> 0) Then
            lb_info.Text = "Write Card Fail"
            Exit Sub
        End If
      
        lb_info.Text = "Write Succeed!"
    End Sub
  
    Private Sub Command4_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command4.Click
        Dim i As Integer
        i = rf_halt(0)
        If (i <> 0) Then
            lb_info.Text = "Halt Fail!"
            Exit Sub
        End If
        lb_info.Text = "Halt Succeed"
    End Sub
  
    Private Sub frm_main_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        For i = 0 To 63
            cb_kh.Items.Insert(i, CStr(i))
        Next i
        cb_ckh.SelectedIndex = 0 : cb_btl.SelectedIndex = 1 : cb_kh.SelectedIndex = 4
    End Sub
End Class

Download Source code


RFID on Ebay Store








RFID Reader - C# Source code

RFID Reader - C# Source code














====================================================================
       MICROSOFT FOUNDATION CLASS LIBRARY : MifareOne
====================================================================


AppWizard has created this MifareOne application for you.  This application
not only demonstrates the basics of using the Microsoft Foundation classes
but is also a starting point for writing your application.

This file contains a summary of what you will find in each of the files that
make up your MifareOne application.

MifareOne.dsp
    This file (the project file) contains information at the project level and
    is used to build a single project or subproject. Other users can share the
    project (.dsp) file, but they should export the makefiles locally.

MifareOne.h
    This is the main header file for the application.  It includes other
    project specific headers (including Resource.h) and declares the
    CMifareOneApp application class.

MifareOne.cpp
    This is the main application source file that contains the application
    class CMifareOneApp.

MifareOne.rc
    This is a listing of all of the Microsoft Windows resources that the
    program uses.  It includes the icons, bitmaps, and cursors that are stored
    in the RES subdirectory.  This file can be directly edited in Microsoft
    Visual C++.

MifareOne.clw
    This file contains information used by ClassWizard to edit existing
    classes or add new classes.  ClassWizard also uses this file to store
    information needed to create and edit message maps and dialog data
    maps and to create prototype member functions.

res\MifareOne.ico
    This is an icon file, which is used as the application's icon.  This
    icon is included by the main resource file MifareOneถมะดภืำ.rc.

res\MifareOne.rc2
    This file contains resources that are not edited by Microsoft
    Visual C++.  You should place all resources not editable by
    the resource editor in this file.




/////////////////////////////////////////////////////////////////////////////

AppWizard creates one dialog class:

MainDlg.h, MainDlg.cpp - the dialog
    These files contain your CMainDlg class.  This class defines
    the behavior of your application's main dialog.  The dialog's
    template is in MifareOne.rc, which can be edited in Microsoft
    Visual C++.


/////////////////////////////////////////////////////////////////////////////
Other standard files:

StdAfx.h, StdAfx.cpp
    These files are used to build a precompiled header (PCH) file
    named MifareOne.pch and a precompiled types file named StdAfx.obj.

Resource.h
    This is the standard header file, which defines new resource IDs.
    Microsoft Visual C++ reads and updates this file.

/////////////////////////////////////////////////////////////////////////////
Other notes:

AppWizard uses "TODO:" to indicate parts of the source code you
should add to or customize.

If your application uses MFC in a shared DLL, and your application is
in a language other than the operating system's current language, you
will need to copy the corresponding localized resources MFC42XXX.DLL
from the Microsoft Visual C++ CD-ROM onto the system or system32 directory,
and rename it to be MFCLOC.DLL.  ("XXX" stands for the language abbreviation.
For example, MFC42DEU.DLL contains resources translated to German.)  If you
don't do this, some of the UI elements of your application will remain in the
language of the operating system.

/////////////////////////////////////////////////////////////////////////////

Download Source code


RFID on Ebay Store




RFID Reader - VB Source code

RFID Reader - VB Source code























Option Explicit

Private Sub Command1_Click()
Dim i&, j%, port&, baud&, buf1(200) As Byte, b1 As Byte, s1$
port = cb_ckh.ListIndex + 1
If (port = 0) Then
    lb_info.Caption = "Please select COM Port!"
End If
baud = CLng(cb_btl.Text)
If (baud = 0) Then
    lb_info.Caption = "Please select Baud rate!"
End If
'Open Port
i = rf_init_com(port, baud)
If (i <> 0) Then
    lb_info.Caption = "Open Port Fail!"
    Exit Sub
End If
'Request
i = rf_request(0, &H52, j)
If (i <> 0) Then
    lb_info.Caption = "Request Fail!"
    Exit Sub
End If
'Anticollision
i = rf_anticoll(0, 4, buf1(0), b1)
If (i <> 0) Then
    lb_info.Caption = "Anticollision Fail!"
    Exit Sub
End If
s1 = ""
For i = 0 To b1 - 1
    s1 = s1 & Right("00" & Hex(buf1(i)), 2)
Next i
tx_kh.Text = s1
'Select card
i = rf_select(0, buf1(0), 4, b1)
If (i <> 0) Then
    lb_info.Caption = "Select card fail!"
    Exit Sub
End If
lb_info.Caption = "Select card succeed!"
End Sub

Private Sub Command2_Click()
Dim i&, m&, buf1(200) As Byte, buf2(200) As Byte, s1$, b1 As Byte, b2 As Byte, b3 As Byte
s1 = Trim(tx_key.Text)
If (Len(s1) <> 12) Then
    lb_info.Caption = "Wrong Key Length!"
    tx_key.SetFocus
    Exit Sub
End If
For i = 0 To 5
    buf1(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
Next i
m = cb_kh.ListIndex
If (m = -1) Then
    lb_info.Caption = "Select Block Please!"
    Exit Sub
End If
If (op_a.Value) Then
   b1 = &H60
End If
If (op_b.Value) Then
   b1 = &H61
End If
b3 = CByte(m)
'Authentication
i = rf_M1_authentication2(0, b1, b3, buf1(0))
If (i <> 0) Then
    lb_info.Caption = "Authentication Fail£¡"
    Exit Sub
End If
'Read card
i = rf_M1_read(0, b3, buf2(0), b2)
If (i <> 0) Then
    lb_info.Caption = "Read Card Fail!"
    Exit Sub
End If
s1 = ""
For i = 0 To b2 - 1
    s1 = s1 & Right("00" & Hex(buf2(i)), 2)
Next i
tx_sj.Text = s1
lb_info.Caption = "Read Succeed!"
End Sub

Private Sub Command3_Click()
Dim i&, m&, buf1(200) As Byte, buf2(200) As Byte, s1$, b1 As Byte, b2 As Byte, b3 As Byte
s1 = Trim(tx_key.Text)
If (Len(s1) <> 12) Then
    lb_info.Caption = "Wrong Key Length!"
    tx_key.SetFocus
    Exit Sub
End If
For i = 0 To 5
    buf1(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
Next i
m = cb_kh.ListIndex
If (m = -1) Then
    lb_info.Caption = "Select Block Please!"
    Exit Sub
End If
If (op_a.Value) Then
   b1 = &H60
End If
If (op_b.Value) Then
   b1 = &H61
End If

s1 = Trim(tx_sj.Text)
If (Len(s1) <> 32) Then
    lb_info.Caption = "Wrong Data length "
    tx_sj.SetFocus
    Exit Sub
End If
For i = 0 To 15
    buf2(i) = Val("&H" & Mid(s1, i * 2 + 1, 2))
Next i
'Authentication
b3 = CByte(m)
i = rf_M1_authentication2(0, b1, b3, buf1(0))
If (i <> 0) Then
    lb_info.Caption = "Authentication Fail!"
    Exit Sub
End If
'Write card
i = rf_M1_write(0, b3, buf2(0))
If (i <> 0) Then
    lb_info.Caption = "Write Card Fail£¡"
    Exit Sub
End If

lb_info.Caption = "Write Succeed!"
End Sub

Private Sub Command4_Click()
Dim i&
i = rf_halt(0)
If (i <> 0) Then
    lb_info.Caption = "Halt Fail!"
    Exit Sub
End If
lb_info.Caption = "Halt Succeed"
End Sub

Private Sub Form_Load()
Dim i&
For i = 0 To 63
    cb_kh.AddItem CStr(i), i
Next i
cb_ckh.ListIndex = 0: cb_btl.ListIndex = 1: cb_kh.ListIndex = 4
End Sub

Download Source code


RFID on Ebay Store




RFID Reader/Writer USB Driver Installation













RFID Reader/Writer USB Driver Installation


First : To Install USB Driver ( Do not connect the RFID reader yet )






























Second : Connect RFID Reader to the USB port of PC.


You can see the virtual COM No. on the “device manager ” as follows


















In This sample found COM3


RFID Module interface MCS51 Microcontroller & C Source code examples















RFID Module interface MCS51 Microcontroller & C Source code examples


////////////////////////////////////////////////////////////
//Test SL025x
//MCS51 source code
////////////////////////////////////////////////////////////  
#include <reg52.h>
#include <string.h>

#define  true     1
#define  false    0

#define  OSC_FREQ        22118400L
#define  TIME0_10ms      65536L - OSC_FREQ/1200L

#define  BOUND4800       256 - 24
#define  BOUND9600       256 - 12
#define  BOUND14400      256 - 8
#define  BOUND19200      256 - 6
#define  BOUND28800      256 - 4
#define  BOUND38400      256 - 3
#define  BOUND57600      256 - 2
#define  BOUND115200     256 - 1

//IO definition
sbit     CARDIN    =    P1^0;                                                    

void InitializeSystem();                                                          
void SendCom(unsigned char *g_cCommand);                                          
void StartTime(unsigned int _MS);                                                 
void StopTime();
char ReadWriteUltralight(void);
char ReadWriteMifareStd(void);

//Command List, preamble + length + command code + data frame
unsigned char code SelectCard[3] =     {0xBA,0x02,0x01 };      
unsigned char code LoginSector0[11] =  {0xBA,0x0A,0x02,0x00,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; 
unsigned char code LoginSector1[11] =  {0xBA,0x0A,0x02,0x01,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
unsigned char code ReadBlock1[4]   =   {0xBA,0x03,0x03,0x01};                                    
unsigned char code WriteBlock1[20] =   {0xBA,0x13,0x04,0x01,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};                               
unsigned char code ReadValue[4]  =     {0xBA,0x03,0x05,0x05}; 
unsigned char code InitializeValue[8] ={0xBA,0x07,0x06,0x04,0x00,0x00,0x00,0x01};                               
unsigned char code IncrementValue[8] = {0xBA,0x07,0x08,0x04,0x00,0x00,0x00,0x20};                               
unsigned char code DecrementValue[8] = {0xBA,0x07,0x09,0x04,0x03,0x00,0x00,0x00};                                         
unsigned char code CopyValue[5] =      {0xBA,0x04,0x0A,0x04,0x05};
unsigned char code ReadULPage5[4] =    {0xBA,0x03,0x10,0x05};                                         
unsigned char code WriteULPage5[8] =   {0xBA,0x07,0x11,0x05,0x11,0x22,0x33,0x44};
unsigned char code TurnOnRedLed[4] =   {0xBA,0x03,0x40,0x01};    
unsigned char code TurnOffRedLed[4] =  {0xBA,0x03,0x40,0x00};            
                          
unsigned char g_ucReceBuf[21];                                     
unsigned char g_ucReceNum;                                         
unsigned char g_ucWatchTime;
unsigned char g_ucCardType;
char g_cStatus;                                      
char g_cErr;

bit   g_bReceOver;                                                
bit   g_bReceReady;
bit   g_bTimeOut;                                                 

void main()
{
     InitializeSystem();  
    
     while(1)
     {

         while (!CARDIN)
         {
              g_cErr = 0;

//Get the unique serial number of Mifare Card
              SendCom(SelectCard);                              //
              StartTime(8);                                     //waitting until receive response from SL013 or over 80ms
              while (!g_bReceOver && !g_bTimeOut);              //
              StopTime();                                       //
              if ((g_ucReceBuf[2]!=0x01) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
              {   continue;   }    


              if (g_ucReceBuf[1] == 8)
             {
                 g_ucCardType = g_ucReceBuf[8];
             }
             else
             {
                 g_ucCardType = g_ucReceBuf[11];
             }
            
             switch(g_ucCardType)
              { 
                  case 1://Mifare_Std_1K
                      g_cStatus = ReadWriteMifareStd( );
                      if (g_cStatus != true)
                      {    g_cErr = 3;    }
                      break;
                  case 2://Mifare_Pro
                      g_cErr = 4;
                      break;
                  case 3://Mifare_UltraLight
                      g_cStatus = ReadWriteUltralight( );
                      if (g_cStatus != true)
                      {    g_cErr = 3;    }
                      break;
                  case 4://Mifare_Std_4K
                      g_cStatus = ReadWriteMifareStd( );
                      if (g_cStatus != true)
                      {    g_cErr = 3;    }
                      break;
                  case 5://Mifare_ProX
                      g_cErr = 4;
                      break;
                  case 6://Mifare_DesFire
                      g_cErr = 4;
                      break;                
                 default:
                     g_cErr = 3;
                      break;
              }

//Glare Red_Led to indicate working ok              
              if (g_cErr == 0)
              {
              SendCom(TurnOnRedLed);                           
                  StartTime(8);                                    
                  while (!g_bReceOver && !g_bTimeOut);             
                  StopTime(); 
                 
                  StartTime(20);                                    
                  while (!g_bTimeOut);             
                  StopTime();
                 
                  SendCom(TurnOffRedLed);                           
                  StartTime(8);                                    
                  while (!g_bReceOver && !g_bTimeOut);             
                  StopTime();                                      
              }
         }
    }
}


////////////////////////////////////////////////////////////
char ReadWriteUltralight(void)
{
  
//Write data to page5 of UltraLight   
    SendCom(WriteULPage5);                    
    StartTime(8);                                    
    while (!g_bReceOver && !g_bTimeOut);             
    StopTime();                                      
    if ((g_ucReceBuf[2]!=0x11) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }

//read data from page5 of UltraLight            
    SendCom(ReadULPage5);                    
    StartTime(8);                                    
    while (!g_bReceOver && !g_bTimeOut);             
    StopTime();                                      
    if ((g_ucReceBuf[2]!=0x10) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }                                           

//Compare read data with written data       
    if (memcmp(&WriteULPage5[4], &g_ucReceBuf[4], 4) != 0 )
    {    return false;   }
    else
    {    return true;   }       
}

////////////////////////////////////////////////////////////
char ReadWriteMifareStd(void)
{
    long lPurseValue;

//Verify password of sector0  
    SendCom(LoginSector0);               
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x02) || (g_ucReceBuf[3]!=2) || g_bTimeOut)
    {    return false;   }              
            
//Write data to block1           
    SendCom(WriteBlock1);                
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x04) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }
    if (memcmp(&WriteBlock1[4], &g_ucReceBuf[4], 16) != 0)
    {    return false;   }                
            
//Read data from block1            
    SendCom(ReadBlock1);                
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x03) ||(g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }

//Compare read data with written data     
    if (memcmp(&WriteBlock1[4], &g_ucReceBuf[4], 16) != 0)
    {    return false;   }                  

//Verify password of sector1
    SendCom(LoginSector1);               
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x02) || (g_ucReceBuf[3]!=2) || g_bTimeOut)
    {    return false;   } 
                         
//Initialize block4 to one purse, and value = 0x01000000            
    SendCom(InitializeValue);                
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x06) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }
    if (memcmp(&InitializeValue[4], &g_ucReceBuf[4], 4) != 0)
    {    return false;   }    

//Increment           
    SendCom(IncrementValue);                
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x08) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   } 

//Decrement           
    SendCom(DecrementValue);                
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x09) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }   
            
//Backup purse to blcok5          
    SendCom(CopyValue);                
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x0A) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }                          
                          
//Read purse value from blcok5            
    SendCom(ReadValue);               
    StartTime(8);
    while (!g_bReceOver && !g_bTimeOut);
    StopTime();
    if ((g_ucReceBuf[2]!=0x05) || (g_ucReceBuf[3]!=0) || g_bTimeOut)
    {    return false;   }
   
//Check value   
    lPurseValue = g_ucReceBuf[7]*0x1000000 + g_ucReceBuf[6]*0x10000 + g_ucReceBuf[5]*0x100 + g_ucReceBuf[4];
    if (lPurseValue != 0x01000000 + 0x20000000 - 0x00000003)
    {    return false;   }
    else
    {    return true;   }

}
       
////////////////////////////////////////////////////////////
void InitializeSystem()
{
    TMOD &= 0x0F;
    TMOD |= 0x21;
    PCON |= 0x80;
    SCON  = 0x50;
    TH1 = TL1 = BOUND115200;
    g_ucReceNum = 0;
    g_bReceOver = 0;
    g_bReceReady = 0;
    TR1   = 1;
    IE   |= 0x90;
}

////////////////////////////////////////////////////////////
void StartTime(unsigned int _MS)
{
    TH0 = (unsigned char)((TIME0_10ms>>8)&0xFF);
    TL0 = (unsigned char)(TIME0_10ms&0xFF);
    g_ucWatchTime = _MS;
    g_bTimeOut = 0;
    ET0 = 1;
    TR0 = 1;
}  

////////////////////////////////////////////////////////////
time0_int () interrupt 1 using 1
{
    TH0 = (unsigned char)((TIME0_10ms>>8) & 0xFF);
    TL0 = (unsigned char)(TIME0_10ms & 0xFF);
    if (g_ucWatchTime--==0)
    {    g_bTimeOut = 1;    }
}

////////////////////////////////////////////////////////////
void StopTime()
{
    ET0 = 0;
    TR0 = 0;
}  

////////////////////////////////////////////////////////////
//Send command to SL025
////////////////////////////////////////////////////////////
void SendCom(unsigned char *g_cCommand)
{   
     unsigned char i,chkdata,sendleg;

     ES = 0;
     sendleg = *(g_cCommand+1) + 1;
    
     chkdata = 0;
     for(i=0; i<sendleg; i++)
     {   
          chkdata ^= *(g_cCommand+i);
         
          TI = 0;
          SBUF = *(g_cCommand+i);
          while(!TI);
     }
    
     TI = 0;
     SBUF = chkdata;
     while (!TI);
     TI = 0;
    
     g_bReceReady = 1;
     g_bReceOver = 0;
     ES = 1;
}

/////////////////////////////////////////////////////////////////////
//Get result from SL025
/////////////////////////////////////////////////////////////////////
seri_int () interrupt 4 using 1
{
   unsigned char i,j;
   unsigned char rece_data = SBUF;
   unsigned char verify = 0;
  
   if(RI)
   {   
       RI = 0;
       if (g_bReceReady)
       { 
           switch (rece_data)
           {   
               case 0xBD:
                    g_bReceReady = 0;
                    g_bReceOver = 0;
                    g_ucReceNum = 0;
                    g_ucReceBuf[0] = 0xBD;
                    g_ucReceBuf[1] = 0;
                    break;
               default:
                    g_ucReceNum++;
                    break;
           }
       }
       else
       {   
           i = ++g_ucReceNum;
           g_ucReceBuf[i] = rece_data;
      if (g_ucReceBuf[1] == g_ucReceNum - 1)
      {
          for (j=0; j<g_ucReceNum+1; j++)
               {   verify ^= g_ucReceBuf[j];    }
               g_bReceOver = 1;
               ES = 0;
           }
           if (g_ucReceNum > sizeof(g_ucReceBuf))
           {
               g_bReceOver = 0;
               g_ucReceNum = 0;
           }
        }
    }
   
    if(TI)
    {  
        TI = 0;
    }
}




RFID Module interface ARM Microcontroller & C Source code examples



RFID Module interface ARM Microcontroller & C Source code example
////////////////////////////////////////////////////////////
//Test SL025x
//ARM C source code
//LM3S600(6MHz) + EWARM 5.30
////////////////////////////////////////////////////////////  
#include "hw_types.h"
#include "hw_memmap.h"
#include "hw_uart.h"
#include "hw_ints.h"
#include "string.h"
#include "sysctl.h"
#include "gpio.h"
#include "systick.h"
#include "interrupt.h"
#include "uart.h"

//============================================
//   Uart bound define
//============================================
#define  BOUND4800         4800
#define  BOUND9600         9600
#define  BOUND14400        14400
#define  BOUND19200        19200
#define  BOUND28800        28800
#define  BOUND38400        38400
#define  BOUND57600        57600
#define  BOUND115200       115200

//============================================
//   Uart Status define
//============================================
#define UARTSTATUS_FREE    0
#define UARTSTATUS_TX      1
#define UARTSTATUS_TXSUCC  2
#define UARTSTATUS_RXSUCC  3
#define UARTSTATUS_RXERR   4

//============================================
//  Command List, preamble + length + command
//============================================
const unsigned char __packed SelectCard[]=      {0xBA,0x02,0x01 };      
const unsigned char __packed LoginSector0[]=    {0xBA,0x0A,0x02,0x00,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; 
const unsigned char __packed LoginSector1[]=    {0xBA,0x0A,0x02,0x01,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
const unsigned char __packed ReadBlock1[]=      {0xBA,0x03,0x03,0x01};                                    
const unsigned char __packed WriteBlock1[]=     {0xBA,0x13,0x04,0x01,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};                               
const unsigned char __packed ReadValue[]=       {0xBA,0x03,0x05,0x05}; 
const unsigned char __packed InitializeValue[]=   {0xBA,0x07,0x06,0x04,0x00,0x00,0x00,0x01};                               
const unsigned char __packed IncrementValue[]=  {0xBA,0x07,0x08,0x04,0x00,0x00,0x00,0x20};                               
const unsigned char __packed DecrementValue[]=  {0xBA,0x07,0x09,0x04,0x03,0x00,0x00,0x00};                                         
const unsigned char __packed CopyValue[]=       {0xBA,0x04,0x0A,0x04,0x05};
const unsigned char __packed ReadULPage5[]=     {0xBA,0x03,0x10,0x05};                                         
const unsigned char __packed WriteULPage5[]=    {0xBA,0x07,0x11,0x05,0x11,0x22,0x33,0x44};
const unsigned char __packed TurnOnRedLed[]=    {0xBA,0x03,0x40,0x01};    
const unsigned char __packed TurnOffRedLed[]=   {0xBA,0x03,0x40,0x00};            

//============================================
//  global variable define
//============================================
unsigned char g_cCardType;
unsigned long int g_iTimer;
unsigned char g_bOverTime;
unsigned char g_cUartTxCnt;
unsigned char g_cUartTxDataLen;
unsigned char *g_pUartTxDat;
unsigned char g_cUartTxCheckSum;
unsigned char g_cUartRxCnt;
unsigned char g_cUartRxBuf[60];
unsigned char g_cUartStatus;

//============================================
//  i/o port define
//============================================
#define CARDIN_SL025 GPIO_PORTD_BASE,GPIO_PIN_2

//============================================
//  procedure define
//============================================
void InitializeSystem();
void Start_Time(unsigned long int ms);
void Stop_Time();
void Test_SL025x();

void main()
{ InitializeSystem();
  g_iTimer=0;
  while(1) Test_SL025x();
}

//============================================
//   system clock interrupt procedure
//============================================
void SysTickIntHandler(void)
{ if(g_iTimer!=0)
  { g_iTimer--;
    if(g_iTimer==0) g_bOverTime=1;
  }
}

//============================================
//  initialize system hardware config
//  system clock,uart0
//============================================
void InitializeSystem()
{  
  //init sysclock
  //period=1ms
  SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_6MHZ);
  SysTickPeriodSet(SysCtlClockGet()/1000);
  SysTickEnable();
  SysTickIntEnable();
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
 
  //CARD_IN PD2
  GPIODirModeSet(GPIO_PORTD_BASE,GPIO_PIN_2,GPIO_DIR_MODE_IN);
  GPIOPadConfigSet(GPIO_PORTD_BASE,GPIO_PIN_2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
 
  //init Uart
  //baud=115200
  SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
  GPIOPinTypeUART(GPIO_PORTA_BASE,GPIO_PIN_0|GPIO_PIN_1);
  UARTDisable(UART0_BASE);
  UARTIntClear(UART0_BASE,UART_INT_OE|UART_INT_BE|UART_INT_PE|UART_INT_FE|UART_INT_RT|UART_INT_TX|UART_INT_RX);
  UARTIntDisable(UART0_BASE,UART_INT_OE|UART_INT_BE|UART_INT_PE|UART_INT_FE|UART_INT_RT|UART_INT_TX|UART_INT_RX);
  UARTConfigSetExpClk(UART0_BASE,SysCtlClockGet(),BOUND115200,(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));
  UARTFIFOLevelSet(UART0_BASE,UART_FIFO_TX1_8,UART_FIFO_RX1_8);
  HWREG(UART0_BASE+UART_O_LCRH) &= ~(UART_LCRH_FEN);
  UARTIntEnable(UART0_BASE,UART_INT_RX|UART_INT_TX);
  IntEnable(INT_UART0);
  g_cUartStatus=UARTSTATUS_FREE;
  g_cUartRxCnt=0;
 
  //interrupt enable
  IntMasterEnable();
}

//============================================
//  start timer
//============================================
void Start_Time(unsigned long int ms)
{ g_iTimer=ms;
  g_bOverTime=0;
}

//============================================
//  stop timer
//============================================
void Stop_Time()
{ g_iTimer=0;
}

void UARTISR_TX025x()
{ unsigned char tmp;
  if(g_cUartTxCnt==g_cUartTxDataLen)
  { if(g_cUartStatus==UARTSTATUS_TX)
    { g_cUartStatus=UARTSTATUS_TXSUCC;
      g_cUartRxCnt=0;
      UARTCharPutNonBlocking(UART0_BASE,g_cUartTxCheckSum);
    }
  }
  else
  { tmp=g_pUartTxDat[g_cUartTxCnt];
    g_cUartTxCheckSum=g_cUartTxCheckSum^tmp;
    g_cUartTxCnt++;
    UARTCharPutNonBlocking(UART0_BASE,tmp);
  }
}

void UARTISR_RX025x(unsigned char dat)
{ unsigned char CheckSum;
  unsigned char counter;
  g_cUartRxBuf[g_cUartRxCnt]=dat;
  g_cUartRxCnt++;
  if(g_cUartRxCnt==g_cUartRxBuf[1]+2)
  { CheckSum=0;
    for(counter=0;counter<g_cUartRxCnt;counter++) CheckSum=CheckSum^g_cUartRxBuf[counter];
    if(CheckSum==0) g_cUartStatus=UARTSTATUS_RXSUCC;
    else g_cUartStatus=UARTSTATUS_RXERR;
  }
}

void UART0_ISR(void)
{ unsigned long flag_int;
  unsigned char tmp;
  flag_int=UARTIntStatus(UART0_BASE,true);
  UARTIntClear(UART0_BASE,flag_int);
  if((flag_int&UART_INT_TX)==UART_INT_TX) UARTISR_TX025x();
  if((flag_int&UART_INT_RX)==UART_INT_RX)
  { tmp=UARTCharGetNonBlocking(UART0_BASE);
    UARTISR_RX025x(tmp);
  }
}

//============================================
//  send buffer by uart0
//============================================
void SendBuf_UART(unsigned char *dat,unsigned char len)
{ g_cUartStatus=UARTSTATUS_TX;
  g_pUartTxDat=dat;
  g_cUartTxDataLen=len;
  g_cUartTxCnt=0;
  g_cUartTxCheckSum=0;
  UARTISR_TX025x();
}

//============================================
//  SL025x1 test procedure
//============================================
void Test_SL025x()
{  
  #define RCVCMD_SL025x g_cUartRxBuf[2]
  #define RCVSTA_SL025x g_cUartRxBuf[3]
  unsigned long int lPurseValue;
  unsigned char g_cErr;
  g_cCardType=0xff;
 
  if((GPIOPinRead(CARDIN_SL025)&GPIO_PIN_2)==GPIO_PIN_2) return;
 
  //Get the unique serial number of Mifare Card
  SendBuf_UART((unsigned char *)(SelectCard),sizeof(SelectCard));  
  Start_Time(300);
  while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
  Stop_Time();  
  if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x01)||(RCVSTA_SL025x!=0)) return;
  g_cCardType=g_cUartRxBuf[g_cUartRxBuf[1]];
 
  switch(g_cCardType)
  {
    case 1:      //Mifare 1K
    case 4:      //Mifare 4K
      //Verify password of sector0  
      SendBuf_UART((unsigned char *)(LoginSector0),sizeof(LoginSector0));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x02)||(RCVSTA_SL025x!=0x02)) return;
     
      //Write data to block1        
      SendBuf_UART((unsigned char *)(WriteBlock1),sizeof(WriteBlock1));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x04)||(RCVSTA_SL025x!=0)) return;
      if(memcmp(&WriteBlock1[4],&g_cUartRxBuf[4],16)!=0) return;
     
      //Read data from block1
      SendBuf_UART((unsigned char *)(ReadBlock1),sizeof(ReadBlock1));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x03)||(RCVSTA_SL025x!=0)) return;
      if(memcmp(&WriteBlock1[4],&g_cUartRxBuf[4],16)!=0) return;
     
      //Verify password of sector1
      SendBuf_UART((unsigned char *)(LoginSector1),sizeof(LoginSector1));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x02)||(RCVSTA_SL025x!=0x02)) return;
     
      //Initialize block4 to one purse, and value = 0x01000000
      SendBuf_UART((unsigned char *)(InitializeValue),sizeof(InitializeValue));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x06)||(RCVSTA_SL025x!=0)) return;
      if(memcmp(&InitializeValue[4],&g_cUartRxBuf[4],4)!=0) return;
     
      //Increment
      SendBuf_UART((unsigned char *)(IncrementValue),sizeof(IncrementValue));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x08)||(RCVSTA_SL025x!=0)) return;
     
      //Decrement
      SendBuf_UART((unsigned char *)(DecrementValue),sizeof(DecrementValue));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x09)||(RCVSTA_SL025x!=0)) return;
     
      //Backup purse to blcok5
      SendBuf_UART((unsigned char *)(CopyValue),sizeof(CopyValue));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x0a)||(RCVSTA_SL025x!=0)) return;
     
      //Read purse value from blcok5           
      SendBuf_UART((unsigned char *)(ReadValue),sizeof(ReadValue));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x05)||(RCVSTA_SL025x!=0)) return;
     
      //Check value   
      lPurseValue=g_cUartRxBuf[7];
      lPurseValue=(lPurseValue<<8)+g_cUartRxBuf[6];
      lPurseValue=(lPurseValue<<8)+g_cUartRxBuf[5];
      lPurseValue=(lPurseValue<<8)+g_cUartRxBuf[4];
      if(lPurseValue!=0x01000000+0x20000000-0x00000003) return;
      g_cErr=0;
      break;
   

    case 3:      //Mifare_UltraLight
      SendBuf_UART((unsigned char *)(WriteULPage5),sizeof(WriteULPage5));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x11)||(RCVSTA_SL025x!=0)) return;
      SendBuf_UART((unsigned char *)(ReadULPage5),sizeof(ReadULPage5));
      Start_Time(300);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();  
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x10)||(RCVSTA_SL025x!=0)) return;
      if (memcmp(&WriteULPage5[4],&g_cUartRxBuf[4],4)!=0) return;
      g_cErr=0;
      break;
   
   
    case 2:      //Mifare_Pro
    case 5:      //Mifare_ProX
    case 6:      //Mifare_DesFire
      g_cErr=4;
      break;
   
   
    default:
      g_cErr=3;
      break;
  }
  if(g_cErr!=0) return;
 
  //Glare Red_Led to indicate working ok
  SendBuf_UART((unsigned char *)(TurnOnRedLed),sizeof(TurnOnRedLed));
  Start_Time(300);
  while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
  Stop_Time();
  Start_Time(300);
  while(g_bOverTime==0);
  Stop_Time();
  SendBuf_UART((unsigned char *)(TurnOffRedLed),sizeof(TurnOffRedLed));
  Start_Time(300);
  while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
  Stop_Time();
}









RFID Products Shop

RFID Module interface AVR Microcontroller & C Source code examples


RFID Module interface AVR Microcontroller & C Source code examples


////////////////////////////////////////////////////////////
//Test SL025x
//AVR C source code
//ATmega16(7.3728MHz) + EWAVR 5.20
//////////////////////////////////////////////////////////// 
#include <ioavr.h>
#include <intrinsics.h>
#include <avr_macros.h>
#include <iomacro.h>
#include <string.h>

//============================================
//  oscillator define
//============================================
#define OSC 7372800L

//============================================
//  Uart bound define
//============================================
#define  BOUND4800         4800
#define  BOUND9600         9600
#define  BOUND14400        14400
#define  BOUND19200        19200
#define  BOUND28800        28800
#define  BOUND38400        38400
#define  BOUND57600        57600
#define  BOUND115200       115200

//============================================
//  Pin define
//============================================
#define CARDIN PIND_Bit2

//============================================
// Uart Status define
//============================================
#define UARTSTATUS_FREE   0
#define UARTSTATUS_TX     1
#define UARTSTATUS_TXSUCC 2
#define UARTSTATUS_RXSUCC 3
#define UARTSTATUS_RXERR  4

//============================================
//  global variable define
//============================================
unsigned char g_cCardType;
unsigned long int g_iTimer;
unsigned char g_bOverTime;
unsigned char g_cUartTxCnt;
unsigned char g_cUartTxDataLen;
unsigned char *g_pUartTxDat;
unsigned char g_cUartTxCheckSum;
unsigned char g_cUartRxCnt;
unsigned char g_cUartRxBuf[60];
unsigned char g_cUartStatus;

//============================================
//  Command List, preamble + length + command
//============================================
const unsigned char SelectCard[]       ={0xBA,0x02,0x01 };     
const unsigned char LoginSector0[]     ={0xBA,0x0A,0x02,0x00,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
const unsigned char LoginSector1[]     ={0xBA,0x0A,0x02,0x01,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
const unsigned char ReadBlock1[]       ={0xBA,0x03,0x03,0x01};                                   
const unsigned char WriteBlock1[]      ={0xBA,0x13,0x04,0x01,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};                              
const unsigned char ReadValue[]        ={0xBA,0x03,0x05,0x05};
const unsigned char InitializeValue[]  ={0xBA,0x07,0x06,0x04,0x00,0x00,0x00,0x01};                              
const unsigned char IncrementValue[]   ={0xBA,0x07,0x08,0x04,0x00,0x00,0x00,0x20};                              
const unsigned char DecrementValue[]   ={0xBA,0x07,0x09,0x04,0x03,0x00,0x00,0x00};                                        
const unsigned char CopyValue[]        ={0xBA,0x04,0x0A,0x04,0x05};
const unsigned char ReadULPage5[]      ={0xBA,0x03,0x10,0x05};                                        
const unsigned char WriteULPage5[]     ={0xBA,0x07,0x11,0x05,0x11,0x22,0x33,0x44};
const unsigned char TurnOnRedLed[]     ={0xBA,0x03,0x40,0x01};   
const unsigned char TurnOffRedLed[]    ={0xBA,0x03,0x40,0x00};           

//============================================
//  procedure define
//============================================
void Init_Hardware();
void Start_Time(unsigned long int ms);
void Stop_Time();
void UARTISR_TX();
void SendBuf_UART(unsigned char *dat,unsigned char len);
void Test_SL025x();

void main()
{
  Init_Hardware();
  g_iTimer=0;
  while(1) Test_SL025x();
}

//============================================
//  initialize system hardware config
//  timer,uart,port
//============================================
void Init_Hardware()
{
  __disable_interrupt();
  DDRD=0x00;PORTD=0xff;
  TCCR0=0;
  TCNT0=184;
  TIMSK_TOIE0=1;
  TCCR0=0x05;
  UCSRB=0xd8;
  UBRRH=0x00;
  UBRRL=OSC/16/BOUND115200-1;
  g_cUartRxCnt=0;
  g_cUartStatus=UARTSTATUS_FREE;
  __enable_interrupt();
}

//============================================
//  start timer
//============================================
void Start_Time(unsigned long int ms)
{
  g_iTimer=ms;
  g_bOverTime=0;
}

//============================================
//  stop timer
//============================================
void Stop_Time()
{
  g_iTimer=0;
}

void UARTISR_TX()

  unsigned char tmp;
  if(g_cUartTxCnt==g_cUartTxDataLen)
  {
    if(g_cUartStatus==UARTSTATUS_TX)
    {
      g_cUartStatus=UARTSTATUS_TXSUCC;
      g_cUartRxCnt=0;
      UDR=g_cUartTxCheckSum;
    }
  }
  else
  {
    tmp=g_pUartTxDat[g_cUartTxCnt];
    g_cUartTxCheckSum=g_cUartTxCheckSum^tmp;
    g_cUartTxCnt++;
    UDR=tmp;
  }
}

//============================================
//  send buffer by uart
//============================================
void SendBuf_UART(unsigned char *dat,unsigned char len)
{
  g_cUartStatus=UARTSTATUS_TX;
  g_pUartTxDat=dat;
  g_cUartTxDataLen=len;
  g_cUartTxCnt=0;
  g_cUartTxCheckSum=0;
  UARTISR_TX();
}

//============================================
//  timer0 overflow interrupt
//  10ms
//============================================
#pragma vector = TIMER0_OVF_vect
__interrupt void Timer0_ISR(void)
{
  TCNT0=184;
  if(g_iTimer!=0)
  {
    g_iTimer--;
    if(g_iTimer==0) g_bOverTime=1;
  }
}

//============================================
//  uart rx interrupt
//============================================
#pragma vector = USART_RXC_vect
__interrupt void UART_ISR_RXC(void)
{
  unsigned char CheckSum;
  unsigned char counter;
  g_cUartRxBuf[g_cUartRxCnt]=UDR;
  g_cUartRxCnt++;
  if(g_cUartRxCnt==g_cUartRxBuf[1]+2)
  {
    CheckSum=0;
    for(counter=0;counter<g_cUartRxCnt;counter++) CheckSum=CheckSum^g_cUartRxBuf[counter];
    if(CheckSum==0) g_cUartStatus=UARTSTATUS_RXSUCC;
    else g_cUartStatus=UARTSTATUS_RXERR;
  }
}

//============================================
//  uart tx interrupt
//============================================
#pragma vector = USART_TXC_vect
__interrupt void UART_ISR_TXC(void)
{
  UARTISR_TX();
}

void Test_SL025x()
{
  #define RCVCMD_SL025x g_cUartRxBuf[2]
  #define RCVSTA_SL025x g_cUartRxBuf[3]
  unsigned long int lPurseValue;
  g_cCardType=0xff;
 
  if(CARDIN!=0) return;
 
  SendBuf_UART((unsigned char *)(SelectCard),sizeof(SelectCard)); 
  Start_Time(30);
  while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
  Stop_Time(); 
  if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x01)||(RCVSTA_SL025x!=0)) return;
  g_cCardType=g_cUartRxBuf[g_cUartRxBuf[1]];
 
  switch(g_cCardType)
  {
    case 1:   //Mifare 1k
    case 4:   //Mifare 4k
      //Verify password of sector0 
      SendBuf_UART((unsigned char *)(LoginSector0),sizeof(LoginSector0));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x02)||(RCVSTA_SL025x!=0x02)) return;
 
      //Write data to block1       
      SendBuf_UART((unsigned char *)(WriteBlock1),sizeof(WriteBlock1));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x04)||(RCVSTA_SL025x!=0)) return;
      if(memcmp(&WriteBlock1[4],&g_cUartRxBuf[4],16)!=0) return;
 
      //Read data from block1
      SendBuf_UART((unsigned char *)(ReadBlock1),sizeof(ReadBlock1));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x03)||(RCVSTA_SL025x!=0)) return;
      if(memcmp(&WriteBlock1[4],&g_cUartRxBuf[4],16)!=0) return;
 
      //Verify password of sector1
      SendBuf_UART((unsigned char *)(LoginSector1),sizeof(LoginSector1));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x02)||(RCVSTA_SL025x!=0x02)) return;
 
      //Initialize block4 to one purse, and value = 0x01000000
      SendBuf_UART((unsigned char *)(InitializeValue),sizeof(InitializeValue));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x06)||(RCVSTA_SL025x!=0)) return;
      if(memcmp(&InitializeValue[4],&g_cUartRxBuf[4],4)!=0) return;
    
      //Increment
      SendBuf_UART((unsigned char *)(IncrementValue),sizeof(IncrementValue));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x08)||(RCVSTA_SL025x!=0)) return;
    
      //Decrement
      SendBuf_UART((unsigned char *)(DecrementValue),sizeof(DecrementValue));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x09)||(RCVSTA_SL025x!=0)) return;
    
      //Backup purse to blcok5
      SendBuf_UART((unsigned char *)(CopyValue),sizeof(CopyValue));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x0a)||(RCVSTA_SL025x!=0)) return;
    
      //Read purse value from blcok5          
      SendBuf_UART((unsigned char *)(ReadValue),sizeof(ReadValue));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x05)||(RCVSTA_SL025x!=0)) return;
    
      //Check value  
      lPurseValue=g_cUartRxBuf[7];
      lPurseValue=(lPurseValue<<8)+g_cUartRxBuf[6];
      lPurseValue=(lPurseValue<<8)+g_cUartRxBuf[5];
      lPurseValue=(lPurseValue<<8)+g_cUartRxBuf[4];
      if(lPurseValue!=0x01000000+0x20000000-0x00000003) return;
    
      //Glare Red_Led to indicate working ok
      SendBuf_UART((unsigned char *)(TurnOnRedLed),sizeof(TurnOnRedLed));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();
      Start_Time(30);
      while(g_bOverTime==0);
      Stop_Time();
      SendBuf_UART((unsigned char *)(TurnOffRedLed),sizeof(TurnOffRedLed));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();
      break;
  

    case 3:   //Mifare Ultralight
      SendBuf_UART((unsigned char *)(WriteULPage5),sizeof(WriteULPage5));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x11)||(RCVSTA_SL025x!=0)) return;
      SendBuf_UART((unsigned char *)(ReadULPage5),sizeof(ReadULPage5));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time(); 
      if((g_cUartStatus!=UARTSTATUS_RXSUCC)||(RCVCMD_SL025x!=0x10)||(RCVSTA_SL025x!=0)) return;
      if (memcmp(&WriteULPage5[4],&g_cUartRxBuf[4],4)!=0) return;
    
      //Glare Red_Led to indicate working ok
      SendBuf_UART((unsigned char *)(TurnOnRedLed),sizeof(TurnOnRedLed));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();
      Start_Time(30);
      while(g_bOverTime==0);
      Stop_Time();
      SendBuf_UART((unsigned char *)(TurnOffRedLed),sizeof(TurnOffRedLed));
      Start_Time(30);
      while((g_cUartStatus!=UARTSTATUS_RXERR)&&(g_cUartStatus!=UARTSTATUS_RXSUCC)&&(g_bOverTime==0));
      Stop_Time();
      break;


    default:
      break;
  }
}






 RFID Products Shop