Combining 51 code code parsing rfid card reader programming ideas

Publisher:cannon928Latest update time:2017-01-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have been busy with this RFID module in the past two days. First of all, I admit that I am a novice. The platform is based on the 51 single-chip microcomputer for beginners, but I still summarize what I have gained from reading the code in the past two days.

Software design of the card reader: It seems that the PDF document has been given, but it takes a long time for beginners to understand it. Here I show the design process in the datasheet, and combine it with code analysis to achieve the effect of identifying the card.


First of all, it seems to be a reset response. According to the description in the datasheet, the communication protocol and baud rate of the MIFARE radio frequency card are defined. When a card enters the operating range of the card reader, the card reader will communicate with it using a specific protocol to determine whether the card entering is a MIFARE radio frequency card.


 


In fact, this paragraph on the datasheet seems to be very impressive, but in fact it gives people a feeling of being confused. In fact, after reading the code, I know that Liu Chen on the software is called initialization. It is necessary to perform a software reset on the card reader and set the working mode of the card reader.


 


Here the code is:


 PcdReset(); //rc522 initialization

 PcdAntennaOff(); //Turn off the antenna

 PcdAntennaOn(); //Turn on the antenna

 M500PcdConfigISOType('A'); //Set working mode

 


 The second step is anti-collision. The datasheet says: When multiple cards enter the sensing range of the card reader, the anti-collision mechanism will be activated and automatically operate from multiple cards. After that, there seems to be a lot of words about how to prevent collision.


In fact, I personally feel that the code is more important, because without reading the card, where does the anti-collision come from? In fact, after talking so much about anti-collision, it is just a function to implement it.


 status = PcdRequest(PICC_REQALL, g_ucTempbuf);

        //PICC_REQALL is a macro definition which means to find all cards in the antenna.

        // g_ucTempbuf is an array. Here the function reads the first two digits in the card and puts them in the array.

   status = PcdAnticoll(g_ucTempbuf); // Anti-collision

 


What's interesting here is that sometimes you need to determine the type of card. Here is a snippet that should be used in the program.


Determine the type of card and the first bit of data returned when reading the card. Other functions use 12864, so you don't need to go into details here.


                 //If there is a card, determine what card it is and then display it on the LCD

                    // 0x4400 = Mifare_UltraLight

                    // 0x0400 = Mifare_One(S50)

                    // 0x0200 = Mifare_One(S70)

                    // 0x0800 = Mifare_Pro(X)

                    // 0x4403 = Mifare_DESFire

                 switch(g_ucTempbuf[0])

                 {

                     case 0x44:

                             ck12864_com(0x93);

                            for(i=0;i<10;i++)

                            {

                                    ck12864_data(leixing1[i]);

                            }

                         break;

                     case 0x02:

                          ck12864_com(0x93);

                             for(i=0;i<8;i++)

                            {

                                    ck12864_data(leixing2[i]);

                            }

                         break;

                     case 0x04:

                          ck12864_com(0x93);

                            for(i=0;i<8;i++)

                            {

                                    ck12864_data(leixing3[i]);

                            }

                         break;

                     case 0x08:

                          ck12864_com(0x93);

                             for(i=0;i<6;i++)

                            {

                                    ck12864_data(leixing4[i]);

                            }

                         break;

                 }

                 

 


The next step is process 3, where you select a card, operate on it, and get the serial number of the selected card according to the datasheet, and return the card capacity at the same time:


Code:


 status = PcdSelect(g_ucTempbuf);

 


Process 4, that is, to operate the selected card, first verify the password, which includes read and write operations


The implementation of the code is also two sentences:


  status = PcdAuthState(PICC_AUTHENT1A, 5, DefaultKey, g_ucTempbuf);

         if (status != MI_OK)

         { continue; }

         //Write data to the block

         status = PcdWrite(5, data1);

         if (status != MI_OK)

         { continue; }

         //Read a piece of data


Step 5: Put the card into sleep mode:


     PcdHalt();

 


 These 5 steps can realize the specific operation process of the card. Now we control the stepper motor by swiping the card, and we can actually skip step 4.


If you want to open the door without a specific card number, you can now do it.


Sample code:

#include

#include "mian.h"

#include "rc522.h"


typedef unsigned int uint;

typedef unsigned char uchar;

uchar status;

uchar g_ucTempbuf[20];


void main()

{

    uint i;

    //initialization:

    PcdReset(); //rc522 initialization

    PcdAntennaOff(); //Turn the antenna off and on

    PcdAntennaOn();

    M500PcdConfigISOType('A'); //Set working mode


    // Anti-collision, a loop is needed here to let the card reader read the card continuously

    while(1)

    {

        status = PcdRequest(PICC_REQALL, g_ucTempbuf);

        //PICC_REQALL is a macro definition which means to find all cards in the antenna.

        // g_ucTempbuf is an array. Here the function reads the first two digits in the card and puts them in the array.

        if(status != MI_OK) //No card found, continue to execute PcdRequest()

        {

              continue;

        }

        

        status = PcdAnticoll(g_ucTempbuf); // Anti-collision

        //Card serial number, 4 bytes, the status here can be used to judge the execution status of PcdAnticoll

        //If the execution is successful, it means that the unique card number has been recorded on g_ucTempbuf

        //Here g_ucTempbuf has used 2+4

        if(status != MI_OK) //No card found, continue to execute PcdRequest()

        {

              continue;

        }

        

        PcdHalt();

        if(status == MI_OK)

        {

          LED_GREEN =0;

          for(i=0;i<125;i++)

             {

                step();

              }

          LED_GREEN = 1;

           }

    

       }

}


void DelayMs(unsigned int _MS)

{

    TH1 = (unsigned char)(RCAP2_1ms>>8);

    TL1 = (unsigned char)(RCAP2_1ms);


    ET1 = 0; // Disable timer2 interrupt

    TR1 = 1;

    while (_MS--)

    {  

        while (!TF1);

        TF1 = 0;

        TH1 = (unsigned char)(RCAP2_1ms>>8);

        TL1 = (unsigned char)(RCAP2_1ms);

    }

    TR1 = 0;

}


Analyze the code: This code only contains a card search, anti-collision, and confirming that the door can be opened with a card. This is the most primitive door opening routine, just like turning on an LED light. The next work only requires specific operations on the card.


Reference address:Combining 51 code code parsing rfid card reader programming ideas

Previous article:Access control system based on 51, human infrared sensor and RC522
Next article:The solution to the problem of microcontroller USB to serial port conversion sometimes working and sometimes not working

Recommended ReadingLatest update time:2024-11-16 14:53

Design of RFID high frequency interface module with embedded XPM memory
1. Introduction RFID (Radio Frequency Identification) technology is an automatic identification technology that has matured since the 1980s and has developed rapidly in recent years. Its technology covers a wide range, and many automatic identification technologies in use can be classified as RFID technolog
[Embedded]
Design of RFID high frequency interface module with embedded XPM memory
Analysis of Common Problems of Invengo RFID Readers
1. Interference between multiple readers When two or more readers are working at the same time, in order to prevent adjacent readers from interfering with each other, the following two requirements must be met during installation and commissioning: 1) The center distance between the antennas of two adjacen
[Analog Electronics]
Design and testing of RFID board-level electronic tag verification platform
Here, we first introduce the working principle of electronic tags and the ISO18000-6C standard, and design the overall circuit to implement the ultra-high frequency electronic tag verification platform according to the ISO18000-6C standard. We focus on the design and implementation of the digital baseband part based on
[Embedded]
Analysis of RFID card reading system based on S3C6410 and CR95HF
Abstract: Aiming at the shortcomings of low main frequency, slow speed and poor portability of current card readers, a design scheme of RFID card reader system based on S3C6410 is proposed. This paper takes the high-performance S3C6410 embedded microprocessor as the core, selects the new CR95HF radio frequency chip, a
[Microcontroller]
Analysis of RFID card reading system based on S3C6410 and CR95HF
Various Test and Measurement Methods for RFID Tags
In the past few years, RFID technology has been continuously developing. RFID has evolved from a specific application in the past to a technology widely used by logistics companies, for example, to read encrypted information in package tags or airport baggage tags. According to a market survey by IDTechEx, the global R
[Test Measurement]
Various Test and Measurement Methods for RFID Tags
Design and application of smart grid based on RFID
1. Introduction Smart grid is the intelligence of power grid. It is based on integrated, high-speed two-way communication network. Through the application of advanced sensing and measurement technology, equipment technology, control method and decision support system technology, it realizes the goal of reli
[Power Management]
Design and application of smart grid based on RFID
A Brief Analysis of RFID (Radio Frequency Identification) Information Security and Its Countermeasures
RFID (Radio Frequency Identification) technology is an automatic identification technology that has matured since the 1980s. This paper analyzes the potential safety hazards of RFID systems in detail and proposes relevant countermeasures in a targeted manner. As the new darling of wireless application, RFID is bei
[Security Electronics]
Comparison and analysis of various RFID payment technologies
Comparison and analysis of several RFID payment technologies: RFID payment The technology itself is relatively mature, but how to combine it with mobile phones for application is still in the exploratory stage. There are currently a variety of RFID payment technology solutions, mainly including patch
[Analog Electronics]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号