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.
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
- Popular Resources
- Popular amplifiers
- Radio Frequency Identification (RFID) System Technology and Application (Written by Ci Xinxin, Wang Subin, and Wang Shuo)
- Introduction to Internet of Things Engineering 2nd Edition (Gongyi Wu)
- Detailed explanation and engineering practice of unmanned monitoring technology (Xie Jianbin, Li Peiqin, Yan Wei, Liu Tong, Lin Chenglong, Hong Quanyi, Zhou Hongfei, Cui Yibing)
- Introduction to Wireless Sensor Networks (Edited by Ma Sasa et al.)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Why does the STM series MCU use SMD T card instead of traditional T card?
- The problem that the main function cannot be run after downloading the program with IAR compiler
- Added multiple ST sensor drivers in MakeCode
- Selection of power device heat sink
- LabView Note 4: Temperature Monitoring System Based on LabView Password Login
- The command configurations in the manual are all expressed in hexadecimal. How should they be used in the code?
- Gizwits Timer Switch
- Getting Started Manual for Oscilloscopes Written by Tektronix
- Learn “how to arrange” and “how to connect”, PCB design specifications are so easy!
- Here are some questions about the ground insulation detection circuit