当前位置:网站首页>Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
2022-07-06 18:33:00 【Hua Weiyun】
One 、 Introduction to the environment
MCU: STM32F103ZET6
Development software : Keil5
Contactless read-write card module : MFRC522
Two 、 Function is introduced
Use MFRC522 The module completes the verification of IC Card number reading 、 Card type differentiation 、IC Card sector password modification 、 Read and write sector data and other functions ; The underlying the SPI Analog timing , It can be easily transplanted to other devices , Complete project development . Now many embedded job designs often use this module , such as : Campus card design 、 Water card recharge consumption design 、 Bus card recharge consumption design, etc .
3、 ... and 、MFR522 Introduce
MF RC522 Is applied to 13.56MHz In contactless communication, one of a series of chips of high integration read-write card . yes NXP The company aims at “ Three watches ” Application launched a low voltage 、 Low cost 、 Small size Contactless read-write card chip , It is a better choice for the research and development of intelligent instruments and portable handheld devices .
MF RC522 Advanced modulation and demodulation concepts are used , Fully integrated in 13.56MHz All types of passive contactless communication methods and protocols . Support ISO14443A Multi tier applications . The internal transmitter part can drive the reader / writer antenna and the antenna ISO 14443A/MIFARE Communication between card and transponder , No other circuits are needed . The receiver section provides a robust and effective demodulation and decoding circuit , Used for processing ISO14443A Compatible transponder signal . Digital part processing ISO14443A Frame and error detection ( Odd and even &CRC). Besides , It also supports fast CRYPTO1 encryption algorithm , Used to verify MIFARE Series of products .MFRC522 Support MIFARE? Higher speed contactless communication , Two way data transmission rate is as high as 424kbit/s.
As 13.56MHz A new member of the chip family of highly integrated read-write card series ,MF RC522 And MF RC500 and MF RC530 There are many similarities , At the same time, it also has many characteristics and differences . The communication between it and the host adopts serial communication with less connection , And according to different user needs , selection SPI、I2C Or serial UART( similar RS232) One mode , It helps to reduce the number of connections , narrow PCB Plate volume , cost reduction .
Four 、IC Card Introduction
Contactless IC Card is also called RF card , from IC chip 、 Induction antenna composition , Encapsulated in a standard PVC Inside the card , The chip and antenna have no exposed parts . It is a new technology developed in the world in recent years , It successfully combines RFID technology with IC Combine card technology , It's over ( There is no power supply in the card ) And the problem of contact free , It is a great breakthrough in the field of electronic devices . The card is at a certain distance ( Usually it is 5—10cm) Near the surface of the reader , Through the transmission of radio waves to complete the data reading and writing operation .
RF reader to IC The card sends a set of electromagnetic waves with a fixed frequency , There's one in the card LC Series resonant circuit , Its frequency is the same as that of the reader , So, under electromagnetic excitation ,LC A resonant circuit produces resonance , So there's a charge in the capacitor ; At the other end of this charge , Connected to an electronic pump with one-way conduction , The charge in the capacitor is sent to another capacitor for storage , When the accumulated charge reaches 2V when , This capacitor can be used as a power supply to provide working voltage for other circuits , Transmit the data in the card or receive the data from the reader .
Non contact IC Between the card and the card reader, the radio wave is used to complete the reading and writing operation . The communication frequency between the two is 13.56MHZ. Non contact IC The card itself is a passive card , When the reader / writer reads and writes the card , The signal sent by the reader / writer consists of two parts superimposed : Part is the power signal , After the signal is received by the card , With its own L/C Generate a moment of energy to supply the chip to work . The other part is instruction and data signal , Command the chip to read the data 、 modify 、 Storage, etc , And return the signal to the reader / writer , Complete a read-write operation . The reader and writer are generally controlled by single chip microcomputer , It is composed of special intelligent module and antenna , And equipped with PC Communication interface , Print port ,I/O Mouth, etc , In order to apply to different fields .
M1 Card detailed indicators
M1 What is a card M1 chip , It refers to the abbreviation of chip produced by NXP, a subsidiary of Philips , Its full name is NXP Mifare1 series , Commonly used S50 And S70 Two models .
M1(S50) Card details :
- Chip type :PhilipsMifare1ICS50
- storage capacity :8Kbit,16 Zones , Two passwords per partition ;
- Working frequency :13.56?MHz;
- Communication rate :106KBoud;
- Reading and writing distance :2.5~10cm;
- Reading and writing time :1~2ms;
- working temperature :-20℃~55℃;
- Erasure life :>100,000 Time ;
- Save the data :>10 year ;
- Dimensions :ISO Standard card 85.6x54x0.82;
- Packaging materials :PVC、PET、PETG、0.13mm Copper wire ;
Mifare S50 and Mifare S70 Also known as Mifare Standard、Mifare Classic、MF1, Abide by ISO14443A Standard cards are the most widely used 、 The most influential one . and Mifare S70 Is the capacity of the S50 Of 4 times ,S50 Is the capacity of the 1K byte ,S70 The capacity of is 4K byte .
The operation sequence and operation command of the reader / writer to the card , The two are exactly the same . Mifare S50 and Mifare S70 Every card has a 4 Globally unique serial number of bytes , The retention period of the data on the card is 10 year , It can be rewritten 10 Ten thousand times , Read infinite times . In general applications , Regardless of whether the card will be read or written bad ,
Of course, except for violent hard damage . Mifare S50 and Mifare S70 There are two main differences . First, the reader / writer sends a request command to the card , The card type returned by both responses (ATQA) Different bytes .Mifare S50 Type of card (ATQA) yes 0004H,Mifare S70 Type of card (ATQA) yes 0002H. Another difference is that the capacity and memory structure of the two are different .
M1 The card is divided into 16 Sectors , Each sector consists of 4 block (0、1、2、3) form . Operating time , take 16 The sectors are divided into 64 Block , The absolute address number is 0-63.
The structure is as follows :
- The first 0 A sector is used to store the manufacturer code , Opinion fixed line , Non modifiable .
- Blocks per sector 0、 block 1、 block 2 For data blocks , Can be used to store data . Data blocks can be read and written .
- Blocks per sector 3 For the control block , Including the password A、 Storage control 、 password B. The specific structure is as follows :
- The password and control bits of each sector are independent , You can set your own password and access control according to your actual needs . Access control is 4 Bytes , common 32 position , Every block in a sector ( Including data and control blocks ) Access conditions are determined by password and access control , In access control, each block has a corresponding three control bits . The definition is as follows :
Mifare 1 S50 General steps for white card reading and writing : Card search –> Download block password –> Read / write block data . The same is true for control blocks .
Access permission setting table of data block :( According to the permissions you need , Complete the above byte 6、7、8 Just fill in )
Control the read and write permission setting of the block :( Contains a pair of passwords A**、 Control authority 、 Read and write permission of password **)
7 6 5 4 3 2 1 0
byte 6 1 1 1 1 1 1 1 1
byte 7 0 0 0 0 1 1 1 1
byte 8 0 0 0 0 0 0 0 0
byte 9
The control permissions set are as follows :0xFF 0x0F 0x00 0x00
Represents the permissions of the data block : Verify password A Or the code B Can read and write data blocks or add value keys .
- Represents the permissions of the control block
(1) verification A After the password, you can write A/B password , Can't read the password .
Can read control bytes (4 individual ), Unable to write control byte Can read and write B password
(2) verification B After the code , Can read and write A/B password , Also readable control bytes , But the control byte cannot be written .
5、 ... and 、 Core code
5.1 rc522.c
#include "sys.h"#include "RFID_RC522.h"#include "delay.h"#include "string.h"#include "usart.h"/* The functionality : Porting interfaces --SPI Sequential reading and writing a byte function parameter :data: The data to be written is returned return value : Data read */u8 RC522_SPI_ReadWriteOneByte(u8 tx_data){ u8 rx_data=0; u8 i; for(i=0;i<8;i++) { RC522_SCLK=0; if(tx_data&0x80){RC522_OUTPUT=1;} else {RC522_OUTPUT=0;} tx_data<<=1; RC522_SCLK=1; rx_data<<=1; if(RC522_INPUT)rx_data|=0x01; } return rx_data; }/* The functionality : initialization RC522 Of IO mouth */void RC522_IO_Init(void){ RCC->APB2ENR|=1<<2; //PA Clock enable RCC->APB2ENR|=1<<7; //PF Clock enable //PA5 The clock RC522_SCLK //PA6 Input RC522_INPUT //PA7 Output RC522_OUTPUT GPIOA->CRL&=0x000FFFFF; GPIOA->CRL|=0x38300000; GPIOA->ODR|=0x3<<5; //RC522_RST <----->PF1-- Reset the foot //RC522_SDA <----->PF0-- Film selection foot GPIOF->CRL&=0xFFFFFF00; GPIOF->CRL|=0x00000033; GPIOF->ODR|=0x3<<0;} /* Function description : Select card, read card memory capacity input parameters :serNum The serial number of the incoming card is returned return value : Card capacity returned successfully */u8 RC522_MFRC522_SelectTag(u8 *serNum) // Read card memory capacity { u8 i; u8 status; u8 size; u8 recvBits; u8 buffer[9]; buffer[0]=PICC_ANTICOLL1; // Anti collision code 1 buffer[1]=0x70; buffer[6]=0x00; for(i=0;i<4;i++) { buffer[i+2]=*(serNum+i); //buffer[2]-buffer[5] Is the card serial number buffer[6]^=*(serNum+i); // Card check code } RC522_CalulateCRC(buffer,7,&buffer[7]); //buffer[7]-buffer[8] by RCR Check code RC522_ClearBitMask(Status2Reg,0x08); status=RC522_PcdComMF522(PCD_TRANSCEIVE,buffer,9,buffer,&recvBits); if((status==MI_OK)&&(recvBits==0x18)) size=buffer[0]; else size=0; return size; }/* The time delay function , Nanosecond */void RC522_Delay(u32 ns){ u32 i; for(i=0;i<ns;i++) { __nop(); __nop(); __nop(); }}/* The functionality :RC522 Chip initialization */void RC522_Init(void){ RC522_IO_Init(); //RC522 initialization RC522_PcdReset(); // Reset RC522 RC522_PcdAntennaOff(); // Turn off the antenna DelayMs(2); // Time delay 2 millisecond RC522_PcdAntennaOn(); // Turn on the antenna M500PcdConfigISOType('A'); // Set up RC632 How it works }/* The functionality : Reset RC522*/void RC522_Reset(void){ RC522_PcdReset(); // Reset RC522 RC522_PcdAntennaOff(); // Turn off the antenna DelayMs(2); // Time delay 2 millisecond RC522_PcdAntennaOn(); // Turn on the antenna } /* work can : Description of card search parameters : req_code[IN]: Card search method 0x52 = All in the sensing area are consistent with 14443A Standard card 0x26 = Find a card that has not entered sleep state pTagType[OUT]: Card type code 0x4400 = Mifare_UltraLight 0x0400 = Mifare_One(S50) 0x0200 = Mifare_One(S70) 0x0800 = Mifare_Pro(X) 0x4403 = Mifare_DESFire return return value : Successfully returns MI_OK*/char RC522_PcdRequest(u8 req_code,u8 *pTagType){ char status; u8 unLen; u8 ucComMF522Buf[MAXRLEN]; // MAXRLEN 18 RC522_ClearBitMask(Status2Reg,0x08); // clear RC522 Register bit ,/ Receive data command RC522_WriteRawRC(BitFramingReg,0x07); // Write RC632 register RC522_SetBitMask(TxControlReg,0x03); // Set up RC522 Register bit ucComMF522Buf[0]=req_code; // Card search method status=RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen); // adopt RC522 and ISO14443 Card communication if((status==MI_OK)&&(unLen==0x10)) { *pTagType=ucComMF522Buf[0]; *(pTagType+1)=ucComMF522Buf[1]; } else { status = MI_ERR; } return status;}/* work can : Description of anti-collision parameters : pSnr[OUT]: Card serial number ,4 Byte return return : Successfully returns MI_OK*/char RC522_PcdAnticoll(u8 *pSnr){ char status; u8 i,snr_check=0; u8 unLen; u8 ucComMF522Buf[MAXRLEN]; RC522_ClearBitMask(Status2Reg,0x08); // clear RC522 Register bit RC522_WriteRawRC(BitFramingReg,0x00); // Write RC522_ClearBitMask(CollReg,0x80); // clear ucComMF522Buf[0]=PICC_ANTICOLL1; //PICC_ANTICOLL1 = 0x93 ucComMF522Buf[1]=0x20; status=RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen); //0x0c, adopt RC522 and ISO14443 Card communication //PCD_TRANSCEIVE = Send and receive data //2: The length of data bytes written to the card //ucComMF522Buf: The address where the data is stored //unLen: The length of data read from the card if(status==MI_OK) { for(i=0;i<4;i++) { *(pSnr+i)=ucComMF522Buf[i]; // Assign the read card number to pSnr snr_check^=ucComMF522Buf[i]; } if(snr_check!=ucComMF522Buf[i]) { status = MI_ERR; } } RC522_SetBitMask(CollReg,0x80); return status;}/* work can : Description of selected card parameters :pSnr[IN]: Card serial number ,4 Byte return return : Successfully returns MI_OK*/char RC522_PcdSelect(u8 *pSnr){ char status; u8 i; u8 unLen; u8 ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0]=PICC_ANTICOLL1; ucComMF522Buf[1]=0x70; ucComMF522Buf[6]=0; for(i=0;i<4;i++) { ucComMF522Buf[i+2]=*(pSnr+i); ucComMF522Buf[6]^=*(pSnr+i); } RC522_CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]); // use MF522 Calculation CRC16 function , Check the data RC522_ClearBitMask(Status2Reg,0x08); // clear RC522 Register bit status=RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen); if((status==MI_OK)&&(unLen==0x18))status=MI_OK; else status=MI_ERR; return status;}/* work can : Verification card password parameter description :auth_mode[IN]: Password authentication mode 0x60 = verification A secret key 0x61 = verification B secret key addr[IN]: Block address pKey[IN]: Sector password pSnr[IN]: Card serial number ,4 Byte return return : Successfully returns MI_OK*/ char RC522_PcdAuthState(u8 auth_mode,u8 addr,u8 *pKey,u8 *pSnr){ char status; u8 unLen; u8 ucComMF522Buf[MAXRLEN]; //MAXRLEN 18( Size of array ) // Verification mode + Block address + Sector password + Card serial number ucComMF522Buf[0]=auth_mode; ucComMF522Buf[1]=addr; memcpy(&ucComMF522Buf[2],pKey,6); // Copy , Copy memcpy(&ucComMF522Buf[8],pSnr,4); status=RC522_PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen); if((status!= MI_OK)||(!(RC522_ReadRawRC(Status2Reg)&0x08)))status = MI_ERR; return status;}/* work can : Read M1 Description of data parameters of a card : addr: Block address p : Read out block data ,16 Byte return return : Successfully returns MI_OK*/ char RC522_PcdRead(u8 addr,u8 *p){ char status; u8 unLen; u8 i,ucComMF522Buf[MAXRLEN]; //18 ucComMF522Buf[0]=PICC_READ; ucComMF522Buf[1]=addr; RC522_CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); status=RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);// adopt RC522 and ISO14443 Card communication if((status==MI_OK&&(unLen==0x90))) { for(i=0;i<16;i++) { *(p +i)=ucComMF522Buf[i]; } } else { status=MI_ERR; } return status;}/* work can : Write data to the M1 Description of card specified block parameters :addr: Block address p : Data written to the block ,16 Byte return return : Successfully returns MI_OK*/ char RC522_PcdWrite(u8 addr,u8 *p){ char status; u8 unLen; u8 i,ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0]=PICC_WRITE;// 0xA0 // Write a piece ucComMF522Buf[1]=addr; // Block address RC522_CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); status=RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if((status!= MI_OK)||(unLen != 4)||((ucComMF522Buf[0]&0x0F)!=0x0A)) { status = MI_ERR; } if(status==MI_OK) { for(i=0;i<16;i++)// towards FIFO Write 16Byte data { ucComMF522Buf[i]=*(p +i); } RC522_CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]); status = RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen); if((status != MI_OK)||(unLen != 4)||((ucComMF522Buf[0]&0x0F)!=0x0A)) { status = MI_ERR; } } return status;}/* work can : Command the card to enter sleep state and return return : Successfully returns MI_OK*/char RC522_PcdHalt(void){ u8 status; u8 unLen; u8 ucComMF522Buf[MAXRLEN]; //MAXRLEN==18 status=status; ucComMF522Buf[0]=PICC_HALT; ucComMF522Buf[1]=0; RC522_CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); status=RC522_PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); return MI_OK;}/* work can : use MF522 Calculation CRC16 Function parameter Count : *pIn : To read CRC The data of len:- Data length *pOut: Calculated CRC result */void RC522_CalulateCRC(u8 *pIn ,u8 len,u8 *pOut ){ u8 i,n; RC522_ClearBitMask(DivIrqReg,0x04); //CRCIrq = 0 RC522_WriteRawRC(CommandReg,PCD_IDLE); RC522_SetBitMask(FIFOLevelReg,0x80); // clear FIFO The pointer // towards FIFO Middle write data for(i=0;i<len;i++) { RC522_WriteRawRC(FIFODataReg,*(pIn +i)); // Start RCR Calculation } RC522_WriteRawRC(CommandReg,PCD_CALCCRC); // wait for CRC The calculation is complete i=0xFF; do { n=RC522_ReadRawRC(DivIrqReg); i--; } while((i!=0)&&!(n&0x04));//CRCIrq = 1 // Read CRC The result of the calculation is pOut[0]=RC522_ReadRawRC(CRCResultRegL); pOut[1]=RC522_ReadRawRC(CRCResultRegM);}/* work can : Reset RC522 return return : Successfully returns MI_OK*/char RC522_PcdReset(void){ RC522_RST=1; //PF1 Write 1 RC522_Delay(10); RC522_RST=0; //PF1 clear 0 RC522_Delay(10); RC522_RST=1; //PF1 Write 1 RC522_Delay(10); RC522_WriteRawRC(CommandReg,PCD_RESETPHASE); // Write RC632 register , Reset RC522_WriteRawRC(CommandReg,PCD_RESETPHASE); // Write RC632 register , Reset RC522_Delay(10); RC522_WriteRawRC(ModeReg,0x3D); // and Mifare Card communication ,CRC Initial value 0x6363 RC522_WriteRawRC(TReloadRegL,30); // Write RC632 register RC522_WriteRawRC(TReloadRegH,0); RC522_WriteRawRC(TModeReg,0x8D); RC522_WriteRawRC(TPrescalerReg,0x3E); RC522_WriteRawRC(TxAutoReg,0x40);// It has to be return MI_OK;}/* The functionality : Set up RC632 How it works */char M500PcdConfigISOType(u8 type){ if(type=='A') //ISO14443_A { RC522_ClearBitMask(Status2Reg,0x08); // clear RC522 Register bit RC522_WriteRawRC(ModeReg,0x3D); //3F//CRC Initial value 0x6363 RC522_WriteRawRC(RxSelReg,0x86); //84 RC522_WriteRawRC(RFCfgReg,0x7F); //4F // Adjust the sensing distance of the card //RxGain = 48dB Adjust the card sensing distance RC522_WriteRawRC(TReloadRegL,30); //tmoLength);// TReloadVal = 'h6a =tmoLength(dec) RC522_WriteRawRC(TReloadRegH,0); RC522_WriteRawRC(TModeReg,0x8D); RC522_WriteRawRC(TPrescalerReg,0x3E); RC522_Delay(1000); RC522_PcdAntennaOn(); // Turn on the antenna } else return 1; // Failure , return 1 return MI_OK; // Successfully returns 0}/* work can : read RC632 Register parameter description :Address[IN]: Register address returns return : Read out value */u8 RC522_ReadRawRC(u8 Address){ u8 ucAddr; u8 ucResult=0; RC522_CS=0; // Film selection RC522 ucAddr=((Address<<1)&0x7E)|0x80; RC522_SPI_ReadWriteOneByte(ucAddr); // dispatch orders ucResult=RC522_SPI_ReadWriteOneByte(0); // Read RC522 Returned data RC522_CS=1; // Release the selection line (PF0) return ucResult; // Returns the data read }/* work can : Write RC632 Register parameter description :Address[IN]: Register address value[IN] : Value written */void RC522_WriteRawRC(u8 Address,u8 value){ u8 ucAddr; RC522_CS=0; //PF0 Write 0 (SDA)(SPI1 Film selection line , Low level active ) ucAddr=((Address<<1)&0x7E); RC522_SPI_ReadWriteOneByte(ucAddr); //SPI1 Send a byte RC522_SPI_ReadWriteOneByte(value); //SPI1 Send a byte RC522_CS=1; //PF1 Write 1(SDA)(SPI1 Film selection line )}/* work can : Set up RC522 Description of register bit parameters :reg[IN]: Register address mask[IN]: Set value */void RC522_SetBitMask(u8 reg,u8 mask) { char tmp=0x0; tmp=RC522_ReadRawRC(reg); // read RC632 register RC522_WriteRawRC(reg,tmp|mask); // Write RC632 register }/* work can : clear RC522 Description of register bit parameters :reg[IN]: Register address mask[IN]: Clearance value */void RC522_ClearBitMask(u8 reg,u8 mask) { char tmp=0x0; tmp=RC522_ReadRawRC(reg); // read RC632 register RC522_WriteRawRC(reg,tmp&~mask); // clear bit mask} /* work can : adopt RC522 and ISO14443 Description of card communication parameters :Command[IN]:RC522 Command word pIn [IN]: adopt RC522 Data sent to the card InLenByte[IN]: The byte length of the transmitted data pOut [OUT]: The received card returns data *pOutLenBit[OUT]: Returns the bit length of the data */char RC522_PcdComMF522(u8 Command,u8 *pIn,u8 InLenByte,u8 *pOut,u8 *pOutLenBit){ char status=MI_ERR; u8 irqEn=0x00; u8 waitFor=0x00; u8 lastBits; u8 n; u16 i; switch(Command) { case PCD_AUTHENT: // Verify key irqEn=0x12; waitFor=0x10; break; case PCD_TRANSCEIVE: // Send and receive data irqEn=0x77; waitFor=0x30; break; default: break; } RC522_WriteRawRC(ComIEnReg,irqEn|0x80); RC522_ClearBitMask(ComIrqReg,0x80); // Clear all middle breaks RC522_WriteRawRC(CommandReg,PCD_IDLE); RC522_SetBitMask(FIFOLevelReg,0x80); // clear FIFO cache for(i=0;i<InLenByte;i++) { RC522_WriteRawRC(FIFODataReg,pIn[i]); } RC522_WriteRawRC(CommandReg,Command); if(Command==PCD_TRANSCEIVE) { RC522_SetBitMask(BitFramingReg,0x80); // Start transmission } // There is a problem , The following loop //i = 600;// Adjust according to the clock frequency , operation M1 Maximum waiting time for card 25ms i=2000; do { n=RC522_ReadRawRC(ComIrqReg); i--; } while((i!=0)&&!(n&0x01)&&!(n&waitFor)); RC522_ClearBitMask(BitFramingReg,0x80); if(i!=0) { if(!(RC522_ReadRawRC(ErrorReg)&0x1B)) { status=MI_OK; if(n&irqEn&0x01) { status=MI_NOTAGERR; } if(Command==PCD_TRANSCEIVE) { n=RC522_ReadRawRC(FIFOLevelReg); lastBits=RC522_ReadRawRC(ControlReg)&0x07; if(lastBits) { *pOutLenBit=(n-1)*8+lastBits; } else { *pOutLenBit=n*8; } if(n==0)n=1; if(n>MAXRLEN)n=MAXRLEN; for(i=0; i<n; i++) { pOut[i]=RC522_ReadRawRC(FIFODataReg); } } } else { status=MI_ERR; } } RC522_SetBitMask(ControlReg,0x80);// stop timer now RC522_WriteRawRC(CommandReg,PCD_IDLE); return status;}/* The functionality : Turn on the antenna ginseng Count : There should be at least... Between each start-up or shutdown of natural danger launch 1ms The interval of */void RC522_PcdAntennaOn(void){ u8 i; i=RC522_ReadRawRC(TxControlReg); if(!(i&0x03)) { RC522_SetBitMask(TxControlReg,0x03); }}/* The functionality : Turn off the antenna ginseng Count : There should be at least... Between each start-up or shutdown of natural danger launch 1ms The interval of */void RC522_PcdAntennaOff(void){ RC522_ClearBitMask(TxControlReg,0x03); // clear RC522 Register bit }
5.2 rc522.h
#ifndef RFID_RC522_H#define RFID_RC522_H#include "sys.h"/*RC522 External interface of RF module : *1--SDA <----->PF0-- Film selection foot *2--SCK <----->PA5-- Clock line *3--MOSI<----->PA7-- Output *4--MISO<----->PA6-- Input *5-- In the air *6--GND <----->GND*7--RST <----->PF1-- Reset the foot *8--VCC <----->VCC*/#define RC522_OUTPUT PAout(7)#define RC522_INPUT PAin(6)#define RC522_SCLK PAout(5)#define RC522_CS PFout(0)#define RC522_RST PFout(1)//MF522 Command word #define PCD_IDLE 0x00 // Cancel the current command #define PCD_AUTHENT 0x0E // Verify key #define PCD_RECEIVE 0x08 // receive data #define PCD_TRANSMIT 0x04 // send data #define PCD_TRANSCEIVE 0x0C // Send and receive data #define PCD_RESETPHASE 0x0F // Reset #define PCD_CALCCRC 0x03 //CRC Calculation //Mifare_One Card command word #define PICC_REQIDL 0x26 // The antenna searching area is not in sleep state , Returns the type of card #define PICC_REQALL 0x52 // All cards in the antenna searching area , Returns the type of card #define PICC_ANTICOLL1 0x93 // Collision proof #define PICC_ANTICOLL2 0x95 // Collision proof #define PICC_AUTHENT1A 0x60 // verification A secret key #define PICC_AUTHENT1B 0x61 // verification B secret key Command authentication code #define PICC_READ 0x30 // Reading block #define PICC_WRITE 0xA0 // Write a piece #define PICC_DECREMENT 0xC0 // Deduction #define PICC_INCREMENT 0xC1 // Recharge #define PICC_RESTORE 0xC2 // Block data to buffer #define PICC_TRANSFER 0xB0 // Save data in buffer #define PICC_HALT 0x50 // Sleep //MF522 FIFO Length definition #define DEF_FIFO_LENGTH 64 //FIFO size=64byte#define MAXRLEN 18//MF522 Register definition // PAGE 0#define RFU00 0x00 #define CommandReg 0x01 #define ComIEnReg 0x02 #define DivlEnReg 0x03 #define ComIrqReg 0x04 #define DivIrqReg 0x05#define ErrorReg 0x06 #define Status1Reg 0x07 #define Status2Reg 0x08 #define FIFODataReg 0x09#define FIFOLevelReg 0x0A#define WaterLevelReg 0x0B#define ControlReg 0x0C#define BitFramingReg 0x0D#define CollReg 0x0E#define RFU0F 0x0F// PAGE 1 #define RFU10 0x10#define ModeReg 0x11#define TxModeReg 0x12#define RxModeReg 0x13#define TxControlReg 0x14#define TxAutoReg 0x15#define TxSelReg 0x16#define RxSelReg 0x17#define RxThresholdReg 0x18#define DemodReg 0x19#define RFU1A 0x1A#define RFU1B 0x1B#define MifareReg 0x1C#define RFU1D 0x1D#define RFU1E 0x1E#define SerialSpeedReg 0x1F// PAGE 2 #define RFU20 0x20 #define CRCResultRegM 0x21#define CRCResultRegL 0x22#define RFU23 0x23#define ModWidthReg 0x24#define RFU25 0x25#define RFCfgReg 0x26#define GsNReg 0x27#define CWGsCfgReg 0x28#define ModGsCfgReg 0x29#define TModeReg 0x2A#define TPrescalerReg 0x2B#define TReloadRegH 0x2C#define TReloadRegL 0x2D#define TCounterValueRegH 0x2E#define TCounterValueRegL 0x2F// PAGE 3 #define RFU30 0x30#define TestSel1Reg 0x31#define TestSel2Reg 0x32#define TestPinEnReg 0x33#define TestPinValueReg 0x34#define TestBusReg 0x35#define AutoTestReg 0x36#define VersionReg 0x37#define AnalogTestReg 0x38#define TestDAC1Reg 0x39 #define TestDAC2Reg 0x3A #define TestADCReg 0x3B #define RFU3C 0x3C #define RFU3D 0x3D #define RFU3E 0x3E #define RFU3F 0x3F// and MF522 Error code returned during communication #define MI_OK 0#define MI_NOTAGERR 1#define MI_ERR 2#define SHAQU1 0X01#define KUAI4 0X04#define KUAI7 0X07#define REGCARD 0xa1#define CONSUME 0xa2#define READCARD 0xa3#define ADDMONEY 0xa4/* RC522 Various driving functions */u8 RC522_SPI_ReadWriteOneByte(u8 tx_data);void RC522_IO_Init(void);u8 RC522_MFRC522_SelectTag(u8 *serNum);void RC522_Delay(u32 ns);void RC522_Init(void);void RC522_Reset(void);char RC522_PcdRequest(u8 req_code,u8 *pTagType);char RC522_PcdAnticoll(u8 *pSnr);char RC522_PcdSelect(u8 *pSnr);char RC522_PcdAuthState(u8 auth_mode,u8 addr,u8 *pKey,u8 *pSnr);char RC522_PcdRead(u8 addr,u8 *p);char RC522_PcdWrite(u8 addr,u8 *p);char RC522_PcdHalt(void);void RC522_CalulateCRC(u8 *pIn ,u8 len,u8 *pOut );char RC522_PcdReset(void);char M500PcdConfigISOType(u8 type);char M500PcdConfigISOType(u8 type);u8 RC522_ReadRawRC(u8 Address);void RC522_WriteRawRC(u8 Address,u8 value);void RC522_SetBitMask(u8 reg,u8 mask) ;void RC522_ClearBitMask(u8 reg,u8 mask);char RC522_PcdComMF522(u8 Command,u8 *pIn,u8 InLenByte,u8 *pOut,u8 *pOutLenBit);void RC522_PcdAntennaOn(void);void RC522_PcdAntennaOff(void);#endif
边栏推荐
- Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery
- CRMEB 商城系统如何助力营销?
- Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock
- 2019阿里集群数据集使用总结
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Virtual machine VirtualBox and vagrant installation
- 小程序在产业互联网中的作用
- ADB common commands
- 從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
- UDP协议:因性善而简单,难免碰到“城会玩”
猜你喜欢
I want to say more about this communication failure
declval(指导函数返回值范例)
推荐好用的后台管理脚手架,人人开源
287. 寻找重复数
Tree-LSTM的一些理解以及DGL代码实现
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
Compilation Principle -- C language implementation of prediction table
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
阿里云国际版ECS云服务器无法登录宝塔面板控制台
小程序在产业互联网中的作用
随机推荐
atcoder它A Mountaineer
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
Blue Bridge Cup real question: one question with clear code, master three codes
Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti
I want to say more about this communication failure
简单易用的PDF转SVG程序
Prophet模型的简介以及案例分析
2019阿里集群数据集使用总结
Celery best practices
從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
C语言自动预订飞机票问题
Five data structures of redis
Cocos2d Lua smaller and smaller sample memory game
Afnetworking framework_ Upload file or image server
使用cpolar建立一个商业网站(1)
Compilation principle - top-down analysis and recursive descent analysis construction (notes)
测试123
2022 Summer Project Training (I)
Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!
287. Find duplicates